0% found this document useful (0 votes)
2 views

JAVA Control Flow Statements

The document outlines Java control statements as part of the IT122 Computer Programming 1 course at Bohol Island State University for the academic year 2024-2025. It details three types of control flow: decision-making statements (including various if statements and switch statements), loop statements (for, while, do-while), and jump statements (break and continue). Each section includes syntax and examples to illustrate the concepts.

Uploaded by

Winssis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JAVA Control Flow Statements

The document outlines Java control statements as part of the IT122 Computer Programming 1 course at Bohol Island State University for the academic year 2024-2025. It details three types of control flow: decision-making statements (including various if statements and switch statements), loop statements (for, while, do-while), and jump statements (break and continue). Each section includes syntax and examples to illustrate the concepts.

Uploaded by

Winssis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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
JAVA CONTROL STATEMENTS
 Java compiler ex executes the code from top to bottom
 CFS provides statement that can be used to control the flow.

3 TYPES OF CONTROL FLOW


1. Decision Making Statements
 If Statements
 Switch Statements
2. Loop Statements
 For loop
 While loop
 Do while loop
3. Jump Statements
 Break Statements
 Continue Statements

1. DECISION MAKING STATEMENTS


 Decide which statements to execute and when
 Control the program flow depending upon the result of the condition

 IF STATEMENTS- used to execute condition, the condition of the if


statements give a Boolean value either true or false.

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”);
}

2. IF-ELSE STATEMENT-extension to the if-statement, which uses


another block of code (else block). Else block is executed if the
condition of the if-block is executed as false.
SYNTAX:
If (condition) {
Statement; //executes then the condition is true
} else {
Statement; //executes when condition is false
}

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

3. IF-ELSE-IF LADDER – contains the if statement followed by multiple


else-if statements. Chain of if-else statements that create a decision
tree where the program enter condition is true.
SYNTAX:
if(condition 1){
statement 1; // executes if condition 1 is true
} else if (condition 2) {
Statement 2; // executes if condition 2 is true
} else {
else statement; // executes when all condition is false;
}

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

4. NESTED IF-STATEMENT – the if statement can contain an if or if-


else statement inside another if or else-of statement.
SYNTAX:
if(condition) {
statement 1; // executes when condition 1 is true
if(condition 2){
statement 2; // executes when condition 2 is true
} else {
statement 3; // executes when condition 2 is false
}
}

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
}

 SWITCH STATEMENTS – similar to if-else statements. Contains multiple


blocks of code called cases and a single case is executed based on the variable
WC is being switched.
– default statement is executed when any of the case
doesn’t match the value of expression. It is optional.
– break statement terminates the switch block when
the condition is satisfied. It is optional, if not used,
next case is executed.

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)

A. for loop – use when the number of iteration is known beforehand.


- 3 parts:
o Initialization
o condition
o update

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

You might also like