Control Statements
• Control statements in Java determine the
flow of execution in a program.
• They are categorized into:
• Decision-making statements (if, if-else,
switch)
• Looping statements (for, while, do-while)
• Jump statements (break, continue, return)
Decision Making Statements
• These statements control the execution based on conditions.
• if Statement
• Executes a block of code if a condition is true.
• Ex:
• public class IfExample
• {
• public static void main(String[] args)
• {
• int num = 10;
• if (num > 5)
• {
• [Link]("Number is greater than 5");
• }
• }
• }
• O/P:Number is greater than 5
Decision Making Statements
• if-else Statement
• Executes one block if true, otherwise another block.
• public class IfElseExample
• {
• public static void main(String[] args)
• {
• int num = 10;
• if (num % 2 == 0)
• {
• [Link]("Even Number");
• }
• else
• {
• [Link]("Odd Number");
• }
• }
• } O/P:Even Number
•
Decision
if-else-if Ladder:
Making Statements
• Checks multiple conditions.
• public class IfElseIfExample
• {
• public static void main(String[] args)
• {
• int marks = 85;
• if (marks >= 90)
• {
• [Link]("Grade A");
• }
• else if (marks >= 75)
• {
• [Link]("Grade B");
• } else
• {
• [Link]("Grade C");
• } } } O/P:Grade B
•
Switch
switch Statement:Used when multiple values are checked against a variable.
• Eg:public class SwitchExample
• {
• public static void main(String[] args)
• {
• int day = 2;
• switch (day) {
• case 1:
• [Link]("Monday");
• break;
• case 2:
• [Link]("Tuesday");
• break;
• case 3:
• [Link]("Wednesday");
• break;
• default: [Link]("Invalid Day");
• } }} o/P:Tuesday
Jump
• break statement – Exits a loop or switch
statement.
• for (int i = 1; i <= 5; i++)
• {
• if (i == 3)
• {
• break; // Loop will exit when i = 3 }
[Link](i);
• }
Continue
• continue statement – Skips the current iteration
and moves to the next.
• for (int i = 1; i <= 5; i++)
• {
• if (i == 3)
• {
• continue; // Skips iteration when i = 3
• }
• [Link](i);
• }
Return
• return statement – Exits from a method and returns a value.
• public int add(int a, int b)
• {
• return a + b; // Method returns sum
• }
• Looping Statements:
Looping Statements
• Used when the number of iterations is known.
• For:
• public class ForLoopExample
• {
• public static void main(String[] args)
• {
• for (int i = 1; i <= 5; i++)
• {
• [Link]("Number: " + i);
• }
• }
• }
• o/p: number 1 number 2 number 3 number 4 number 5
while Loop