Java Decision-Making Statements – Tutorial Chat

0
107

Java decision-making statements allow you to make decisions, based upon the result of a condition. As like all programs, Java too have a set of statements, which are executed sequentially in the order in which they appear. This happens when jumping of statements or repetition of certain calculations is not necessary. However, there may arise some situations where programmers have to change the order of execution of statements based on certain conditions which involve kind of decision-making statements. In this chapter, you will learn about how the control flow statements work.

List of Java Decision-Making Statements Are as Follows:

  • if Statement
    • if statement
    • if-else statement
    • else-if statement
  • Conditional Operator
  • switch statement

Now, Let us discuss Each and Everyone in Detail

If Statement

“if” statement in Java is used to control the program flow based on some condition, it’s used to execute some statement code block if expression is evaluated to true, otherwise it will get skipped.

Syntax:

if(test_expression)

{

    statement 1;

    statement 2;

    …

}

If-Else Statement

It is also used to control the program flow based on some condition, only the difference is: it’s used to execute some statement code block if expression is evaluated to true, otherwise executes else statement code block.

Syntax:

if(test_expression)

{

   //execute your code

}

else

{

   //execute your code

}

Else-If Statement

It is like another if condition, it’s used in the program when if statement having multiple decisions.

Syntax:

if(test_expression)

{

   //execute your code

}

else if(test_expression n)

{

   //execute your code

}

else

{

   //execute your code

}

Switch Statement

Java switch statement is used when you have multiple possibilities for the if statement

Syntax:

switch(variable)

{

case 1:

   //execute your code

break;

case n:

   //execute your code

break;

default:

   //execute your code

break;

}

Conditional Operator

This operator is used to make conditional expressions.

Syntax:

expression1? expression2: expression3

If we get “true” or a non-zero number as the result of expression1, expression2 will be evaluated and the result of expression2 will be the overall result of the conditional expression.

Conclusion

In this article, we have discussed decision-making using if statements and switch case.

  • An if statement executes its body if a specific condition is met.
  • An if statement may have an else part that is executed if the condition is not met.
  • Optionally, one or more else if parts could be used as alternative(s) if the first condition is not met.
  • The switch case structure is useful when one variable is to be checked against a set of values.

Here You Will Also Get a FREE Guide on 

Java Variable 

Java Modifier

Java Class

In the next article, we will talk about “Numbers Class” and it’s an interesting topic that you shouldn’t miss. So, stay here. Subscribe Jntu World to get live notifications to get FREE tutorial guide and training on programming!

Java Decision-Making Statements – Tutorial Chat
Rate this post

LEAVE A REPLY

Please enter your comment!
Please enter your name here