+ All Categories
Home > Documents > Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the...

Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the...

Date post: 30-Aug-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
40
Programming 1 Nested loops
Transcript
Page 1: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Programming 1

Nested loops

Page 2: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Objective

• control of loop using

✓ Break statement

✓ Continue statement

• Nested loop

Page 3: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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.

Page 4: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Example of loop statements

• Write a program that prints the factorial of even numbers from 2 to 10

Page 5: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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.

Page 6: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

You could exit the loop before the condition becomes false???

• In Java that by use:

✓ Break statement

✓ Continue statement

Page 7: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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;

Page 8: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Break Example

Page 9: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Break Example

• Output: 0, 1, 2, 3

Page 10: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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.

Page 11: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Break Example

Page 12: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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.

Page 13: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Break Example

Page 14: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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;

Page 15: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Continue Example

The execution go to the

condition

Ignore these

statements

This statement out of while loop

Page 16: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Continue with For loop

Page 17: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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

Page 18: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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

Page 19: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Continue with While loop

Page 20: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Difference between break and continue

Page 21: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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.

Page 22: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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.

Page 23: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Break Example

• The program will allow the user to enter 10 numbers and then collect them in case The user enters zero.

Page 24: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Break and Continue Example

• What is the output???

Page 25: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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.

Page 26: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Nested loops

• the nested loop can be for, while or do.. while

Page 27: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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.

Page 28: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Nested loops Example

• How much cycles in loops???

Page 29: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Nested loops Example

Page 30: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Nested loops Example

Page 31: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Nested loops Example

• What is the result ???

Page 32: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Nested loops Example

• What is the result ???

Page 33: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Nested loops Example

• What is the result ???

Page 34: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Nested loops Example

• What is the result ???

Page 35: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Nested loops Example

• Write program using nested for loop to get the following result ???

Page 36: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Nested loops Example

• Nested loop with more than 2 loops.

Page 37: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Notes

• One of the most common error is to write the counter name error for one of the nested loop.

Page 38: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Notes

• Write commands is a good practice in program.

• Without commands:

✓That make statements is not clear

✓Not easy to understand program.

Page 39: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

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.

Page 40: Programming 1 - Yola nested Loop.pdfBreak concept • The break statement is used to force the compiler to exit even and If the number of iterations is not already assigned. • The

Summary

• control of loop using

✓ Break statement

✓ Continue statement

• Nested loop


Recommended