Control Flow Statement In Java

Introduction To JAVA

Control Flow Statement In Java

 

  1. It is use to control the execution flow of the program. (Control Flow Statement)
  2. There are three categories of control flow statement.
    1. Sequential statement execution
      1. By default each program executes in sequence.
    2. Conditional statement
      1. Can decide whether to execute specific line or block of line.
      2. To achieve conditional statement can use If and if variation, switch cases.
    3. Looping Statement
      1. Can execute line or block of line multiple times.
      2. To achieve looping statement can use for, while or do-while loop

If Statement

Syntax for if :

if(Boolean Expression/Condition) {
   Statement(s)
}

 

 

Syntax for if-else

If(Boolean Expression/Condition) {
    Statement(s)
} else {
    Statement(s)
}

IMP Point: else cannot be come without if.

 

Example :-

public class IfDemo
{
	public static void main(String args[])
	{
		int age = 23;
		if(age >= 1 && age <=110)
		{
		System.out.println("valid age is " + age);
		}
	 	else
		{
		System.out.println("Invalid age is " + age);
		}
	}
}

 

 

Syntax for else-if

if(Boolean Expression/Condition) {

   Statement(s)

} else if(Boolean Expression/Condition) {

   Statement(s)

} else if(Boolean Expression/Condition) {

   Statement(s)

} else {

   Statement(s)

}

IMP point: If any of the condition is become true then it will not check for other condition

 

Syntax for Nested-if

if(Boolean Expression/Condition) {

   if(Boolean Expression/Condition) {

       Statement(s)

   }

} else {

   if(Boolean Expression/Condition) {

        Statement(s)

   }

}

 

Example :-

/* percent = 30
  destingtion class 75-100
  first class 60-74
  second class 50-59
  pass class 35-49
  fail 0-34
*/
public class ElseIfDemo
{
	public static void main(String args[])
	{
		int percent = 74;
		
		if(percent >= 75 && percent <=100) {
                        System.out.println("You got Distingtion"); 
                } else if(percent >= 60 && percent <=74) { 
                        System.out.println("You got First Class"); 
                } else if(percent >= 50 && percent <=59) { 
                        System.out.println("You got Second Class"); 
                } else if(percent >= 35 && percent <=49){
			System.out.println("You got Pass Class");
		}
		else
		{
			System.out.println("You are Fail");
		}
	}
}

 

Switch cases

Syntax for switch

switch(value) {

        case label/value 1:

               Statement(s)

               break;

        case label/value 2:

               Statement(s)

               break;

        case label/value 3:
 
               Statement(s)

               break;

        default:

               Statement(s)

}

 

 

Switch-Case Rules

  1. Case label must be of same data type of switch value.
  2. Case label must be unique.
  3. Break is not mandatory in cases, but it is required to get logically correct output.
  4. Switch case value must of byte, short, int, char, enum, String(Jdk 1.7) data type only, other data type is not allowed.
  5. Default case is not mandatory.
  6. If multiple cases have same execution then, you can combine a multiple cases.

 

Example :-

public class SwitchDemo
{
	public static void main(String args[])
	{
		int day = 5;
		switch(day)
		{
			case 1:
				System.out.println("Mon");
				break;
			case 2:
				System.out.println("Tuse");
				break;
			case 3:
				System.out.println("Wen");
				break;
			case 4:
				System.out.println("Thus");
				break;
			case 5:
				System.out.println("Fri");
				break;
			case 6:
				System.out.println("Sat");
				break;
			case 7:
				System.out.println("Sun");
				break;
		}
	}
}

 

 

While loop

Syntax:

Declaration and initialization of variable

while(condition/Boolean Expression) {

      Statement(s);

      Increment/Decrement;

}

 

While loop is also known as pre condition check.

Loop will not execute until condition become true.

Do-While loop

            Syntax:

Declaration and initialization of variable

do {

     Statement(s);

     Increment/Decrement;

} while(condition/Boolean Expression);

 

Do-while loop is also known as post condition check.

Do-while loop executes at least once even if condition is false.

 

Example :-

public class DoWhileDemo
{
	public static void main(String args[])
	{
		int count = 1;
		do
		{
			System.out.println("Good Morning");
			count++; // count = count + 1;
		}
		while(count <= 5);
		
	}
}
class DoWhile
{
	public static void main(String args[])
	{
		int a = 1;
		do
		{
			System.out.println("Good Night");
			a++;
		}
		while(a <= 5);
	}
}

 

For Loop

Syntax :

for(Declaration & initialization ; Conditions/Boolean Expression ; increment/decrement/statements) {

        Statement(s)

}

 

Example :-

public class ForLoopDemo
{
	public static void main(String args[])
	{
		int num = 5;
		for(int i = 1; i <= 10; i++)
		{
			System.out.println(num*i);
		}
	}
}

 

 

Tips and Tricks

for(;;) {

     Statement(s)

}

 

Above For Loop is a valid for loop and it is consider as true and execute infinite times

 

for (int i = 1 ; i <= 10 ; System.out.println(" Hello ") , i++) {

 

}

 

Above example statements are written in the last part of the for loop, can write multiple statements also, but it must be separate by comma (,)

 

Nested For Loop

The loop inside another loop is called as nested loop.

 

for ( ; ; ) {  // outer for loop - always for row

        for ( ; ; ) {  // inner for loop - always for columns



       }

}

 

Example :-

/*
	1 2 3 4 5
	1 2 3 4 5
	1 2 3 4 5
i. e. r=3, c=5.
*/

public class NestedForDemo
{
	public static void main(String args[])
	{
		for(int r=1;r<=3;r++)
		{
			for(int c=1;c<=5;c++)
			{
			System.out.print(c + " ");
			}
		System.out.println();
		}
	}
}

 

 

Join Telegram : Click Here

 

All Full Stack Java Study Material

 

 

Job For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Us !