L5-Computer-Programming-2-1
L5-Computer-Programming-2-1
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”);
}
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
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:
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.