Download presentation
Presentation is loading. Please wait.
1
Repetition 04-03-15
2
Repetition Important Do things over and over thousands of times. Computer fast, accurate and doesn't get bored. Example count.c
3
Basic Loop Structures First, need a repetition statement while statement for statement do-while statement Second, need a condition to be evaluated (x <= 5)
4
Basic Loop Structures Third, need statement to initialize the condition being tested. x = 1; Fourth, need statement within the repeating code to update the condition x++;
5
While Statement Syntax Basic statement syntax. while(expression) statement; Statement can be a block. while(expression) { statement 1; statement 2;... }
6
While Statement Could also count down by subtracting. down.c
7
Repetition to Make a Table Put Heading in first Make a table of odd numbers, up to 20. odds.c
8
Infinite Loop Will go on indefinitely without intervention. to stop. Whenever possible, avoid using == and != to avoid infinite loops.
9
Another Table Example Make a table of squares of numbers from 1 to 12. Algorithm: 1)Set size to 12 2)Print headings 3)Set num to 1 4)While (num <= size) a) square = num*num b) Print num and square c) num++
10
Another Table Example(cont.) table.c – Let's finish the loop.
11
Conversion table(Skip) For sports I want a conversion table to convert kilometers to miles. I want it to start with 3 km. I want it to go in increments of 3. I will stop at 36 km. There are.62 miles in a km. Let's write an algorithm Program started in kmToMi.c
12
Participation In lab: Write a program section that prints the even numbers from 10 to 20, one number per line. Place a heading “Even Numbers” above the list.
13
Next Computing Sums and Averages with a while.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.