A program executes from top to bottom except when we use control statements, we can control the order of execution of the program, based on logic and values.
In Java, control statements can be divided into the following three categories:
- Selection Statements
- Iteration Statements
- Jump Statements
Selection Statements
Selection statements allow you to control the flow of program execution on the basis of the outcome of an expression or state of a variable known during runtime.
Selection statements can be divided into the following categories:
- The if and if-else statements
- The if-else statements
- The if-else-if statements
- The switch statements
The if statements
The first contained statement (that can be a block) of an if statement only executes when the specified condition is true. If the condition is false and there is not else keyword then the first contained statement will be skipped and execution continues with the rest of the program. The condition is an expression that returns a boolean value.
Example
- package com.deepak.main;
-
- import java.util.Scanner;
-
- public class IfDemo
- {
- public static void main(String[] args) {
-
- int age;
- Scanner inputDevice = new Scanner(System.in);
- System.out.print("Please enter Age: ");
- age = inputDevice.nextInt();
- if(age > 18)
- System.out.println("above 18 ");
- }
-
- }
Output:
The if-else statements
In if-else statements, if the specified condition in the if statement is false, then the statemet after the else keyword (that can be a block) will execute.
Example
- package com.deepak.main;
-
- import java.util.Scanner;
-
- public class IfElseDemo
- {
- public static void main( String[] args )
- {
- int age;
- Scanner inputDevice = new Scanner( System.in );
- System.out.print( "Please enter Age: " );
- age = inputDevice.nextInt();
- if ( age >= 18 )
- System.out.println( "above 18 " );
- else
- System.out.println( "below 18" );
- }
- }
OutputThe if-else-if statements
This statement following the else keyword can be another if or if-else statement.
That would looks like this:
if(condition)
statements;
else if (condition)
statements;
else if(condition)
statement;
else
statements;Whenever the condition is true, the associated statement will be executed and the remaining conditions will be bypassed. If none of the conditions are true then the else block will execute.
Example
- package com.deepak.main;
-
- import java.util.Scanner;
-
- public class IfElseIfDemo
- {
- public static void main( String[] args )
- {
- int age;
- Scanner inputDevice = new Scanner( System.in );
- System.out.print( "Please enter Age: " );
- age = inputDevice.nextInt();
- if ( age >= 18 && age <=35 )
- System.out.println( "between 18-35 " );
- else if(age >35 && age <=60)
- System.out.println("between 36-60");
- else
- System.out.println( "not matched" );
- }
- }
Output
The Switch Statements
The switch statement is a multi-way branch statement. The switch statement of Java is another selection statement that defines multiple paths of execution of a program. It provides a better alternative than a large series of if-else-if statements.
Example
- package com.deepak.main;
-
- import java.util.Scanner;
-
- public class SwitchDemo
- {
- public static void main( String[] args )
- {
- int age;
- Scanner inputDevice = new Scanner( System.in );
- System.out.print( "Please enter Age: " );
- age = inputDevice.nextInt();
- switch ( age )
- {
- case 18:
- System.out.println( "age 18" );
- break;
- case 19:
- System.out.println( "age 19" );
- break;
- default:
- System.out.println( "not matched" );
- break;
- }
- }
- }
Output
An expression must be of a type of byte, short, int or char. Each of the values specified in the case statement must be of a type compatible with the expression. Duplicate case values are not allowed. The break statement is used inside the switch to terminate a statement sequence. The break statement is optional in the switch statement.
Iteration Statements
Repeating the same code fragment several times until a specified condition is satisfied is called iteration. Iteration statements execute the same set of instructions until a termination condition is met.
Java provides the following loop for iteration statements:
- The while loop
- The for loop
- The do-while loop
- The for each loop
The while loop
It continually executes a statement (that is usually be a block) while a condition is true. The condition must return a boolean value.
Example
- package com.deepak.main;
-
- public class WhileDemo
- {
- public static void main( String[] args )
- {
- int i = 0;
- while ( i < 5 )
- {
- System.out.println( "Value :: " + i );
- i++;
- }
- }
- }
OutputThe do-while loop
The only difference between a while and a do-while loop is that do-while evaluates its expression at the bottom of the loop instead of the top. The do-while loop executes at least one time then it will check the expression prior to the next iteration.
Example
- package com.deepak.main;
-
- public class DoWhileDemo
- {
- public static void main( String[] args )
- {
- int i = 0;
- do
- {
- System.out.println( "value :: " + i );
- i++;
- }
- while ( i < 5);
- }
- }
OutputThe for loop
A for loop executes a statement (that is usually a block) as long as the boolean condition evaluates to true. A for loop is a combination of the three elements initialization statement, boolean expression and increment or decrement statement.
Syntax:
for(<initialization>;<condition>;<increment or decrement statement>){
<block of code>
}The initialization block executes first before the loop starts. It is used to initialize the loop variable.
The condition statement evaluates every time prior to when the statement (that is usually be a block) executes, if the condition is true then only the statement (that is usually a block) will execute.
The increment or decrement statement executes every time after the statement (that is usually a block).
Example
- package com.deepak.main;
-
- public class WhileDemo
- {
- public static void main( String[] args )
- {
- int i = 0;
- while ( i < 5 )
- {
- System.out.println( "Value :: " + i );
- i++;
- }
- }
- }
OutputThe For each loop
This was introduced in Java 5. This loop is basically used to traverse the array or collection elements.
Example
- package com.deepak.main;
-
- public class ForEachDemo
- {
- public static void main( String[] args )
- {
- int[] i =
- { 1, 2, 3, 4, 5 };
- for ( int j : i )
- {
- System.out.println( "value :: " + j );
- }
- }
- }
OutputJump Statements
Jump statements are used to unconditionally transfer the program control to another part of the program.
Java provides the following jump statements:
- break statement
- continue statement
- return statement
Break Statement
The break statement immediately quits the current iteration and goes to the first statement following the loop. Another form of break is used in the switch statement.
The break statement has the following two forms:
- Labeled Break Statement
- Unlabeled Break Statement
Unlabeled Break Statement: This is used to jump program control out of the specific loop on the specific condition.
Example
- package com.deepak.main;
-
- public class UnLabeledBreakDemo
- {
- public static void main( String[] args )
- {
- for ( int var = 0; var < 5; var++ )
- {
- System.out.println( "Var is : " + var );
- if ( var == 3 )
- break;
- }
- }
- }
OutputLabeled Break Statement: This is used for when we want to jump the program control out of nested loops or multiple loops.
Example
- package com.deepak.main;
-
- public class LabeledBreakDemo
- {
- public static void main( String[] args )
- {
- Outer: for ( int var1 = 0; var1 < 5; var1++ )
- {
- for ( int var2 = 1; var2 < 5; var2++ )
- {
- System.out.println( "var1:" + var1 + ", var2:" + var2 );
- if ( var1 == 3 )
- break Outer;
- }
- }
- }
- }
Output
Continue Statement
The continue statement is used when you want to continue running the loop with the next iteration and want to skip the rest of the statements of the body for the current iteration.
The continue statement has the following two forms:
- Labeled Continue Statement
- Unlabeled Continue Statement
Unlabeled Continue Statement: This statement skips the current iteration of the innermost for, while and do-while loop.
Example
- package com.deepak.main;
-
- public class UnlabeledContinueDemo
- {
- public static void main( String[] args )
- {
- for ( int var1 = 0; var1 < 4; var1++ )
- {
- for ( int var2 = 0; var2 < 4; var2++ )
- {
- if ( var2 == 2 )
- continue;
- System.out.println( "var1:" + var1 + ", var2:" + var2 );
- }
- }
- }
- }
Example
Labeled Continue Statement: This statement skips the current iteration of the loop with the specified label.
Example
- package com.deepak.main;
-
- public class LabeledContinueDemo
- {
- public static void main( String[] args )
- {
- Outer: for ( int var1 = 0; var1 < 5; var1++ )
- {
- for ( int var2 = 0; var2 < 5; var2++ )
- {
- if ( var2 == 2 )
- continue Outer;
- System.out.println( "var1:" + var1 + ", var2:" + var2 );
- }
- }
- }
- }
OutputReturn Statement
The return statement is used to immediately quit the current method and return to the calling method. It is mandatory to use a return statement for non-void methods to return a value.
Example
- package com.deepak.main;
-
- public class ReturnDemo
- {
- public static void main( String[] args )
- {
- ReturnDemo returnDemo = new ReturnDemo();
- System.out.println( "No : " + returnDemo.returnCall() );
- }
-
- int returnCall()
- {
- return 5;
- }
- }
Output