0% found this document useful (0 votes)
2 views20 pages

Flow Control Statements

The document provides an overview of flow control statements in Java, which manage the execution flow of a program based on conditions. It details various types of control statements, including decision-making, looping, and jump statements, along with examples of how to use them. Additionally, it explains how to take user input using the Scanner class and provides examples of if, if-else, and switch statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views20 pages

Flow Control Statements

The document provides an overview of flow control statements in Java, which manage the execution flow of a program based on conditions. It details various types of control statements, including decision-making, looping, and jump statements, along with examples of how to use them. Additionally, it explains how to take user input using the Scanner class and provides examples of if, if-else, and switch statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

FLOW CONTROL

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;

public class UserInputExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Create Scanner object

System.out.print("Enter a number: ");


int number = sc.nextInt(); // Read integer input

System.out.println("You entered: " + number);

sc.close(); // Close Scanner


}
}
Decision-Making Statements

 Decision-making statements in Java are constructs used to


control the flow of execution based on certain conditions.
 They allow a program to execute different parts of code depending on
whether a condition (or set of conditions) is true or false.
 The decision-making statements in Java are primarily of 3 types,
namely: if, if-else and switch statements.
1. Simple If
2. If-else
3. If- else ladder
4. Nested if-else
5. switch
'if' Statement in Java
 The if statement in Java evaluates a boolean condition. If the
condition is true, the block of code inside the if statement is
executed.
 If comes with a condition.
 The condition is associated with the statement block.
 True statement block will execute only when the given condition is
true.
 If the given condition is false JVM skips the TSB and it will go to next
line.
Syntax:
if(condition){ if(condition)
//instructions; {
}
}
Example for If:
public class SimpleIfExample {
public static void main(String[] args) {
int number = 10;

// Simple if statement
if (number > 0) {
System.out.println("The number is positive.");
}
}
}
Output: The number is positive.
'if-else' Statement in Java

 The if-else statement in Java is used for multiple conditions.


 It comes after an if statement and before an else statement.
 If comes with condition and condition is associated with true
statement block.
 Else by default comes with false statement block.
 True Statement Block will execute only when the condition is true.
 False Statement Block will execute when if condition is false.
 Only one block will be executed once.
 Syntax:

 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;

if (marks >= 90) {


System.out.println("Grade: A");
} else if (marks >= 80) {
System.out.println("Grade: B");
} else if (marks >= 70) {
System.out.println("Grade: C");
} else if (marks >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
}
}
nested if-else
 A nested if-else statement in Java is when an if-else block is placed inside
another if-else block.
 This allows multi-level decision-making where different conditions are
evaluated in stages.
 The outer if condition is checked first.
 If true, the inner if-else condition executes.
 If false, the else block of the outer condition executes.
 This structure helps in complex logical evaluations.
 Syntax:
if (condition1) {
if (condition2) {
// Executes if both condition1 and condition2 are true
} else {
// Executes if condition1 is true but condition2 is false
}
} else {
// Executes if condition1 is false
}
Example:
public class NestedIfElseExample {
public static void main(String[] args) {
int yearsOfExperience = 6;
double performanceRating = 4.5;

if (yearsOfExperience >= 5) { // Outer if condition


if (performanceRating >= 4.0) { // Inner if condition
System.out.println("Eligible for high bonus.");
} else {
System.out.println("Eligible for standard bonus.");
}
} else {
System.out.println("Not eligible for bonus.");
}
}
}
switch Statement in Java

 The switch statement in Java allows for the


selection of a block of code to be executed based on
the value of a variable or expression. It is an
alternative to a series of if statements and is often
more concise and easier to read.
 The switch statement evaluates an expression
(typically an integer, character, string, or enum).
 It compares the expression against different case
labels.(==)
 If a match is found, the corresponding block of code
executes.
 The break statement ensures that execution exits
the switch block after the matching case runs.
Syntax:

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.");
}
}

You might also like