310 likes | 541 Views
APS105. Repeating. Repeating. Repeat a certain number of times Do 100 pushups! Repeat until done Do pushups until you are sore! In programming these are called “loops” “for” loops: counted, repeat until count is done “while” loops: repeat while a condition is true.
E N D
APS105 Repeating
Repeating • Repeat a certain number of times • Do 100 pushups! • Repeat until done • Do pushups until you are sore! • In programming these are called “loops” • “for” loops: counted, repeat until count is done • “while” loops: repeat while a condition is true loops are a very common part of programs!
While Loops • General form: while (<expression>) <statement> • Effect: • Evaluate expression • If expression is true • execute statement • goto (1)
While Loop Example1 • Read ints until zero is read, then print the sum .
While Loop Example1 (curt version) • Read ints until zero is read, then print the sum .
While Loop Example2 • Read ints until a negative num, then print the average .
Do-While Loops • “while input number is non-zero, print it and input another number” • Matches a “while” loop nicely • Note: may print zero times if first num is zero • “input a number, print it, repeat if non-zero • Note: will print at least once • Matchs a “do-while” loop nicely • do-while: general form: do <statement> while (<expression>);
Do-While Example • Input a number until between 1 and 10 .
For Loops • General form: for (<expr1>; <expr2>; <expr3>) <statement> • Identical to this while loop arrangement: <expr1>; while (<expr2>) { <statement> <expr3> }
For Loops (cont’d) • Typical use: for (<initialization>; <condition>; <update>) <statement> • <Initialization> • Executed once at the beginning • <condition> • Must be true (non-zero) to continue • <statement> • <update> • goto (2)
For Loop Example • Print the squares of the numbers 1..10 .
for variations: locally-declared counter variable • Example .
for variations: empty initialization • Example: .
for variations: empty update • Example: .
for variations: empty condition • If condition expression is empty: • Assumed to be true • Example: .
for variations: complex expr’s • Example: .
Which Kind of Loop to Use? • Use for: • If the number of repetitions is known • Use while: • If want to test condition before doing loop body • Use do-while: • If want to do loop body then test loop condition
Loops with Condition AND Counting • Sum the first 100 squares (starting at 1), but stop if the sum passes 100000 .
Nesting Loops: Example1 • Print a triangular pattern • Eg., if n is 5 then print: 1 12 123 1234 12345
Nesting Loops: Example2 • Print a triangular pattern of stars • Eg., if n is 5 then print: ***** **** *** ** *
Nesting Loops: Example3 • Print a triangular pattern of stars • Eg., if n is 5 then print: * ** *** **** *****
Another Loop Example • User inputs a digit (retry until a digit is input) • Then user inputs an integer • Print the number of occurrences of digit in int • Example User inputs digit 3 User inputs integer 342432 Program outputs: there are 2 occurrances of 3 in 342432