Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

L5-Computer-Programming-2-1

Lesson 5 covers control structures in Java, including if, else if, switch-case, while, do-while, and for statements. Each structure is explained with examples demonstrating their usage in Java programs. The lesson also includes guidelines for using these control structures effectively.

Uploaded by

roynal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

L5-Computer-Programming-2-1

Lesson 5 covers control structures in Java, including if, else if, switch-case, while, do-while, and for statements. Each structure is explained with examples demonstrating their usage in Java programs. The lesson also includes guidelines for using these control structures effectively.

Uploaded by

roynal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lesson 5 :

java </
JAV
A
UI
CONTROL >

STRUCTU U
X
RES
Lesson objectives:
apply if and else if
1 statements in a Java
program; apply do-while

apply switch-case
4 statement in a Java
program; and

2 statement in a Java
program;
apply for loop
5 statement in a Java
program.
apply while statement
3 in a Java program;
Selection statement
If statement:

if (condition) {
statement;
}

Example:
int number = 100;
if (number>50){
System.out.println(“The number is greater than
50”);
}

The program will display the line “The number is


greater than 50” if the conditional statement
if(number>50) result is TRUE. If FALSE, the
Selection statement
If – else if Example:
statement: char letter = ‘J’;
if (letter ==‘F’){
if (condition) { System.out.println(“year level = Freshman”);
}else if (letter == ‘S’) {
statement1;
System.out.println(“year level = Sophomore”);
} else if (condition) { }else if (letter == ‘J’) {
statement2; System.out.println(“year level = Junior”);
} else { }else if (letter == ‘G’) {
statement3; System.out.println(“year level = Senior”);
} }
else{
System.out.println(“Not a letter code!”);
}
Selection statement
Switch-case Example:
statement: char letter = ‘J’;
switch (letter){
case ‘F’:
switch (expression) { System.out.println(“year level = Freshman”);
case 1: break;
case ‘S’:
statement 1; System.out.println(“year level = Sophomore”);
break; break;
case 2: case ‘J’:
System.out.println(“year level = Junior”);
statement 2; break;
break; case ‘G’:
case 3: System.out.println(“year level = Senior”);
break;
statement 3; default:
break; System.out.println(“It is not a letter code”);
default: break;
}
statement 4;
break;
Repetition statement
While statement: Output:

while (condition) { Jump 1


Jump 2
statement; Jump 3
} Jump 4
Jump 5
Example:

int times = 1;
while (times<=5){
System.out.println(“Jump! ”
+times );
times++; //times is incremented
by 1
}
Repetition statement
Do-while statement: Output:

do { Jump 1
Jump 2
statement; Jump 3
}while (condition); Jump 4
Jump 5
Example:

int times = 1;
do {
System.out.println(“Jump ”
+times);
times++;
}while (times<=5);
Repetition statement

For statement: Output:

Jump 1
for (init; condition; expression) { Jump 2
statement; Jump 3
} Jump 4
Jump 5

Example:
for (int times = 1; times<=5; times+
+){
System.out.println(“Jump ”+times);
}
In using control structures, we must also remember the
following guidelines:

1. The while statement executes the statement/instruction


while the condition is TRUE.

2. The switch – case statement executes the


statement/instruction of the case that matches with the
switch(expression)

3. The do-while statement executes the statement/instruction


while the condition is TRUE.

4. The for loop statement executes the statement/instruction


while the condition is TRUE.
Sample program 1
Sample program 2
Sample program 3
Sample program 4
Sample program 5

The program will continuously ask for a number until the user
inputs a number greater than 50, then that’s the time the
program will stop.

Note: It will still print Enter a number=80 because do-while


evaluates after it processes a statement.
Sample program 6
TECH notes

• The if statement allows you to control the execution of a statement or


block. If the boolean expression evaluates as true, then the block of
code will be executed.
• The switch-case statement is a selection statement that provides
branching on multiple values of expression.
• The while loop simply repeats the execution of a statement or
compound statement while the condition set expression is true.
• The do-while loop repeatedly executes the statement while the
expression is true but it evaluates the expression after the execution of
the statement.
• The main function of a for loop is to repeat the execution of the
statement while the condition remains true, similar to the while loop
principle.
THANK
S!
Do you have any questions?
addyouremail@freepik.co
m +91 620 421 838
yourcompany.com

CREDITS: This presentation template was


created by Slidesgo, including icons by
Flaticon, and infographics & images by
Freepik
Please keep this slide for attribution

You might also like