aps105
Download
Skip this Video
Download Presentation
APS105

Loading in 2 Seconds...

play fullscreen
1 / 30

APS105 - PowerPoint PPT Presentation


  • 176 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' APS105' - varana


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
aps105

APS105

Repeating

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 loops1
While Loops
  • General form:

while (<expression>)

<statement>

  • Effect:
    • Evaluate expression
    • If expression is true
      • execute statement
      • goto (1)
while loop example1
While Loop Example1
  • Read ints until zero is read, then print the sum

.

while loop example1 curt version
While Loop Example1 (curt version)
  • Read ints until zero is read, then print the sum

.

while loop example2
While Loop Example2
  • Read ints until a negative num, then print the average

.

do while loops
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
Do-While Example
  • Input a number until between 1 and 10

.

for loops1
For Loops
  • General form:

for (<expr1>; <expr2>; <expr3>)

<statement>

  • Identical to this while loop arrangement:

<expr1>;

while (<expr2>)

{

<statement>

<expr3>

}

for loops cont d
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
For Loop Example
  • Print the squares of the numbers 1..10

.

for variations empty condition
for variations: empty condition
  • If condition expression is empty:
    • Assumed to be true
  • Example:

.

which kind of loop to use
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
Loops with Condition AND Counting
  • Sum the first 100 squares (starting at 1), but stop if the sum passes 100000

.

nesting loops example1
Nesting Loops: Example1
  • Print a triangular pattern
    • Eg., if n is 5 then print:

1

12

123

1234

12345

nesting loops example2
Nesting Loops: Example2
  • Print a triangular pattern of stars
    • Eg., if n is 5 then print:

*****

****

***

**

*

nesting loops example3
Nesting Loops: Example3
  • Print a triangular pattern of stars
    • Eg., if n is 5 then print:

*

**

***

****

*****

another loop example
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