JAVA Control Flow Statements
JAVA Control Flow Statements
IT122-COMPUTER PROGRAMMING 1
SECOND SEMESTER, A.Y 2024-2025
Java Control Statements
JAVA CONTROL STATEMENTS
Java compiler ex executes the code from top to bottom
CFS provides statement that can be used to control the flow.
4 TYPES OF IF-STATEMENTS
1. Simple if statement
2. If-else statement
3. If-else-if ladder
4. Nested if-statement
Republic of the Philippines
BOHOL ISLAND STATE UNIVERSITY
Magsija, 6342, Bohol, Philippines
Office of the College of Computing and Information Sciences
Balance I Integrity I Stewardship I Uprightness
IT122-COMPUTER PROGRAMMING 1
SECOND SEMESTER, A.Y 2024-2025
Java Control Statements
1. SIMPLE IF STATEMENT- the most basic statement among all
control flow statements in Java. Enables the program to enter a
block of code if the expression evaluates to true.
SYNTAX:
If (condition) {
Statement; //executes then the condition is true
}
EXAMPLE:
int x=10, y=12;
if (x+y>20); {
sout(“x+y is greater than 20”);
}
EXAMPLE:
int x=10, y =12;
if(x+y<10){
sout(“x+y is less than 10”);
} else {
sout(“x+y is greater than 12”);
}
Republic of the Philippines
BOHOL ISLAND STATE UNIVERSITY
Magsija, 6342, Bohol, Philippines
Office of the College of Computing and Information Sciences
Balance I Integrity I Stewardship I Uprightness
IT122-COMPUTER PROGRAMMING 1
SECOND SEMESTER, A.Y 2024-2025
Java Control Statements
EXAMPLE:
int num=10;
if(num>20){
sout(“Number is greater than 20”);
} else if(num > 15){
sout(“Number is greater than 15”);
} else if(num >10){
sout(“Number is greater than 10”);
} else {
sout(“The number is 10 or less”);
}
Republic of the Philippines
BOHOL ISLAND STATE UNIVERSITY
Magsija, 6342, Bohol, Philippines
Office of the College of Computing and Information Sciences
Balance I Integrity I Stewardship I Uprightness
IT122-COMPUTER PROGRAMMING 1
SECOND SEMESTER, A.Y 2024-2025
Java Control Statements
EXAMPLE:
if(num > 0){ //outer if
sout(“Number is positive.”);
if(num % 2 == 0){ // inner if
sout(“Number is even”);
} else {
sout(“Number is oddd”);
}
} else {
sout(“Number is non-positive”);
if(num == 0){
sout(“Number is zero”);
} else {
sout(“Number is negative”);
}
Republic of the Philippines
BOHOL ISLAND STATE UNIVERSITY
Magsija, 6342, Bohol, Philippines
Office of the College of Computing and Information Sciences
Balance I Integrity I Stewardship I Uprightness
IT122-COMPUTER PROGRAMMING 1
SECOND SEMESTER, A.Y 2024-2025
Java Control Statements
}
SYNTAX:
switch(expression){
case value1:
statement 1;
break;
case valueN:
statement N;
break;
default:
default statement;
}
EXAMPLE:
int num = 2;
switch(num){
case 0:
sout(“Number is 0”);
break;
case 1:
Republic of the Philippines
BOHOL ISLAND STATE UNIVERSITY
Magsija, 6342, Bohol, Philippines
Office of the College of Computing and Information Sciences
Balance I Integrity I Stewardship I Uprightness
IT122-COMPUTER PROGRAMMING 1
SECOND SEMESTER, A.Y 2024-2025
Java Control Statements
sout(“Number is 1”);
break;
default:
sout(num);
}
2. LOOP STATEMENTS
A. for loop
B. while loop
C. do-while loop
D. enhanced for loop (for-each)
SYNTAX:
for(initialization; condition; update){}
EXAMPLE:
for(int i=0; i <= 5; i++){
sout(“Iteration ” + i);
}
B. while loop – used when the number of iterations is not known and
depends on a condition. The condition is checked before the code block is
executed.
SYNTAX:
while(condition){
statements;
}
Republic of the Philippines
BOHOL ISLAND STATE UNIVERSITY
Magsija, 6342, Bohol, Philippines
Office of the College of Computing and Information Sciences
Balance I Integrity I Stewardship I Uprightness
IT122-COMPUTER PROGRAMMING 1
SECOND SEMESTER, A.Y 2024-2025
Java Control Statements
EXAMPLE:
int i = 1;
while(i<=5){
sout(“Iteration ” + i);
i++; // increment
}
C. do-while loop – similar to while, but the condition is checked after the
loop body. Ensures the code executes at least once.
SYNTAX:
do{
statements;
} while(condition);
EXAMPLE:
int i=1;
do {
sout(“Iteration”+i);
i++;
} while (i<=5);