Control Statement in Java

Control Statements in Java

What are Control Statements

Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Show below is the general form of a typical decision making structure found in most of the programming languages

Decision making statements in Java

  • If Statement
  • If else
  • If else If
  • Switch Statement

if Statement

the single if statement in java language is used to execute if condition is true. the syntax of if statement given below.

if (expression)
{
       // statement to be executed
}

if-else Statement

the if-else statement in java language is used to execute if condition is true or false. the syntax of if-else statement given below.

if (expression)
{
       // statement to be executed if condition is true
}
else
{
       // statement to be executed if condition is false
}

if-else-if Statement

the if-else-if statement in java language is used to execute one condition from multiple statements. the syntax of if-else-if statement given below.

if(condition1)
{
    //code to be executed if condition1 is true  
}

else if(condition2)
{  
    //code to be executed if condition2 is true  
}  

else if(condition3)
{  
    //code to be executed if condition3 is true  
}  

else
{  
    //code to be executed if all the conditions are false  
}

Switch Statement

the switch statement in java language is used to execute one statement from multiple conditions. It is like if-else-if statement. the syntax of if-else-if statement given below.

switch(expression)
{
    case value1:    
    //code to be executed;    
    break;  //optional  
    case value2:    
    //code to be executed;    
    break;  //optional  
    default:     
    code to be executed if all cases are not matched;    
}