Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program options Write a program for one of the following

Similar presentations


Presentation on theme: "Program options Write a program for one of the following"— Presentation transcript:

1 Program options Write a program for one of the following
Volume and surface area of cylinder (YourNameCylinder) Input: The radius and height of a cylinder. Process: Surface area = πr2 + πr2 +2 πrh Volume = πr2h Output: The Volume and surface area of the cylinder. Write a program to calculate the volume of a spherical cap. Include the needed inputs. (YourNameSphere) a = radius of the cap r = radius of the sphere h = height of the cap

2 Write a second program for one of the following

3 Computer Science 1 If..then..else

4 Learning Objectives Understand the semantics and syntax of if then
Be able to read a program that has if .. Then Be able to write a program that uses if..then.

5 How do you. Congratulate someone if they have done a great job
How do you.. Congratulate someone if they have done a great job? Check to see if three sides make a triangle? Check if an equation has real roots?

6 If..then..else When Semantics
If it makes sense to say ‘if’, it probably makes sense to use if If you want to check to make sure you have a triangle before finding its’ area Check to make sure you have real roots before using the quadratic equation If you want to password protect your program. Semantics

7 Note the missing semicolon!
Syntax: If..Then If (condition) then Begin {Commands to do when the condition is TRUE} end; //or SingleStatement; Note the missing semicolon! Program IfThenSample; Var Testscore:integer; Begin Writeln(‘Please enter a test score’); Readln(testscore); If (testscore>95) then Writeln(‘Congratulations’); Writeln(‘Keep up the good work’); End; End. Note the indenting

8 Conditions: Boolean Idea Symbol Example Less than <
If (score <20) then Greater than > If (score > 95) then Less than or equal to <= If (score <= 50) then Greater than or equal to >= If (score >= 94) then Equal to = If (name = ‘Wilma’) then Not equal to <> If (answer <> password) then Order of ops () If ((score + 8) < total) then

9 AND OR NOT AND OR NOT Connects two conditions
The statement is true iff both conditions are true. If ((age >= 13) and (age<20)) then begin writeln(‘You are a teenager’); end; OR The statement is true if either one or the other or both conditions are true If ((name = ‘Smith’) OR (class = ‘CS1’)) then Writeln(‘You’’re in the correct class’); NOT Changes true to false and false to true If NOT (score<90) then Writeln(‘You have an A ‘);

10 Looking at the condition: True or False
For this example assume the following a:= 4; b:= 6; c:= 3; d:= 5.2; (5<4) (12>6) and(3<2) NOT ((8>7) OR (6<>3)) ((a>c) and (d<b)) OR (6>=b) What condition would you use to determine if the temperature (t) is in the 90s? (temp

11 NO SEMICOLON BEFORE THE ELSE!!!
Syntax: If..Then..Else If (condition) then Begin {Commands to do when the condition is TRUE} end else begin {Commands to do in the condition is FALSE} end; NO SEMICOLON BEFORE THE ELSE!!!

12 Dry Run Write the names for the variables Write Screen
Step through the program and change the variables/screen based on the commands.

13 Fix the following This program is on the website as Fix9-20.pas
Load it and save it to your folder before fixing.

14 Program options. Input: A test score.
Output: A congratulations if the score is above 90. Push: Add comments for scores 80-90, 70 – 80 , 60 – 70 etc… Input: The lengths for three sides of a triangle. Output: Whether or not the three sides make a triangle. Push: Calculate the area of this triangle. Push: Determine if it is a right triangle. Input: Three scores. Output the three scores in order lowest to highest.


Download ppt "Program options Write a program for one of the following"

Similar presentations


Ads by Google