Conditional , Control & Unconditional Statements
A program written in Java programming language is generally executed by JVM sequentially
(one by one) in the order in which they appear.These statements are called sequential
statements. The flow of execution takes place from top to bottom.
There are three main types of flow of execution (control) that occur in any computer
programming. They are:
● Sequential: Statements execute from top to bottom one by one.
● Conditional or selection: Out of two instructions, only one will be executed
successfully based on the specified condition. This is because the condition
generates the result as either true or false.
● Repetition or loop: Group of statements repeats whenever the specified
condition is true.
Here are the topics if you want to jump directly:
Conditional Statements
o If statement
o If-else statement
o If-else-if statement
o Nested if statement
o Switch statement
Looping Statements
o For loop
o While loop
o Do-while loop
Unconditional Statements/Jump Statements
o break statement
o continue statement
Control Statements
Control Statements are used to control the execution flow of the
Program.
There are three types of control statements:
● Conditional Control Statements
● Looping Control Statements
● Unconditional Control Statements/Jump Statements
Conditional Statements
● Conditional Statements allows the program to select between the alternatives during
the program execution.
● They are also called decision-making statements or selection statements.
1. If statement
It will go inside the block only if the condition is true otherwise, it will not execute
the block.
Syntax:
if (condition)
// statements (if Block)
//other statements.
Execution Flow Chart of If Statement
Example
class Condif
{ static
System.out.println("Simple If Statement");
public static void main(String args[])
int n=5;//local variable
if(n==5)
{ System.out.println("n=" +n);
System.out.println("Done");
2. If-Else Statement
If the condition is true then, it will execute the If block. Otherwise, it will execute the Else
block.
Syntax:
if (condition)
// statements (if Block)
}
else{
// statements (Else block)
//other statements
Execution flow chart of If-Else Statement
Example
class Condifelse
static
{
System.out.println("If else Statement");
public static void main(String args[])
int n=5;
if(n>4)
System.out.println("n=" +n);
else
System.out.println("Wrong number");
3. If Else-If statement
If the condition is true, then it will execute the If block. Otherwise, it will execute the
Else-If block. Again, if the condition is not met, then it will move to the else block.
Syntax:
if (condition){
// statements (if Block)
}
else if(condition){
// statements (Else-If block)
else{
//statements(Else Block)
}//other statements
Execution flow chart of If Else-If Statement
Example
class Condifelseif
static
{
System.out.println("If else if Statement");
public static void main(String args[])
int a=10,b=15,c=5;
if((a>b)&&(a>c))
System.out.println("a is greater:" +a);
else if((b>c)&&(b>a))
System.out.println("b is greater:" +b);
else
System.out.println("C is greater:" +c);
3. Nested if statement
Example
class Nestedif
static
System.out.println("Nested if Statement");
}
public static void main(String args[])
int a=70,b=45,c=25,d=50;
if(a>d)
System.out.println("a is greater:" +a);
if(b>c) 45>25
System.out.println("b is greater:" +b);
else
System.out.println("C is greater:" +c);
else
System.out.println("D is greater:" +d);
5. Switch Statement
Switch statement allows the program to select one action among multiple actions during the
program execution.
Syntax:
switch(variable/value/expression)
case:
//statements;
break;
case:
//statements;
break;
default:
//statements;
● Based on the argument in the switch statement suitable case value will be selected and
executed.
● If no matching case found, then the default will be executed.
● It is optional to write a break statement at the end of each case statement.
Example
import java.util.*;
class Switchprogram
public static void main(String []args)
System.out.println("Enter the choice from 0-2");
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
switch(0) switch(value/variable/expression)
case 0:
System.out.println("Hai");
break;
case 1:
System.out.println("Hello");
break;
case 2:
System.out.println("Bye");
break;
default:
System.out.println("No choice found");
Looping Control Statements
These are used to execute a block of statements multiple times. It means it executes the same
code multiple times so it saves code. These are also called Iteration statements.
There are three types of looping control statements:
● For loop
● While loop
● Do-while loop
For loop
● It executes the code until condition is false.
● It is used when numbers of iterations are known.
Syntax:
for(initialization; condition; increment/decrement)
//statements(For Body)
Example
public class Forloop
public static void main(String[] args)
int i;
for(i=0;i<=5;i++)
System.out.println("Hello");
}
}
While loop
· While loop executes till the condition becomes false.
Syntax:
while(condition)
// statements
Example
public class Whileloop
public static void main(String[] args)
int i=1;
while(i<5)
System.out.println("While Loop");
i++;
}
Do-while loop
· When you are using for or while, then it will execute the loop body only if the
condition if true.
· In do-while loop, it will execute the loop first, then it checks the condition. So, it
will execute the loop atleast once.
· It is called exit controlled loop while for & while loop are called entry controlled
loop.
Syntax:
do{
//statements
}while(condition);
Example
public class DoWhileloop {
public static void main(String[] args) {
int a=5;
do
{ System.out.println("Hello");
a--;
}while(a!=0);
Unconditional Control Statements/Jump Statements
break Statement
· break is a keyword. It is used within any control statements. It is used to terminate
the execution of the current loop or switch statements.
· Syntax: break;
Example
class Breakprogram
public static void main(String args[])
int i;
for(i=0;i<5;i++)
if(i==3)
break;
System.out.println("Break Statement" +i);
continue Statement
· continue is a keyword. It is used to continue the execution of the current loop with
the next iteration.
Syntax: continue;
Example
class Continueprogram
public static void main(String args[])
for(int i=1;i<=4;i++)
System.out.println("i="+i);
if(i==3)
continue;
System.out.println("continue1");