Sei sulla pagina 1di 42

Chapter 8: Control

Statements (Loop)

Session Plan (Week 8):


To understand:
Understand the various concepts of loop control

structure

TMK 3102-Chap 8: Control 1


Statement (Loop)
Introduction (recall)
 Program control specifies the order the
statements are executed in a program
 Program execute statements in
sequence
 How to by pass the sequences
selection statements
 Or How to repeat the sequences
loop statements

TMK 3102-Chap 8: Control 2


Statement (Loop)
Why we need loop? (1)
Consider the Statements:
Write a java program to print the following statement 100 times

“I will do my
homework!!”

TMK 3102-Chap 8: Control 3


Statement (Loop)
Why we need loop? (1)
Consider the Statements:
Write a java program to print the following statement 100 times

public static void main(String[]


“I args){
will do my
System.out.println(“1. I willhomework!!”
do my homework”);
..


System.out.println(“100. I will do my homework”);
}

TMK 3102-Chap 8: Control 4


Statement (Loop)
Why we need loop? (2)
Write a java program for the
following problem: Can you
Read 5 integer and display the identify the
value of their summation. input and
output???
Input : 5 integer
n1, n2, n3, n4, n5
Output: The summation of
n1, n2,n3, n4, n5

Input example: 2 3 4 5 6

Output example: 20
TMK 3102-Chap 8: Control 5
Statement (Loop)
Why we need loop? (2)
start ..
..
Input n1 .
Input n2 n1 = in.nextInt();
n2 = in.nextInt();
Input n3 n3 = in.nextInt);
n4 = in.nextInt();
input n4
n5 = in.nextInt();
input n5 sum = n1+n2+n3+n4+n5
System.out.println(“
sum ← n1+n2+n3+n4+n5 Sum=“+sum);

output sum

end
6
TMK 3102-Chap 8: Control 6
Statement (Loop)
Why we need loop? (2)
start ..
..
Input n1 .
Input n2 n1 = in.nextInt();
n2 = in.nextInt();
Input n3 n3 = in.nextInt);
n4 = in.nextInt();
input n4
n5 = in.nextInt();
input n5 sum = n1+n2+n3+n4+n5
System.out.println(“
sum ← n1+n2+n3+n4+n5 Sum=“+sum);
HOW if number of
output sum input is more than
1000?
end
7
TMK 3102-Chap 8: Control 7
Statement (Loop)
loop statements
Change the program flow by execute a
block of statements repeatedly

 while loop
 for loop
 do.while loop
 Loop Flow Control : break and continue

TMK 3102-Chap 8: Control 8


Statement (Loop)
while Loop
Syntaxes

while (condition)
statement; false
condition

or true

while (condition) { Statement(s)


statement1;
statement2;
statements;
}
TMK 3102-Chap 8: Control 9
Statement (Loop)
while Loop
 Condition is tested first. Loop is controlled by condition
 One of statement must change the condition to
false to stop looping, otherwise infinite loop
 Use conditional operation as statement to
satisfied condition count =0;
while (count < 100){
false System.out.println(“welcome”);
condition count++;
}
true
//example of infinite loop
Statement(s)
count =0;
while (count < 100)
10 System.out.println(“welcome”);
TMK 3102-Chap 8: Control 10
Statement (Loop)
while Loop (counter control)
number of loop is certain
Example:
i=0;
while (i<=3){ Initial value
System.out.print(i+” “);
false
i++; condition
}
true
Statement(s)

TMK 3102-Chap 8: Control 11


Statement (Loop)
while Loop (sentinel control)
number of loop is uncertain
int i=2, input = 0;
while (input != 1){
System.out.print(“i x i=“+i+”x”+i+”=“);
i=i*i;
System.out.println(i);
System.out.print(“press 1 to stop:”);
input = in.nextInt();
}
System.out.println(“Program Finished”);

TMK 3102-Chap 8: Control 12


Statement (Loop)
Self Test
How many times will the following loop
execute?

ctr = 0;
while (ctr == 5){
System.out.printf(“ctr : %d”,ctr);
ctr++;
}

TMK 3102-Chap 8: Control 13


Statement (Loop)
while Loop (case study 1)

Write a java program that lets the user


enter students’ grades. The program
then asked if user still has a grades or
not. When all grades data have been
entered, the program will calculate the
grade average and display it on the
screen.

TMK 3102-Chap 8: Control 14


Statement (Loop)
while Loop (case study 2)
In January 2000, Ibrahim put RM25,000 in
a retirement fund that paying 8.5 %
interest per year. Now, he wants to know
how much money his fund will contain in
year 2020 and the interest made every
year. He like to see this information in a
form of table. Help him by writing a java
program to calculate and display the
related information as requested.

TMK 3102-Chap 8: Control 15


Statement (Loop)
for Loop
 Condition is tested first
 Loop is controlled by a counter
 Syntaxes
for (initial value ; condition; update counter)
statement;

OR
for (initial value ; condition; update counter) {
statement;
statement;
}

TMK 3102-Chap 8: Control 16


Statement (Loop)
for Loop flow
Syntax
for(initial value;
condition; Initial value
increasecounter)
false
Statement(s)
condition
true
Statement(s)

increase counter

TMK 3102-Chap 8: Control 17


Statement (Loop)
for Loop flow
for (i=0; i<3; i++){
System.out.println(“I
will do my homework”);
Initial value
}
false
condition
true
Statement(s)

increase counter

TMK 3102-Chap 8: Control 18


Statement (Loop)
for Loop flow 2

int sum=0;
int val = 0; Initial value
for (i=1; i<=3; i++){
System.out.print(“Value false
“+i +”:”);
condition
val = in.nextInt();
sum=sum+val; true
} Statement(s)
System.out.println(“Sum
:”+ sum);
increase counter

TMK 3102-Chap 8: Control 19


Statement (Loop)
Self Test
What is the value of i after execution:

num =0;
for (i=0;i>10;i++){
num += i;
System.out.println(i + “ “
+ num);
}

TMK 3102-Chap 8: Control 20


Statement (Loop)
Self Test
What is the output of the following java
segment:

num =0;
for (i=9;i>=0;i--) {
num = num + 10 * (i -1)
System.out.print(num + “ “);
}
System.out.println();

TMK 3102-Chap 8: Control 21


Statement (Loop)
Self Test
Write a java program that used a for
statement to produce the Multiplication
Table for any number enter by user. The
multiplication for that number start with
number 1 and end with number 12.

TMK 3102-Chap 8: Control 22


Statement (Loop)
do-while Loop
 Statements in the loop are executed first (at
least once, and condition is tested last
 Loop is controlled by a condition or counter
 Syntax
do {
statement;
statement;
} while (condition);
statement;

TMK 3102-Chap 8: Control 23


Statement (Loop)
do-while Loop Example
int num=0; int sum=0;
num = in.nextInt();
do {
sum = sum + num% 10;
num = num / 10;
} while (num > 0);
System.out.println(“Sum of the digit : ” + sum);

TMK 3102-Chap 8: Control 24


Statement (Loop)
do-while Loop Example
int num=0; int sum=0; num 12867
1286
num = in.nextInt();
do { sum 70
sum = sum + num% 10;
num = num / 10;
} while (num > 0);
System.out.println(“Sum of the digit : ” + sum);

sum = sum + num % 10 num = num /10


sum = 0 + 12867 %10 num = 12867 / 10
sum = 0 + 7 num = 1286
sum = 7
TMK 3102-Chap 8: Control 25
25 Statement (Loop)
do-while Loop Example
int num=0; int sum=0; num 1286
128
num = in.nextInt();
do { sum 7
13
sum = sum + num% 10;
num = num / 10;
} while (num > 0);
System.out.println(“Sum of the digit : ” + sum);

sum = sum + num % 10 num = num /10


sum = 7 + 1286 %10 num = 1286 / 10
sum = 7 + 6 num = 128
sum = 13
TMK 3102-Chap 8: Control 26
26 Statement (Loop)
do-while Loop Example
int num=0; int sum=0; num 128
12
num = in.nextInt();
do { sum 13
21
sum = sum + num% 10;
num = num / 10;
} while (num > 0);
System.out.println(“Sum of the digit : ” + sum);

sum = sum + num % 10 num = num /10


sum = 13 + 128 %10 num = 128 / 10
sum = 13 + 8 num = 12
sum = 21
TMK 3102-Chap 8: Control 27
Statement (Loop)
do-while Loop Example
int num=0; int sum=0; num 01
num = in.nextInt();
do { sum 23
24
sum = sum + num% 10;
num = num / 10;
} while (num > 0);
System.out.println(“Sum of the digit : ” + sum);

sum = sum + num % 10 num = num /10


sum = 23 + 1 %10 num = 1 / 10
sum = 23 + 1 num = 0
sum = 24
TMK 3102-Chap 8: Control 28
28 Statement (Loop)
do-while Loop Example
int num=0; int sum=0; num 01
num = in.nextInt();
do { sum 29
24
sum = sum + num% 10;
num = num / 10;
} while (num > 0);
System.out.println(“Sum of the digit : ” + sum);

Sum of the digit : 24


TMK 3102-Chap 8: Control 29
29 Statement (Loop)
Nested Loop
 The body of a loop is again a loop.
 Describe as “ inner loop nested inside outer
loop”
 A loop within a loop

for(;;){ for(;;){ while(){


for(;;) { while() for(;;) {
} { }
} } }
}

TMK 3102-Chap 8: Control 30


Statement (Loop)
Nested Loop (Example )
 Draw a triangle of “*” by determine the
size of triangle first. Example; if size is 5,
then the triangle is:

*
**
***
****
*****

TMK 3102-Chap 8: Control 31


Statement (Loop)
Nested Loop (Example )
//import section
import java.util.Scanner;
public class NestedLoopExample {
public static void main(String[] args) {
//declaration section
int i,j,input;
Scanner console = new Scanner(System.in);
//input section
System.out.print("What is the size of the triangle:");
input = console.nextInt();
//process section
for(i=1;i<=input;i++){
System.out.println("");
for(j=1;j<=i;j++)
System.out.print("*");
}
}
}
TMK 3102-Chap 8: Control 32
32 Statement (Loop)
Nested Loop (Example details )
//process section
for(i=1;i<=input;i++){ input =3
for(j=1;j<=i;j++)
System.out.print("*");
System.out.println(“ ");

i i <= input j j <= i


*
1 true 1 true **
2 true 2 true false ** *
3 true 3 true false ***
4 false 4 false

TMK 3102-Chap 8: Control 33


33 Statement (Loop)
break Statement
 (Recall) in switch – false
break used to skip next loop
statement
true
 Used to exit early from
a loop. Statement(s)
 Can be placed within if
statement of a loop. false
if..
• If condition is meet,
loop is exited true
immediately. break

TMK 3102-Chap 8: Control 34


Statement (Loop)
break example
Add the integers from 1 to 20 in the order to
sum until sum is equal or greater than 8.
public class BreakExample {
public static void main(String[] args){
int sum =0;
int num;
for(num =1;num < 20;num++) {
sum+=num;
if (sum >= 8)
break;
}
System.out.println(“Finish”);
}
}
TMK 3102-Chap 8: Control 35
Statement (Loop)
continue Statements
 Used in while, for, and do...while
structures.
 When executed in a loop, the remaining
statements in the loop are skipped; proceeds
with the next iteration of the loop.
 When executed in a while/do…while
structure, expression is evaluated immediately
after continue statement.
 In a for structure, the update statement is
executed after the continue statement; the
loop condition then executes.

TMK 3102-Chap 8: Control 36


Statement (Loop)
continue example

Add all integers from 1 to 10 except the


number that can divide by 3.

TMK 3102-Chap 8: Control 37


37 Statement (Loop)
continue example

Add all integers from 1 to 10 except the


number that can divide by 3.
public class ContinueExample {
public static void main(String[] args){
int sum =0;
int num;
for(num =1;num <=10;num++) {
if((num % 3 ) == 0)
continue;
sum+=num;
}
System.out.println(“Finish”);
}
}

TMK 3102-Chap 8: Control 38


38 Statement (Loop)
continue example

Add all integers from 1 to 10 except the


number that can divide by 3.
public class ContinueExample { sum 7310
12
19
27
37
public static void main(String[] args){
int sum =0;
int num;
num 918567432? 0
11
true
false
for(num =1;num <=10;num++) {
if((num % 3 ) == 0) true
false
continue;
sum+=num;
}
System.out.println(“Finish”);
} Finish
}

TMK 3102-Chap 8: Control 39


39 Statement (Loop)
label statement
label refer to some location where loop
statement exist in java program by the
special name.

whileloop: // label
while( condition) {
forloop: //label
for(i=0;i<10;i++)

}
TMK 3102-Chap 8: Control 40
40 Statement (Loop)
continue and break with label
import java.util.Scanner;
public class labelexample {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
whileloop:
while (true){
forloop:
for(int i = 0; i < 100; i++) {
int c = console.nextInt();
if (c == 1)
continue whileloop;
else if ( c == 2)
break forloop;
else
System.out.println(c);
}
System.out.println(">");
}
}
} TMK 3102-Chap 8: Control 41
Statement (Loop)
Thank You

TMK 3102-Chap 8: Control 42


Statement (Loop)

Potrebbero piacerti anche