Programming 1
Nested loops
Objective
• control of loop using
✓ Break statement
✓ Continue statement
• Nested loop
Concept of loop
• Loop is the execution of a number of commands as a block
based on a certain condition a number of times.
• It is called Loop because the flow of operations is repeated in
loop.
Example of loop statements
• Write a program that prints the factorial of even numbers from 2 to 10
You could exit the loop before the condition becomes false???
• Usually the only way out of programming statements is to finish evaluating the condition with a false value.
• However, it is often best to terminate the programming statements when certain conditions are met, to save time of processor.
You could exit the loop before the condition becomes false???
• In Java that by use:
✓ Break statement
✓ Continue statement
Break concept
• The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned.
• The compiler then exits to execute the first program command immediately after loop.
• Used in loops or selection statements in general, but use in a switch is mandatory.
• How to write:
break;
Break Example
Break Example
• Output: 0, 1, 2, 3
Break Example
• Write a for program with break statement do the following:
• Read 10 numbers from users.
• Calculate the total number.
• If user enter number 999 then the loop is end and without calculate the number.
Break Example
Break Example
• Do the following:
• Enter 10 numbers from user.
• Calculate the total number
• If user enter 0, end the program and display the result.
Break Example
Continue concept
• The continue statement is used to force the compiler to end the current cycle and start a new cycle by returning to the loop head.
• How to write:
continue;
Continue Example
The execution go to the
condition
Ignore these
statements
This statement out of while loop
Continue with For loop
Continue with For loop
• Output: 0, 1, 2, 3, 5,6,7,8,9
• Where is the 4????
• Because continue statement the loop statement not executed when i =4
Continue with While loop
• Do the following:
• Use while loop to calculate total of numbers between 1 to 99
• If the number is multiples of 10 (10 , 20 , 30 ….) then ignore the current cycle in while loop and not calculate this number.
• Display the result
Continue with While loop
Difference between break and continue
Difference between break and continue
By using the continue
statement, the for loop
will continue to print the
numbers from 1 to 4 and
when the value of the
counter( count = 5 )will
ignore the rest of the
cycle and come to the
beginning of the for loop
resumed from the
number 6 and complete
printing.
Difference between break and continue
With break statement ,
the for loop will
continue to print the
numbers 1 to 4 and when
the value of the counter(
count = 5 ) will be forced
out of for loop.
Break Example
• The program will allow the user to enter 10 numbers and then collect them in case The user enters zero.
Break and Continue Example
• What is the output???
Nested loops
• Loop can contain another loop inside it.
• The internal loop must have a counter variable name different from the variable name counter for external loop.
• Loop inside another loop that creates nested loops.
• The internal loop is performed in each external loop cycle.
Nested loops
• the nested loop can be for, while or do.. while
Nested loops Example
• external For loop repeated 5 times.
• The internal For loop repeated 10 times for each external For loop.
• So the total of cycle in loops is 50 times.
Nested loops Example
• How much cycles in loops???
Nested loops Example
Nested loops Example
Nested loops Example
• What is the result ???
Nested loops Example
• What is the result ???
Nested loops Example
• What is the result ???
Nested loops Example
• What is the result ???
Nested loops Example
• Write program using nested for loop to get the following result ???
Nested loops Example
• Nested loop with more than 2 loops.
Notes
• One of the most common error is to write the counter name error for one of the nested loop.
Notes
• Write commands is a good practice in program.
• Without commands:
✓That make statements is not clear
✓Not easy to understand program.
Notes
// Print numbers(1, 2, 3, 4, 5) 10 times on 10 different lines
//Move between 10 lines
// Print numbers(1, 2, 3, 4, 5)
in the same line.
Summary
• control of loop using
✓ Break statement
✓ Continue statement
• Nested loop