2. Objectives
In this chapter you will be able to :
• Introduce sentinel value in loop
• Variations of Loop implementation as in Java Syntax using
Sentinel value
• Introduce Nested loop
2
3. Sentinel-controlled Loop
• Sentinel-controlled repetition is sometimes called indefinite
repetition because it is not known in advance how many times
the loop will be executed.
• It is a repetition procedure for solving a problem by using
a sentinel value (also called a signal value, a dummy value or a
flag value) to indicate "end of data entry".
• Eg:
• DOWHILE( loop <> 0) , DOWHILE ( number <>99 )
3
4. Check Point 1:
4
Example 1: Problem
Using loop, display a string constant of “Hello”.
Stop the loop until user enter 99.
After all the task is finished, display “Done !”
Check Your Answer ?
INPUT PROCESS OUTPUT
5. True
Pseudocode
Display_Hello
Use loop as integer
Prompt for loop
Get loop
DOWHILE (loop<>99)
Display message “Hello”
Prompt for loop
Get loop
ENDDO
Display message “Done”
END
5
Initialization
Testing
Update
Flowchart
Start
Stop
Use loop as integer
Prompt for loop
Get loop
DOWHILE
(loop<>99)
Display message “Hello”
Prompt for loop
Get loop
False
7. Comparison of all Loop syntax: do..while( )
7
• Check Point:
• What is the output when you
entered the following input?
• A. 1,2,3,99 ?
• B. 10,99 ?
• C.99 ?
8. Comparison of all Loop syntax: for ( )
8
• Check Point:
• What is the output when you entered
the following input?
• A. 3, 99 ?
• B. 99?
• What should you change, if the program
must stop when user enter 0 ?
9. Nested Loop Function & Syntax
• It can be defined as a loop within a loop
• Just like a nest
9
Questions:
1. How many time(s) the
outer loop shall be
executed?
2. How many time(s) the
inner loop shall be
executed?
3. What is output from this
nested loop?
11. Nested Loop- Case Study[Multiplication Table]
11
Questions:
1. How many time(s) the outer
loop shall be executed?
2. How many time(s) the inner
loop shall be executed?
3. What is output from this
nested loop?
12. Nested Loop- Case Study[Multiplication Table]
12
It will display a 1*12
multiplication table
until 12 !!!!
13. Check Point - Nested Loop – Write Java Codes
• Create your own multiplication table to display the following
table using nested loop:
13
Questions:
1. What is outer loop
control variable
statement?
2. How many times the
outer loop should
repeat?
3. What is the inner loop
control variable
statement?
4. How many times the
inner loop should
repeat?
15. Nested Loop- Tracing Output 1
for (loop=1;loop<=3;loop++)
{
for(inLoop=1;inLoop<=2;inLoop++)
{
//total=loop*2 >>arithmetic operation
System.out.println(“Value is” + loop*2);
}
System.out.println(“@@@@@”);
}
System.out.println(“Thank you”);
15
Check Point 1:
1. What is the name of loop
control variable for the outer
loop statements? loop
2. What is the name of loop
control variable for the inner
loop statements? inLoop
3. What is the output for the
following statements?
16. Dry run Table – Tracing Output 1
16
loop loop<=3 loop++ inLoop inLoop<=2 inLoop++ Output
1 1<=3?True 1+1=2 1 1<=2? True 1+1=2 Value is 2
2 2<=2?True 2+1 =3 Value is 2
3 3<=2? False @@@@@
2 2<=3?True 2+1=3 1 1<=2? True 1+1=2 Value is 4
2 2<=2?True 2+1=3 Value is 4
3 3<=2? False @@@@@
3 3<=3? True 3+1=4 1 1<=2? True 1+1=2 Value is 6
2 2<=2?True 2+1=3 Value is 6
3 3<=2? False @@@@@
4 4<=3? False Thank you
17. Nested Loop- Tracing Output 2
for (loop=1;loop<=2;loop++)
{
for(inLoop=1;inLoop<=4;inLoop++)
{
System.out.println(“Inner is” + inLoop);
} //end of inLoop
System.out.println(“@@@@@”);
System.out.println(“Outer loop:”+loop);
} //end of loop
System.out.println(“Thank You. End of Code”);
17
Check Point 2:
1. What is the name of loop
control variable for the outer
loop statements? loop
2. What is the name of loop
control variable for the inner
loop statements? inloop
3. What is the output for the
following statements?
19. Home work: Check Point Nested Loop &Array
• Question 1
• Display yearly total sales from 4 teams of Atomie
Marketing&Sales Network Company. Each team will need to
input their monthly sales.
• Produce IPO, Pseudocode, Flowchart and Java. Use nested loop.
• Question 2
• Modify the java code so that it will use Array to store data
19
20. Home work: Check Point Nested Loop –
Pseudocode?
20
Input Process Output
monthlySales Using nested loop, display yearly total sales
from 4 teams of Atomie Marketing&Sales
Network Company. Each team will need to input
their monthly sales.
yearlyTotalSales
22. Check Point 1
• Design an algorithm that will prompt for your age in years and
months. Calculate and display your age in months. If the
calculated months figure is more than 500, three asterisks
should also appear beside the month figure. Your program is to
continue processing until a sentinel of 999 is entered.
22
INPUT PROCESS OUTPUT
Check Your Answer ?
23. Check Point 2
• Design an algorithm that will prompt for total collection of
payroll amount entered by branch manager of Sushi King
Company until a sentinel amount of -99 is entered. After the
sentinel has been entered, display the total payroll amount.
23
INPUT PROCESS OUTPUT
Check Your Answer ?