Upload
mamat88
View
215
Download
0
Embed Size (px)
Citation preview
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
1/42
TMK 3102-Chap 8: Control
Statement Loo
1
Chapt er 8: Contro l St a t em ent s (Loop)
Session Plan (Week 8):To understand:
Understand the various concepts of loop control
structure
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
2/42
TMK 3102-Chap 8: ControlStatement Loo
2
In t roduct ion (rec a l l ) Program control specifies the order the
statements are executed in a program
Program execute statements insequence
How to by pass the sequencesselection statements
Or How to repeat the sequencesloop statements
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
3/42
TMK 3102-Chap 8: ControlStatement Loo
3
Why w e need l oop? (1)
Cons ider t he St a tem ent s :
I will do my
homework!!
Write a java program to print the following statement 100 times
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
4/42
TMK 3102-Chap 8: ControlStatement Loo
4
Why w e need l oop? (1)
Cons ider t he St a tem ent s :
I will do my
homework!!
Write a java program to print the following statement 100 times
public static void main(String[] args){System.out.println(1. I will do my homework);..
System.out.println(100. I will do my homework);
}
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
5/42
TMK 3102-Chap 8: ControlStatement Loo
5
Why w e need l oop? (2)
Can you
identify the
input and
output???
Write a java program for the
following problem:
Read 5 integer and display the
value of their summation.
Input : 5 integern1, n2, n3, n4, n5
Output: The summation ofn1, n2,n3, n4, n5
Input example: 2 3 4 5 6
Output example: 20
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
6/42
TMK 3102-Chap 8: ControlStatement Loo
6
Why w e need l oop? (2)
Input n1
Input n2
Input n3
input n4
input n5
output sum
sum n1+n2+n3+n4+n5
start
end
..
..
.
n1 = in.nextInt();
n2 = in.nextInt();n3 = in.nextInt);
n4 = in.nextInt();
n5 = in.nextInt();
sum = n1+n2+n3+n4+n5
System.out.println(Sum=+sum);
6
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
7/42
TMK 3102-Chap 8: ControlStatement Loo
7
Why w e need l oop? (2)
Input n1
Input n2
Input n3
input n4
input n5
output sum
sum n1+n2+n3+n4+n5
start
end
..
..
.
n1 = in.nextInt();
n2 = in.nextInt();n3 = in.nextInt);
n4 = in.nextInt();
n5 = in.nextInt();
sum = n1+n2+n3+n4+n5
System.out.println(Sum=+sum);
7
HOW if number ofinput is more than
1000?
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
8/42
TMK 3102-Chap 8: ControlStatement Loo
8
l oop s ta t ement s
Change the program flow by execute ablock of statements repeatedly
whileloop
forloop
do.whileloop Loop Flow Control : breakand continue
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
9/42
TMK 3102-Chap 8: ControlStatement Loo
9
while Loop
Syntaxes
while (condition)
statement;
or
while (condition) {statement1;statement2;statements;
}
condition false
true
Statement(s)
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
10/42
TMK 3102-Chap 8: ControlStatement Loo
10
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
conditionfalse
true
Statement(s)
count =0;while (count < 100){
System.out.println(welcome);
count++;
}
//example of infinite loop
count =0;
while (count < 100)
System.out.println(welcome);10
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
11/42
TMK 3102-Chap 8: ControlStatement Loo
11
w hi le Loop (c ount er cont ro l)
Example:
i=0;
while (i
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
12/42
TMK 3102-Chap 8: ControlStatement Loo
12
while Loop (sent ine l cont ro l )
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);
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
13/42
TMK 3102-Chap 8: ControlStatement Loo
13
Self Test
How many times will the following loopexecute?
ctr = 0;
while (ctr == 5){
System.out.printf(ctr : %d,ctr);
ctr++;
}
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
14/42
TMK 3102-Chap 8: ControlStatement Loo
14
while Loop (c ase s t udy 1)
Write a java program that lets the userenter students grades. The program
then asked if user still has a grades ornot. When all grades data have beenentered, the program will calculate thegrade average and display it on the
screen.
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
15/42
TMK 3102-Chap 8: ControlStatement Loo
15
while Loop (c ase s t udy 2)
In January 2000, Ibrahim put RM25,000 ina retirement fund that paying 8.5 %interest per year. Now, he wants to know
how much money his fund will contain inyear 2020 and the interest made everyyear. He like to see this information in aform of table. Help him by writing a java
program to calculate and display therelated information as requested.
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
16/42
TMK 3102-Chap 8: ControlStatement Loo
16
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;
}
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
17/42
TMK 3102-Chap 8: ControlStatement Loo
17
for Loop f low
Initial value
condition
false
true
increase counter
Statement(s)
Syntax
for(initial value;condition;
increasecounter)Statement(s)
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
18/42
TMK 3102-Chap 8: ControlStatement Loo
18
for Loop f low
for (i=0; i
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
19/42
TMK 3102-Chap 8: ControlStatement Loo
19
for Loop f low 2
int sum=0;
int val = 0;
for (i=1; i
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
20/42
TMK 3102-Chap 8: ControlStatement Loo
20
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);
}
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
21/42
TMK 3102-Chap 8: ControlStatement Loo
21
Self Test
What is the output of the following javasegment:
num =0;
for (i=9;i>=0;i--) {
num = num + 10 * (i -1)
System.out.print(num + );
}
System.out.println();
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
22/42
TMK 3102-Chap 8: ControlStatement Loo
22
Self Test
Write a java program that used a forstatement to produce the Multiplication
Table for any number enter by user. Themultiplication for that number start withnumber 1 and end with number 12.
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
23/42
TMK 3102-Chap 8: ControlStatement Loo
23
do-w hi le Loop
Statements in the loop are executed first (atleast once, and condition is tested last
Loop is controlled by a condition or counter
Syntaxdo {
statement;
statement;
} while (condition);
statement;
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
24/42
TMK 3102-Chap 8: ControlStatement Loo
24
do-while Loop Ex am ple
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);
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
25/42
TMK 3102-Chap 8: ControlStatement Loo
25
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);
num
sum
12867
0
sum = sum + num % 10
sum = 0 + 12867 %10
sum = 0 + 7
sum = 7
7
num = num /10
num = 12867 / 10
num = 1286
1286
do-while Loop Ex am ple
25
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
26/42
TMK 3102-Chap 8: ControlStatement Loo
26
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);
num
sum
sum = sum + num % 10
sum = 7 + 1286 %10
sum = 7 + 6
sum = 13
7
num = num /10
num = 1286 / 10
num = 128
1286
13
128
do-while Loop Ex am ple
26
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
27/42
TMK 3102-Chap 8: ControlStatement Loo
27
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);
num
sum
sum = sum + num % 10
sum = 13 + 128 %10
sum = 13 + 8
sum = 21
13
num = num /10
num = 128 / 10
num = 12
128
21
12
do-while Loop Ex am ple
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
28/42
TMK 3102-Chap 8: ControlStatement Loo
28
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);
num
sum
sum = sum + num % 10
sum = 23 + 1 %10
sum = 23 + 1
sum = 24
23
num = num /10
num = 1 / 10
num = 0
1
24
0
do-while Loop Ex am ple
28
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
29/42
TMK 3102-Chap 8: ControlStatement Loo
29
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);
num
sum 29
1
24
0
Sum of the digit : 24
do-while Loop Ex am ple
29
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
30/42
TMK 3102-Chap 8: ControlStatement Loo
30
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(;;) {
}}
for(;;){
while()
{}
}
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
31/42
TMK 3102-Chap 8: ControlStatement Loo
31
Nest ed Loop (Ex am ple )
Draw a triangle of * by determine thesize of triangle first. Example; if size is 5,then the triangle is:
******
*********
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
32/42
TMK 3102-Chap 8: ControlStatement Loo
32
Nest ed Loop (Ex am ple )
//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 sectionSystem.out.print("What is the size of the triangle:");
input = console.nextInt();
//process section
for(i=1;i
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
33/42
TMK 3102-Chap 8: ControlStatement Loo
33
Nested Loop (Ex am ple det a i ls )
//process section
for(i=1;i
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
34/42
TMK 3102-Chap 8: ControlStatement Loo
34
break Sta tement
(Recall) in switch break used to skip nextstatement
Used to exit early froma loop.
Can be placed within ifstatement of a loop.
If condition is meet,loop is exitedimmediately.
loopfalse
true
Statement(s)
if..
break
true
false
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
35/42
TMK 3102-Chap 8: ControlStatement Loo
35
break examp le
Add the integers from 1 to 20 in the order tosum 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);
}}
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
36/42
TMK 3102-Chap 8: ControlStatement Loo
36
continue Sta tements
Used in while, for, and do...whilestructures.
When executed in a loop, the remainingstatements in the loop are skipped; proceedswith the next iteration of the loop.
When executed in a while/dowhilestructure, expression is evaluated immediatelyafter continue statement.
In a for structure, the update statement isexecuted after the continue statement; theloop condition then executes.
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
37/42
TMK 3102-Chap 8: ControlStatement Loo
37
continue example
Add all integers from 1 to 10 except the
number that can divide by 3.
37
l
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
38/42
TMK 3102-Chap 8: ControlStatement Loo
38
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
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
39/42
TMK 3102-Chap 8: ControlStatement Loo
39
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
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
40/42
TMK 3102-Chap 8: ControlStatement Loo
40
label refer to some location where loopstatement exist in java program by thespecial name.
40
whileloop:// label
while( condition) {
forloop://label
for(i=0;i
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
41/42
TMK 3102-Chap 8: ControlStatement Loo
41
continue and break w i th labe l
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(">");
}
}
}
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 8
42/42
Thank You