Download presentation
1
CMP 131 Introduction to Computer Programming
Violetta Cavalli-Sforza Week 7, Lecture (Wednesday)
2
TODAY IF-THEN and IF-THEN-ELSE statements
Using compound statements with them IF-THEN-ELSE for multiple choice CASE statement for multiple choice
3
Yes/No Choice The logic: IF some condition or test is TRUE THEN perform some action [ ELSE do nothing ] Examples: If the car is running out of gas, put in gasoline. If you are failing your class and you cannot drop it, study more. If someone says hello and you don’t want to seem impolite, say hello back.
4
Yes/No Choice: The IF-THEN Statement
Syntax: IF <condition> THEN <statement> Syntax graph: Flowchart: IF condition THEN statement statement 1 yes condition statement 2 no statement 3
5
{File: ABSVAL1.PAS} PROGRAM AbsoluteValue; VAR Number : real; BEGIN writeln('Type a number, then press enter: '); readln(Number); write('The absolute value of ', Number, ' is '); IF Number < 0 THEN Number := -Number; writeln(Number); readln; END.
6
Either/Or Choice The logic: IF some condition or test is TRUE THEN perform some action ELSE perform a different action Examples: If the weather is hot wear light clothes, else wear warm clothes. If a number is odd increment OddCount else increment EvenCount If a noun starts with a capital letter categorize it as a proper noun (name) else as a common noun.
7
Either/Or Choice : The IF-THEN-ELSE Statement
Syntax: IF <condition> THEN <statement> ELSE <statement> Syntax graph: Flowchart: IF condition THEN statement ELSE statement 1 condition yes statement 4 statement 2 no statement 3
8
{File: ABSVAL2.PAS} PROGRAM AbsoluteValue; VAR Number : real; BEGIN writeln('Type a number, then press enter: '); readln(Number); write('The absolute value of ', Number, ' is '); IF Number < 0 THEN writeln(-Number) ELSE writeln(Number); readln; END.
9
IF-THEN(-ELSE) and Compound Statements
Conditional statements: IF <condition> THEN <statement> IF <condition> THEN <statement> ELSE <statement> What is <statement>? A single statement: An input or output statement An assignment Another IF … THEN … ELSE statement (nested conditional statements) A looping statement (FOR, WHILE, REPEAT) … A compound statement: BEGIN <statement> ; <statement> ; …. <statement> END
10
{File: ORDER1.PAS} PROGRAM Order2Numbers; VAR Number1, Number2 : real; BEGIN writeln('Enter 2 numbers, then press ENTER: '); readln(Number1, Number2); IF Number1 < Number2 THEN writeln('The ordered numbers are:', Number1, Number2) ELSE writeln('The ordered numbers are:', Number2, Number1); readln END.
11
{File: ORDER2.PAS} PROGRAM Order2Numbers; VAR Number1, Number2, Temp : real; BEGIN writeln('Enter 2 numbers, then press ENTER: '); readln(Number1, Number2); IF Number2 < Number1 THEN Temp := Number2; Number2 := Number1; Number1 := Temp; END; writeln('The ordered numbers are:', Number1, Number2); readln END.
12
Ordering 3 numbers… What can happen?
Foo =< Bar =< Nem Nem =< Foo =< Bar Foo =< Nem =< Bar Bar =< Foo =< Nem Nem =< Bar =< Foo Bar =< Nem =< Foo So you have to test all the possible input combinations!!!
13
{File: ORDER3.PAS} PROGRAM Order3Numbers; VAR Foo, Bar, Nem, Temp : real; BEGIN writeln('Enter 3 numbers, then press ENTER: '); readln(Foo, Bar, Nem); IF Foo < Bar THEN IF Bar < Nem THEN writeln('The ordered numbers are:', Foo, Bar, Nem) ELSE IF Nem < Foo THEN writeln('The ordered numbers are:', Nem, Foo, Bar) writeln('The ordered numbers are:', Foo, Nem, Bar) ELSE { Foo >= Bar } IF Foo < Nem THEN writeln('The ordered numbers are:', Bar, Foo, Nem) IF Nem < Bar THEN writeln('The ordered numbers are:', Nem, Bar, Foo) writeln('The ordered numbers are:', Bar, Nem, Foo); readln END.
14
Test Set Foo Bar Nem Foo < Bar < Nem 1 2 3 Nem < Foo < Bar
Foo = Nem = Bar
15
If you want to add a statement
{File: ORDER3.PAS} PROGRAM Order3Numbers; VAR Foo, Bar, Nem, Temp : real; BEGIN writeln('Enter 3 numbers, then press ENTER: '); readln(Foo, Bar, Nem); IF Foo < Bar THEN writeln(‘Foo < Bar’); IF Bar < Nem THEN writeln('The ordered numbers are:', Foo, Bar, Nem) ELSE IF Nem < Foo THEN writeln('The ordered numbers are:', Nem, Foo, Bar) writeln('The ordered numbers are:', Foo, Nem, Bar) END ELSE { Foo >= Bar } writeln(‘Foo >= Bar’); IF Foo < Nem THEN writeln('The ordered numbers are:', Bar, Foo, Nem) IF Nem < Bar THEN writeln('The ordered numbers are:', Nem, Bar, Foo) writeln('The ordered numbers are:', Bar, Nem, Foo); readln END. compound statement compound statement
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.