wellnwill Marathahalli Bangalore

Introduction to Flow Control

The flow control statements are used to run or skip blocks of code when special conditions are met. The flow control describes the execution order of statements.

There are three types of flow control statements:

  • Selection/Conditional Statements a. if statement b. switch statement
  • Looping/iterative Statements a. While statement b. do- while statement c. for statement d. for each
  • Transfer Statements a. break b. continue c. return d. try catch finally

Selection Statements / Conditional Statements

if Statement -->

If result of the boolean expression is true then statements of if block will be executed.

Syntax- if (boolean condition)
              {
                Statements;
              }
                Next statements;


if-else Statements -->

If result of the boolean expression is true then statements of if block will be executed. If result of the boolean expression is false then statements of else block will be executed.

if (boolean condition)
{
     Statements; // this block will be executed if condition is true.
}else
    {
     Statements; // this block will be executed if condition is false.
    }

If else-if Statements -->

Syntax:
if (boolean condition1)
{
   //this block will be executed if condition1 is true.
}
else if(boolean condition2)
{
   //this block will be executed if condition1 is false and condition2 is true.
}
else if(boolean condition3)
{
   //this block will be executed if condition1 and condition2 are false and condition3 is true.
}
.
.
.
else
{
 //this block will be executed if all above mentioned conditions will be false.
}
class Test91
{
   public static void main(String args[])
   {
     If (true)
     {
       System.out.println("condition is true and");
       System.out.println("you are inside if block");
     }
  }
}

// O/P - condition is true and you are inside if block.


class Test92
{
 public static void main(String args[])
 {
   if(!true)
   {
    System.out.println("condition is true and");
    System.out.println("you are inside if block");
   }
   System.out.println("condition is false and");
   System.out.println("you are outside of if block");
 }
}

// O/P - condition is false and you are outside of if block.
class Test93
{
  public static void main(String args[])
  {
    int x=100;
    if(99==99)
    {
      x=10;
      System.out.println("value of x :"+x);
    }
  }
}

// O/P - value of x:10
class Test96
{
  public static void main(String args[])
  {
    if(99==99)
    System.out.println("if without block");
  }
}

// O/P - if without block





-> Curly bracket is optional when if block has only one statement but that statement should not be declarative statement.

Footer with Map