Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Java Control Statements- if-else and switch statements

Lets begin with a tutorial on Java how to program series next session called as Control Statements. Control statements are decision making statements in Java. They are basically used in Java source code to perform thought process i.e they are control flow statements which control flow of execution of Java code by decision making, looping and branching. Now we can control certain block of code by enabling these control statements in Java. There are various different types of control statements used in Java such as : If else statement, switch statement, while statement, break statement, continue statement, do statement and for statement etc. Let us look each of them in detail below :

The if Statement
In Java if statement is most widely used. The if statement is control statement which helps programmers in decision making. The if statement uses boolean expression in making decision whether a particular statement would execute or not. Lets look at syntax of if statement :


 Syntax : 

 if ( boolean-expression )
 { 
   if-code;
 } 

 rest-of-code; 



The if statement is a type of conditional branching statement. By the conditional branching we mean that branching of flow depends on some conditions. The decision making is done based on true or false. Looking at the above syntax and below diagram we can come to explanation of if statement. If the boolean-expression is true, if-code gets executed. If the boolean-expression is false rest_of_code gets executed.





The if-else statements
It is basically extension of if statement. Lets look at the syntax of the if-else statement.



Syntax :

if(boolean-expression)
{
  if-code;
}
else
{
  else-code;
}

rest-of-code;





If the boolean-expression is true if-code gets executed. If boolean-expression is false else-code gets executed. In this decision making statements generally either of the block gets executed whether its if-code or else-code, after either execution rest-code gets executed.





The Nested if-else statements
The Nested if-else statements are used where we want to make series of decision making. Thus one if-else is nested within another if-else. Therefore nested if-else give us to make a decision within a decision already taken. Lets look at the syntax of nested if-else statements


Syntax :

if(boolean-expression 1)
{
  if(boolean-expression 2)
  {
    statement 1;
  }
  else
  {
     statement 2;
  }
}
else
{
  statement 3;
}
rest-of-code;



Lets analyze about syntax and flowchart. Nested if-else syntax is very much. Generally if you look at the flowchart you will find that there are two if-else statements. You can nest them to n number. For the sake of simplicity we have taken only two if-else. Logic implemented in above syntax is like if boolean-expression 1 is false, statement - 3 gets executed and then program flows to rest of code. But if the boolean-expression 1 is true the controls flow to another expression called as boolean-expression 2. Then the same code follows if boolean-expression 2 is evaluated as false statement - 2 gets executed and then to rest of the code. But if boolean-expression 2 is true, statement - 1 is executed and then follows to rest of code.

Else-if Ladder Statements
Below diagram shows else-if ladder statements. Else-if ladder is generally used when we want to take multiple decision. These multiple decision are in chains of if followed by else-if statements till n number. Lets us look at flowchart and syntax associated with else-if ladder.


Syntax : 

if(boolean-expression 1)
{
  statement-1;
}
else if(boolean-expression 2)
     { 
       statement-2;
     }
     else if(boolean-expression n)
       {
         statement-n;
       }       
          else
          {
          default-statement;    
          }

rest-of-code;









 

Lets analyze the syntax and flowchart of Else-if ladder. The multiple decision making starts from top and flows towards bottom. The ladder works very simple and straight way. Whenever true condition is evaluated the statement below if gets executed and rest-of-code execution follows and whenever false condition is evaluated control flows towards next condition till default statement gets executed followed by rest-of-code.

The Switch Statements
We saw how if-else statement worked when making a multiple decision. But this if-else is limited to use in complex scenarios. Java provides a sensational switching technique when making a multi-way decision. This switch statement evaluates an expression and based on the result of expression chooses a path that matches one of the list values. The list values are called as Cases. The expression value is matched against this cases, and which ever case match the control moves to that switch. Lets look at the syntax and flowchart for the switch statement:


switch(expression)
{
  case value-1:
              block-1
              break;
  case value-2:
              block-2
              break;

  default:
             default-block
             break;
}
                
rest-of-code;



Lets look at how switch statement works in Java. Switch statement first starts with a keyword switch followed by an expression. The Java code for expression must be integer or character expression. The value of expression must be an Integral constant. There are various case labels such as value-1, value-2 etc. These case labels must be evaluable to integral constants. When the switch is executed, value of expression is compared with case labels value starting from top and moving towards bottom. If any of case labels that matches with expression, the block of code that follows case gets executed. After block of code gets executed there is a break statement at the end of block code which breaks the switch statement and transfer control to rest-of-code to execute. If break statement is not written the control starts compairing with next case that follows. At the last default is an optional case which is placed when no case matches with expression. If no expression value matches with case labels value than default case is triggered and code associated with it gets executed.



 
© 2021 Learn Java by Examples Template by Hubberspot