1 / 15

CMP 131 Introduction to Computer Programming

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.

rollo
Download Presentation

CMP 131 Introduction to Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMP 131Introduction 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. 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

  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. 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:

  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 BEGIN 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) 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.

  14. Test Set

  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 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

More Related