What is control flow statement ?
- Conditional statements are used to make decisions in a Java program.
- They allow the program to execute different blocks of code based on whether a condition is true or false.
- Control flow statements divided into 3 types.
Types of control flow statement:
- Decision making statement
- Looping statement
- Jumping statement
Decision making statement:
- These statements are used to execute different blocks of code based on conditions.
Types of Decision Making statements:
- if statement
- if-else statement
- else-if statement
- Nested if statement
- Switch statement
If Statement :
- If statement executes a block of code only when the condition is true , otherwise nothing .
Syntax:
if(condition):
{
//block of code
}
Example:
class if_statement
{
public static void main (String[] args)
{
int age = 20;
if(age>=18)
{
System.out.println("Eligible to Vote");
}
}
}
Output:
Eligible to Vote
Here the condition is true , so the if block is executed.
If-Else statement :
- If-else executes one block of code when the condition is true , and executes another block of code when the condition is false.
- If-else is used when we have only two possibilities based on the condition, either true or false.
Syntax:
if(condition)
{
//block of code
}
else
{
//block of code
}
Example:
public class IF_Else {
public static void main(String[] args) {
int age=10;
if(age>=18)
{
System.out.println("Eligible to vote");
}
else
{
System.out.println("Not Eligible to vote");
}
}
}
Output:
Not Eligible to vote
Here the condition is false , so the else block is executed.
Else if Statement :
- Else-if is used when we have multiple conditions to check.
Syntax:
if(condition)
{
// block of code
}
else if (condition)
{
//block of code
}
else if (condition)
{
//block of code
}
else if (condition)
{
//block of code
}
else
{
//block of code
}
Example:
public class Else_if {
public static void main(String[] args) {
int marks=80;
if(marks > 90 && marks <= 100)
{
System.out.println("Grade O");
}
else if(marks > 80 && marks <= 90)
{
System.out.println("Grade A+");
}
else if(marks > 70 && marks <= 80)
{
System.out.println("Grade A");
}
else if(marks > 60 && marks <= 70)
{
System.out.println("Grade B+");
}
else if(marks > 50 && marks <= 60)
{
System.out.println("Grade B");
}
else if(marks > 40 && marks <= 50)
{
System.out.println("Grade C");
}
else
{
System.out.println("Fail");
}
}
}
Output:
Grade A
Here the marks is 80 .
else if(marks > 70 && marks <= 80)
{
System.out.println("Grade A");
}
so this else if block is executed .
Nested If Statement :
A nested if statement is an if statement placed inside another if statement.
It is used when we need to check a condition only after another condition has been satisfied.
Syntax:
if(Condition)
{
if(condition)
{
// block of code
}
else
{
// block of code
}
}
else
{
// block of code
}
Example:
public class LargestNumber {
public static void main(String[] args) {
int a=140 , b=170 , c=115;
if(a>b)
{
if(a>c)
{
System.out.println("A is the Largest Number");
}
else
{
System.out.println("C is the Largest Number");
}
}
else
{
if(b>c)
{
System.out.println("B is the Largest Number");
}
else
{
System.out.println("C is the Largest Number");
}
}
}
}
Output:
B is the Largest Number
Switch Statement:
- Switch is used to select one block of code from multiple options.
Syntax:
switch(option)
{
case 1:
// code
break;
case 2:
// code
break;
case 3:
// code
break;
case 4:
// code
break;
default:
// code
}
Example:
public class Switch {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
Output:
Wednesday
Here the day value is 3 , so case 3 is executed.
Top comments (0)