Download presentation
Presentation is loading. Please wait.
Published byShinta Farida Agusalim Modified over 5 years ago
1
Computer Science 1 For..do loop Dry Run Take notes on the For loop
Complete one of the for loop programs.
2
Dry Run What is this program doing?
How can you modify this to find the total of three scores the user entered? How would you modify it to find the total of four scores? Five scores? 100 scores?
3
Computer Science 1 For loop
4
Learning Objective Be able to implement a loop in a program
5
Constructs Linear, You do something, then the next thing, … Decision,
Writeln, readln, :=, .. Decision, If..then If..then..else case Looping, You repeat something. For..do Repeat..until While..do
6
For..do loop When Semantics Syntax
When you need to repeat something a set number of times Semantics Syntax For loopingvariable := start to finish do Begin Code that is repeated end Can be a constant ( 1 or 5 ) or a variable or an expression that results in a matching type Must be an ordinal type variable. (Integer/ char) No ; Use to for counting up and downto for counting down You can leave off the begin and end if there is only one line of code being repeated.
7
For loop total example How would you modify this to get 5 scores?
//Mr. Smith // For loop example program // Computer Science 1 // program fortotal; var score, total, count: integer; begin total:=0; for count:= 1 to 4 do writeln('Please enter a score.'); readln(score); total:= total + score; end; writeln('The total of the scores = ', total); end. How could you have the program echo the user’s input to the screen? How would you modify this to get 5 scores? How could you let the user determine how many times it will go through the loop?
8
Dry Run For count:= 2 to 5 do Begin End; If (count mod 2) = 1 then
Writeln(‘Dino’) Else Writeln(‘Scooby’); End;
9
Program options Input 8 scores, find the average (Add the scores and divide by the number of scores.) Power: Input the base and the exponent. And calculate and show the power Ex. Input base = 2, exponent = 3 Calculation: 2^3 = 2*2*2 Displays 2^3 = 8 Factorial: Input a positive integer, output it’s factorial. Ex.: Input 5 Calc: 5! = 1*2*3*4*5 Output: 5! = 120
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.