Control Flow Statements

If Statement

If else Statement

If…else…if statement

Nest if…else Statement

Switch Statement

If Statement

  • An if statement consists of a Boolean expression followed by one or more statements.
  • If the Boolean expression evaluates to true then the block of code inside the if statement will be executed.
  • If not the first set of code after the end of the if statement (after the closing curly brace) will be executed.
if(Boolean expression)
{
//Some code should be work
}

Example Code

public static void main(String[] y)
{
int my_int=25;
if(my_int==25)
{
System.out.println("the condition is true");
}
}
####output####
the condition is true

If else Statement

  • An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
if(Boolean expression)
{
//some code excut
}else
{
//some code excut
}

Example Code

public static void main(String[] y)
{
int my_int=25;
if(my_int<25)
{
System.out.println("the condition is true");
}else
{
System.out.println("the condition is not true");
}
}
####output####
the condition is not true

If…else…if statement

  • An if statement can be followed by an optional else if…else statement, which is very useful to test various conditions using single if…else if statement.
  • When using if , else if , else statements there are few points to keep in mind.
     An if can have zero or one else’s and it must come after any else if’s.
     An if can have zero to many else if’s and they must come before the else.
     Once an else if succeeds, none of the remaining else if’s or else’s will be tested.
if(Boolean Expression 1)
{
//some code
}e
lse if (Boolean Expression 2)
{
//some code
}e
lse if (Boolean Expression 2)
{
//some code
}e
lse
{
//some code
}
Example    p
ublic static void main(String[] y)
{
int my_int=25;
if(my_int<25)
{
System.out.println("the condition is true");
}e
lse if(my_int>25)
{
System.out.println("the else if condition is true for 25
> 25");
}e
lse if(my_int==25)
{
System.out.println("the condition is true for 25==25");
}e
lse
{
System.out.println("any of the condition is not ture");
}
}
####output####
the condition is true for 25==25

Nest if…else Statement

  • It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement.
if(boolean expression 1)
{
if(boolean expression 2)
{
//some code
}else
{
//some code
}
}

Example

public static void main(String[] y)
{
int course_value=90;
String course_name="java";
String Course_type="core & advanced";
if(course_value==90)
{
if(course_name=="java")
{
if(Course_type=="core & advanced")
{
System.out.println("the nested condition is
true");
}
}
}else
{
System.out.println("the nested conditions are not true");
}
}
####output####
the nested condition is true

Switch Statement

  • Switch case statements are a substitute for long if statements that compare a variable to several“integral” values (“integral” values are simply values that can be expressed as an integer, such as the value of a char).
  • The basic format for using switch case is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.
switch(expression)
{
case value1: {//some code}break;
case value2: {//some code}break;
default: {//some code}break;
}

Example

public static void main(String[] y)
{
int x=10;
/*
* x value is compared with case values
*/
switch(x)
{case 10:{System.out.println("the x value is 10");}break;
case 30:{System.out.println("the x value is 30");}break;
case 40:{System.out.println("the x value is 40");}break;
}
}
####output####
the x value is 10