Flow Control Statements
Flow Control Statements
STATEMENTS
3
Flow control Statements
Control statements in Java are instructions that manage
the flow of execution of a program based on certain
conditions.
They are used to make decisions, to loop through blocks of
code multiple times, and to jump to a different part of the
code based on certain conditions.
Control statements are fundamental to any programming
language, including Java, as they enable the creation of
dynamic and responsive programs.
Types of Control Statements in
Java
• Decision-Making Statements
• (if, if-else, switch).
• Looping Statements
• (for, while , do-while)
• Jump Statements
• (break, continue, and return.)
Take User Input Using Scanner Class
The Scanner class in Java is used to accept input from the user through
the keyboard. It belongs to the java.util package and provides various
methods to read different types of data.
Steps to Take Input
Import the Scanner Class
import java.util.Scanner;
Create a Scanner Object
Scanner sc = new Scanner(System.in);
Use Scanner Methods to Take Input
nextInt(), nextDouble(), nextLine() to read different types
of input.
Close the Scanner Object
sc.close(); (To free system resources)
Example:
import java.util.Scanner;
// Simple if statement
if (number > 0) {
System.out.println("The number is positive.");
}
}
}
Output: The number is positive.
'if-else' Statement in Java
if (condition) {
// True Statement Block
} else {
//False Statement Block
}
Example for If-else:
public class IfElseEx {
public static void main(String[] args) {
int number = -5;
// If-else statement
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is non-positive.");
}
}
}
if-else-if ladder
The if-else if ladder in Java is a decision-making structure used when
multiple conditions need to be evaluated sequentially.
If-else if ladder comes with multiple conditions each condition is associated
with separate task.
In if else if ladder we can take N number of conditions.
It must be started with if statement only.
JVM check weather the given condition are true or not. If any one is true
that TSB will be execute then JVM will go to next line.
Syntax: if (condition1) {
// Executes TSB 1, if condition1 is true
} else if (condition2) {
// Executes TSB 2 if condition1 is false and condition2 is
true
} else if (condition3) {
// Executes if TSB1 and TSB2 conditions are false, but
TSB3 is true
} else {
// Executes FSB if none of the above conditions are true
Example:
public class GradeExample {
public static void main(String[] args) {
int marks = 75;
switch (expression) {
case value1:
// Executes if expression == value1
break;
case value2:
// Executes if expression == value2
break;
default:
// Executes if no case matches
}
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.");
}
}