0% found this document useful (0 votes)
87 views7 pages

Java Control Flow Statements Overview

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)
87 views7 pages

Java Control Flow Statements Overview

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

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

Common questions

Powered by AI

In Java, decision-making statements include the simple if statement, the if-else statement, the if-else-if ladder, and the nested if-statement. These statements control the program flow based on conditions that evaluate to Boolean values (true or false). A simple if statement allows for execution of a code block when its condition is true. An if-else statement adds an alternate execution path if the condition is false. An if-else-if ladder provides multiple conditions to be evaluated sequentially. Nested if-statements enable conditional branching within other conditional branches, allowing complex decision structures .

Omitting break statements in a Java switch statement leads to 'fall-through,' where execution continues into the subsequent cases regardless of their conditional alignment. This results in unintended logic execution, potentially causing bugs or unexpected behavior. The risk includes executing multiple statements mistakenly and decreased program control flow clarity, making debugging more complex. However, fall-through can be intentionally useful for designing specific patterns like multiple cases executing the same code when properly documented and handled with caution .

Switch statements in Java control program flow by executing code blocks based on the value of a variable. A switch block contains multiple cases, each representing a potential value. When the switch statement is executed, it evaluates the expression and compares it to each case value. If a match is found, the corresponding block of code is executed. The break statement is used to terminate the switch block; if omitted, the execution continues with the next case in sequence. This makes the break statement crucial for ensuring that only the relevant case block executes .

A do-while loop is preferred over a while loop in scenarios where the loop body should always execute at least once, regardless of the condition. The key difference between them is that a do-while loop checks the condition after executing the loop body, ensuring at least one execution, while a while loop checks the condition before executing, potentially bypassing the loop if the condition is false initially .

A simple if-statement executes a block of code only when its condition evaluates to true, providing a straightforward path for conditional execution. Its control flow is single-branched and does not handle alternatives if the condition is false. Conversely, an if-else statement extends this by providing an additional 'else' block that executes when the if condition is false, allowing a dual-path branch for control flow. Simple if-statements are suitable for checks without alternatives, while if-else statements are used where both true and false outcomes require defined actions .

Switch-case structures offer distinct readability and efficiency advantages over if-else-if ladders when dealing with multiple discrete values. They streamline control flow by eliminating repetitive if-else checks, reducing compiler overhead. Each case in the switch deals with specific values without the need for repeated condition evaluations inherent in if-else-if ladders. Additionally, switch-case structures incorporate defaults and explicitly terminate execution with break statements, enhancing clarity and reducing logical error risks associated with nested conditions in ladders .

In Java, loop statements include the for loop, while loop, do-while loop, and enhanced for loop. The for loop is typically used when the number of iterations is known beforehand; it executes by initializing before the loop starts, checking a condition before each iteration, and updating after each iteration. The while loop is used when the number of iterations is not predetermined and depends on a run-time condition; it evaluates the condition before execution of the loop body. The do-while loop is similar to the while loop but checks the condition after executing the loop body, ensuring at least one execution. The enhanced for loop is used for iterating over collections or arrays, simplifying the syntax by handling only the elements' values without having to use an index .

In Java, jump statements such as break and continue significantly affect loop control. The break statement exits the loop prematurely, terminating the current loop's iteration sequence when a certain condition is met. The continue statement, on the other hand, skips the current loop iteration and proceeds with the next one, without exiting the loop entirely. These statements are critical for controlling loop flow, enabling the program to escape or bypass iterations dynamically based on runtime conditions .

The initialization, condition, and update components are crucial in a for loop's structure, dictating the loop's start, continuation, and incrementation. Initialization sets the starting point, establishing a loop counter or defining initial conditions. The condition is evaluated before each iteration; if true, the loop body executes, maintaining control until the condition evaluates to false. The update alters the loop counter after each iteration, eventually leading to the loop's termination by making the condition false. Together, these components ensure accurate and controlled iteration .

Nested if-statements in Java allow for hierarchical checks within a decision-making process, enabling complex decision trees and conditional logic within each block. This can enhance the precision and specificity of a program's response to conditions. However, the drawbacks include increased complexity and reduced readability, as deeply nested statements can become difficult to follow and maintain. They can also lead to performance inefficiencies due to numerous conditional checks required at multiple levels .

You might also like