150 likes | 232 Views
CMP 131 Introduction to Computer Programming. Violetta Cavalli-Sforza Week 7, Lecture (Wednesday). TODAY. IF-THEN and IF-THEN-ELSE statements Using compound statements with them IF-THEN-ELSE for multiple choice CASE statement for multiple choice. Yes/No Choice.
E N D
CMP 131Introduction to Computer Programming Violetta Cavalli-Sforza Week 7, Lecture (Wednesday)
TODAY • IF-THEN and IF-THEN-ELSE statements • Using compound statements with them • IF-THEN-ELSE for multiple choice • CASE statement for multiple choice
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.
IF statement condition THEN statement 1 statement 3 statement 2 Yes/No Choice: The IF-THEN Statement • Syntax: IF <condition> THEN <statement> • Syntax graph: • Flowchart: yes condition no
{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.
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.
IF THEN condition ELSE statement statement statement 1 statement 4 statement 2 statement 3 yes no condition Either/Or Choice : The IF-THEN-ELSE Statement • Syntax: IF <condition> THEN <statement> ELSE <statement> • Syntax graph: • Flowchart:
{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.
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
{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.
{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 BEGIN Temp := Number2; Number2 := Number1; Number1 := Temp; END; writeln('The ordered numbers are:', Number1, Number2); readln END.
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!!!
{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) ELSE writeln('The ordered numbers are:', Foo, Nem, Bar) ELSE { Foo >= Bar } IF Foo < Nem THEN writeln('The ordered numbers are:', Bar, Foo, Nem) ELSE IF Nem < Bar THEN writeln('The ordered numbers are:', Nem, Bar, Foo) ELSE writeln('The ordered numbers are:', Bar, Nem, Foo); readln END.
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 BEGIN 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) ELSE writeln('The ordered numbers are:', Foo, Nem, Bar) END ELSE { Foo >= Bar } BEGIN writeln(‘Foo >= Bar’); IF Foo < Nem THEN writeln('The ordered numbers are:', Bar, Foo, Nem) ELSE IF Nem < Bar THEN writeln('The ordered numbers are:', Nem, Bar, Foo) ELSE writeln('The ordered numbers are:', Bar, Nem, Foo); END readln END. compound statement compound statement