Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1 while

Similar presentations


Presentation on theme: "Computer Science 1 while"— Presentation transcript:

1 Computer Science 1 while
Warm up How do you…

2 Learning Objectives Understand the semantics and syntax of another looping structure. Be able to write a program using this structure.

3 Warm-up In Pascal, write a program that can be used to find the total of the test scores for students in this class.

4 How do you…(use pseudo-code)
Modify this to work for a teacher in another class? Keep in mind how easily teachers make mistakes… not being able to count the number of students in class… Keep in mind how lazy teachers are… Not wanting to type in anything they don’t have to…

5 You need something that can…
Get another score every time the teacher adds a score. Repeat an unknown number of times Be able to stop getting scores when the teacher types in a special score for quitting (flag or sentinel) Possibly 0 times. (No students in class)

6 While..do loop When to use it…
When you need to repeat something an unknown number of times When it might not occur. Examples If you are getting a bunch of stuff from a person While the condition is true, it will go through the loop.

7 While..loop semantics Get Variable While (variable<>flag) do
Begin {Commands you will repeat} End {Note: This will be on every test/quiz from now to the end of the semester!!}

8 While..loop Syntax While (condition) do Begin End;
More than one command; End; Commands;

9 Dry Run program whiley; var a,b:integer; begin a:=0; b:=10;
while (a<b) do a:= a + 4; writeln(a,b); end; writeln('Done'); end.

10 Use the following input:
Dry run #2 Use the following input: 10, 8, 30, 6, 14, -1 program whilez; var score, x:integer; begin x:=0; writeln('Please enter a score, -1 to quit'); readln(score); while (score <> -1) do if score > x then x := score; end; writeln(‘Here is is ', x); end.

11 Check for Understanding: Dry run
Program WhatDoesItPRintOut; var a:integer; begin a:=5; while (a>2) do case a of 1,2: a:=a-1; 3: a:=a*2; 4,5:a:=a-1; 6:a:=a-4; end; writeln(a); End.

12 So… Modify the code to get an unknown number of scores and returning the total.

13 While loop program options
Input: An unknown number of scores Output: The total and average score. Push: Calculate the highest and the lowest score Input: An unknown number of names and ages. Output: The name and age of the oldest person. Write a program t' hold an election for t' worst pirate (Unknown # o' voters…) Black Beard T' Great Pirate Roberts Henry Morgan t' Terrible Show t' results o' t' vote and declare t' worst pirate.

14 While loop program 2 Millionaire Slug in Well Menu for projects
Input: A starting amount of money and an interest rate. Output: A chart showing how much money the user has after each year and how many years it will take to become a millionaire. Push: Add a feature that incorporates an annual donation to savings Push: Modify it to compound monthly Slug in Well Input: The depth of a well. Process: The slug climbs up 3’ each day, but if it does not climb out of the well then it slides back down 2’ at night Output: A chart showing the height of the slug at the end of each day and the number of days it takes the slug to climb out of the well. Push: Show it graphically Menu for projects Create a menu using the while loop that will let the user run a program you have written previously and when finished it will ask if you would like to run it again or quit. Rock-paper-scissors,…


Download ppt "Computer Science 1 while"

Similar presentations


Ads by Google