Download presentation
Presentation is loading. Please wait.
1
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
2.5 Write program using control structure
2
Learning Outcome Subtopic 2.5
Explain selection statements in Java programs: Simple if, else statements Complex if, else statements The switch statements 1 Explain looping statements in Java programs. The for loops The do/while loop 2 Apply break, continue, break statements with labels and continues statements with labels in Java programming 3
3
Simple If, else statement
if (expression that evaluates to a boolean result) { // Perform the enclosed statements if true ... } else { // Perform the enclosed statements if not true Example What is the value of grade when mark is 10? if (mark > 25) grade = ‘A’; else grade = ‘B’; The answer is ‘B’ because the condition is false, thus the else part is executed.
4
Complex If, else statement
The inner selection structure is placed within the body of the outer if or else part.
5
Cont.. Example What is the final value of number? int number = 2;
if (number < 0) number = number+0; else { if (number == 1) number = number+1; number = number+2; } The answer is 4. For the outer selection structure, the else part is executed. When executing the inner if-else part, the condition is false, thus the else part is executed.
6
Switch statement A switch statement is used for any expressions that reduce to an integral value (char or int). It uses case labels, and for each possible value, a separate case label is required. The default label will be executed when none of the listed cases is matched. It is not compulsory to put a default label, as the compiler will continue with the next statement after the closing brace of switch.
7
Cont.. Flow Chart for Switch Selection Structure
8
Cont.. Example What is the final value of number?
int number=4; switch (number) { case 2: number = number +10; break; case 4: number = number +5; break; case 6: number = number +7; break; default: number = 0; } The answer is 9. A compiler will start at case labeled with 4 since the integral-value is matched with the tested expression. The accumulating statement will add up 5 to number, then ends with the keyword break.
9
Cont.. Example What is the final value of number? int number=4; switch (number) { case 2: number = number +10; break; case 4: number = number +5; case 6: number = number +7; break; default: number = 0; } At case 4, number will be added by 5 which makes it 9, then the execution continues with case 6 to add 7 to number. So the final value becomes 16. A break statement is a must if we want to execute a particular case label only. If we do not put a break statement for each case label, the compiler will start executing at the matched case label and will continue with the remaining cases too.
10
The for loops The for loop:
for ( <init_expr>; <test_expr>; <alter_expr> ) <statement_or_block> Example: for ( int i = 0; i < 10; i++ ) System.out.println(i + " squared is " + (i*i)); or (recommended): for ( int i = 0; i < 10; i++ ) { }
11
The do/while loops The do/while loop: do <statement_or_block>
while ( <test_expr> ); Example: int i = 0; do { System.out.println(i + " squared is " + (i*i)); i++; } while ( i < 10 );
12
The break statement do { statement; if ( condition ) { break; }
} while ( test_expr );
13
The continue statement
do { statement; if ( condition ) { continue; } } while ( test_expr );
14
Using break statements with label
outer: do { statement1; statement2; if ( condition ) { break outer; } statement3; } while ( test_expr ); statement4;
15
Using continue statements with label
test: do { statement1; statement2; if ( condition ) { continue test; } statement3; } while ( test_expr ); statement4;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.