Java Decision Making Statements

Using if-else

    if else loop is use for condition check. The if-then statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true.


  • It is a two-way decision making statement, which is used along with the construction of an expression.
  • It is of Four types :

    • Simple if
      • Syntax :
        if(condition)
        {
        	statement;
        }
        statement;
        
    • if ... else
      • Syntax :
        if(condition)
        {
        	statement;
        }
        else
        {						
        	statement;
        }
        
    • Nested if
      • Syntax :
        if(condition)
        {
        	if(condition)
        	{
        		statement;
        	}
        	else
        	{
        		statement;
        	}
        }
        else
        {
        	statement;
        }
          
    • else ... if ladder
      • Syntax :
        if(condition)
        {
           statement;
        }
        else if(condition)
        {
           statement;
        }
        else
        {
           statement;
        }
        statement;    
         

Using switch

switch case is used to check a number of possible execution paths. A switch can work with the byte, short, char and int primitive data types.


  • Java has a built in multi-way design statement called switch
  • The expression inside the switch case may be an integer or a character.
  • Since JDK 1.7 we have facility to take an String inside the switch case.
  • Each case must be end with colon (:)
  • The default statement is executed if there is no match with any case specified in the switch case.
  • Each case must be terminated with a break; statement.
    • Syntax :
      switch(expression
      {
      	case value1: statement();
      	break;
      	case value2: statement();
      	break;
      	case value3: statement();
      	break;
      	default:  statement();
      	break;
      }

    Loop

    for loop allows a code to be repeatedly executed. A for loop is classified as an iteration statement.
    Difference between while and do while is that while statement continually executes a block of statements while a particular condition is true whereas do while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.


    • When you want to repeat something for definite number of times, it is called loop.
    • Java has three basic loops

      • for()
        • More than one variable can initialize at a time in this loop statement.
        • The increment section should have also one part.
        • Syntax :
          for(initialization; condition;increment/decrement)
          {
          	loop body;
          }
          
      • while()
        • Here when the test condition fails, control is transferred out of the loop, otherwise the loop will continue or repeat itself.
        • Here the body of the loop may not be executed at all, if the condition is not satisfied for the first time.
        • Syntax :
          initialization;
          while(condition)
          {
          	loop body;
          }
          
      • do-while()
        • Since the test condition is provided at the bottom of the loop, it is always executed at least once.
        • This loop will be executed till the condition is true.
        • Syntax :
          initialization;
          do
          {
          	body of loop;
          }while(condition);
          

      • Enhance for loop

      • Since JDK 1.5 (tiger) java introduce one more loop called Enhance for loop. Enhanced for loop allows you to iterate through a collection without having to create an Iterator or without having to calculate beginning and end conditions for a counter variable.

        • The enhanced for loop was created as a shortcut for when you don't need the index of the element. It streamlined things.
        int a[]= {10,20, 30, 40, 50};
        for(int x: a)
        {
        	System.out.println(x);
        }