Java Control Statements
Java Control Statements
Flow control describes the order in which all the statements will be executed at
runtime.
Java compiler executes the code from top to bottom.
The statements in the code are executed according to the order in which they appear.
However, Java provides statements that can be used to control the flow of Java code.
Such statements are called control flow statements.
Control flow statements, change, or break the flow of execution by implementing
decision making, looping, and branching your program to execute particular blocks of
code based on the conditions.
Ladder if statement
assert
switch Statements
1
KVR 9949939869
1) If Statement:
1. if statement in Java:
Syntax of If Statement:
if(condition) {
block of code // is executed if the condition is true
}
2
KVR 9949939869
Working of if Statement:
Flow Diagram:
Condition True
3
KVR 9949939869
Program:
if (a) {
Program:
if (a=20) {
4
KVR 9949939869
Program:
Program:
5
KVR 9949939869
Program:
Program:
import java.util.Scanner;
Output:
PS D:\JavaWork> java IfExample
Enter Voter age...!
21
6
KVR 9949939869
Eligible to Vote...!
Code after if body...!!
PS D:\JavaWork> java IfExample
Enter Voter age...!
12
Code after if body...!!
if-else Statement:
The Java if-else statement also tests the condition.
It executes the if block if condition is true otherwise else block is
executed.
Syntax:
if(condition) {
//Statement(s); //This block will execute only if the condition is true
}
else {
Statement(s); //This block will execute if the condition is false
}
// Statement(s); // after if-else block will execute
Working of if Statement:
7
KVR 9949939869
Flow Diagram:
Condition
After If-Else
block code
Program:
import java.util.Scanner;
8
KVR 9949939869
Output:
PS D:\JavaWork> javac IfExample.java
17
25
Both else part and curly braces are optional. Without curly braces we can take only one
statement under if, but it should not be declarative statement.
Semicolon(;) is a valid java statement which is call empty statement and it won't produce any
output.
Program:
Program:
9
KVR 9949939869
Program:
int a = 5;
Program:
Program:
10
KVR 9949939869
import java.util.Scanner;
11
KVR 9949939869
Program:
import java.util.Scanner;
12
KVR 9949939869
import java.util.Scanner;
13
KVR 9949939869
The if-else-if ladder statement executes one condition from multiple statements.
Syntax:
if(condition1) {
statement(s); /*This block will execute if condtion1 is true*/
}
else if(condition2) {
statement(s); /* This block will execute if condition1 is not met and
condition2 is true */
}
else if(condition3) {
statement(s);/* This block will execute if condition1 and condition2 is
not met and condition2 is true */
}
.
.
else {
statement(s);// This block will execute if all of the above condition is
false.
}
Flow Diagram:
14
KVR 9949939869
Program:
import java.util.Scanner;
public class Test{
public void findGrade(int marks) {
15
KVR 9949939869
Output:
16
KVR 9949939869
poor
PS D:\JavaWork> java Test
Enter your marks to find grade: 49
Fail
Program:
import java.util.Scanner;
17
KVR 9949939869
Output:
PS D:\JavaWork> javac Test.java
PS D:\JavaWork> java Test
Enter number to find sign: -6
The number is negative.
PS D:\JavaWork> java Test
Enter number to find sign: 9
The number is positive.
Nested if statement
The nested if statement represents the if block within another if block. Here, the inner
if block condition executes only when outer if block condition is true.
Syntax:
if(condition1) {
} StatementFirst(s);
if(condition2) {
StatementSecond(s);
}
Program:
import java.util.Scanner;
18
KVR 9949939869
if(age>=18){
if(weight>50){
System.out.println("You are eligible to donate blood");
}
}
}
public static void main(String args[]){
Test t = new Test();
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.print("Enter your weight: ");
int weight = sc.nextInt();
t.donate(age, weight);
}
}
Output:
Program:
import java.util.Scanner;
19
KVR 9949939869
20