02 ProgramControlStatements
02 ProgramControlStatements
JAVA PROGRAMMING
Plan 2
1
12/10/2022
Java Programming
Example 4
1. // Read a character from the keyboard.
2. public class KbIn {
3. public static void main(String[] args)
4. throws java.io.IOException{
5. char ch;
6.
Java Programming
2
12/10/2022
if statement [1] 5
if(condition)
statement;
else
statement;
Java Programming
if statement [2] 6
1. if(condition)
2. {
3. statement sequence
4. }
5. else
6. {
7. statement sequence
8. }
Java Programming
3
12/10/2022
Java Programming
13.
Java Programming
14.
4
12/10/2022
Nested ifs 9
Java Programming
10
5
12/10/2022
9.
.
10.
.
11. else
12. statement;
Java Programming
11
12
6
12/10/2022
13.
default:
14. statement sequence
Java Programming
15. }
13
14.
System.out.println(i + " is four"); break;
default:
15.
System.out.println(i + " is five or more");
16. }
17. }
18. } Java Programming
14
7
12/10/2022
15
16
1. switch(i) {
2. case 1:
3. case 2:
4. case 3: System.out.println("i is 1, 2 or 3");
5. break;
6. case 4: System.out.println("i is 4");
7. break;
8. }
Java Programming
16
8
12/10/2022
switch(ch1) {
case 'A’:
System.out.println("This A is part of outer switch.");
switch(ch2) {
case 'A’:
System.out.println("This A is part of inner
switch");
break;
case 'B': // ...
}// end of inner switch
case 'B': //...
}
}
Java Programming
17
Java Programming
18
9
12/10/2022
Result 19
Java Programming
19
• General form:
for(initialization; condition; iteration) statement;
Java Programming
20
10
12/10/2022
Example 21
1. // Show quare roots of 1 to 99 and the rounding error.
2. public class SqrRoot {
3. public static void main(String[] args) {
4. double num, sroot, rerr;
5. for(num = 1.0; num < 100.0; num++) {
6. sroot = Math.sqrt(num);
7. System.out.println("Square root of " + num +
8. " is " + sroot);
9. rerr = num - (sroot * sroot);
10. System.out.println("Rounding error is " + rerr);
11. System.out.println();
12. }
13. }
14. }
Java Programming
21
22
1. // A negatively running for loop.
2. public class DecrFor {
3. public static void main(String[] args) {
4. int x;
5. for(x = 100; x > -100; x -=5)
6. System.out.println(x);
7. }
8. }
9.
Java Programming
22
11
12/10/2022
Java Programming
23
24
1. //Loop until an S is typed.
2. public class ForTest {
3. public static void main(String[] args)
4. throws java.io.IOException{
5. int i;
6. System.out.println("Press S to stop.");
7. for(i = 0; (char)System.in.read() != 'S'; i++)
8. System.out.println("Pass #" + i);
9. }
10. }
Java Programming
24
12
12/10/2022
Java Programming
25
Java Programming
26
13
12/10/2022
Java Programming
27
Java Programming
28
14
12/10/2022
Java Programming
29
Plan 30
30
15
12/10/2022
Syntax 31
while(condition) statement;
• statement may be a single statement or a block of
statements
• condition defines the condition that controls the loop.
• The condition may be any valid Boolean expression.
• The loop repeats while the condition is true.
• When the condition becomes false, program control passes to
the line immediately following the loop.
Java Programming
31
Example 32
1. // Demonstrate the while loop
2. public class WhileDemo {
3. public static void main(String[] args) {
4. char ch;
5. // print the alphabet using a while loop
6. ch = 'a';
7. while (ch <= 'z') {
8. System.out.print(ch);
9. ch++;
10. }
11. }
12. }
Java Programming
32
16
12/10/2022
Exercise: 33
Java Programming
33
Plan 34
34
17
12/10/2022
Syntax 35
do{
statements;
}while (condition);
Java Programming
35
Example 36
1. // Demonstrate the do-while loop
2. public class DWDemo {
3. public static void main(String[] args)
4. throws java.io.IOException{
5. char ch;
6. do {
7. System.out.print("Press a key followed by ENTER: ");
8. ch = (char) System.in.read(); // get a char
9. }while(ch !='q’);
10. }
11. }
Java Programming
36
18
12/10/2022
37
Java Programming
38
19
12/10/2022
Plan 39
39
break statement 40
Java Programming
40
20
12/10/2022
Example 41
1. //Using break to exit a loop.
2. public class BreakDemo {
3. public static void main(String[] args) {
4. int num = 100;
5. //loop while i-square is less than num
6. for(int i = 0; i < num; i++) {
7. // terminate loop if i*i >= 100;
8. if (i*i >= num) break;
9. System.out.print(i + " ");
10. }
11. System.out.println("Loop complete.");
12. }
13. }
Java Programming
41
42
1. // Read input until a q is received.
2. public class Break2 {
3. public static void main(String[] args)
4. throws java.io.IOException{
5. char ch;
6. for(;;) {
7. ch = (char)System.in.read(); // get a char
8. if(ch == 'q') break;
9. }
10. System.out.println("Your pressed q!");
11. }
12. }
Java Programming
42
21
12/10/2022
43
Plan 44
44
22
12/10/2022
Syntax 45
break label;
• label is the name of a label that identifies a block of
code.
Java Programming
45
46
23
12/10/2022
47
1. // This program contains an error.
2. public class BreakErr {
3. public static void main(String[] args) {
4. one: for(int i = 0; i < 3; i++) {
5. System.out.print("Pass " + i + ": ");
6. }
7. for(int j =0; j < 100; j++) {
8. if(j == 10) break one; // WRONG
9. System.out.print(j + " ");
10. }
11. }
12. }
Java Programming
47
Plan 48
48
24
12/10/2022
continue statement 49
Java Programming
49
Example 50
1. // Use continue.
2. public class ContDemo {
3. public static void main(String[] args) {
4. int i;
5. // print even numbers between 0 and 100
6. for(i = 0; i <= 100; i++) {
7. if ((i%2) != 0) continue; // iterate
8. System.out.println(i);
9. }
10. }
11. }
Java Programming
50
25
12/10/2022
1.
2.
// Use continue with a label.
public class ContToLabel {
51
3. public static void main(String[] args) {
4.
outerloop:
for(int i = 1; i < 10; i++) {
5.
System.out.print("\nOuter loop pass " + i +
6.
", Inner loop: ");;
7.
for(int j = 1; j < 10; j++) {
8.
// continue outer loop
9.
if(j == 5) continue outerloop;
10. System.out.print(j);
11. }
12. }
13. }
14. }
15.
Java Programming
51
Plan 52
52
26
12/10/2022
53
Java Programming
53
Example 54
1. /* Use nested loops to find factors of numbers
2. * between 2 and 100.
3. */
4. public class FindFac {
5. public static void main(String[] args) {
6. for(int i = 2; i <= 100; i++) {
7. System.out.print("Factors of " + i + ": ");
8. for(int j = 2; j < i; j++)
9. if((i%j) == 0) System.out.print(j + " ");
10. System.out.println();
11. }
12. }
13. }
Java Programming
54
27
12/10/2022
55
QUESTION ?
Java Programming
55
28