1 / 30

Loops

Loops. Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan). Nested if’s. Sometimes when one condition is true we will want to check to see if something else is true.

candrews
Download Presentation

Loops

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. Loops Brent M. DingleTexas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3rd Edition by Tom Swan)

  2. Nested if’s • Sometimes when one condition is true we will want to check to see if something else is true. • For example say we wanted to sell all the red cars we have. We would say:While (all cars not checked) DOBEGIN if (current car is red) then BEGIN Sell the current car END;END;

  3. Nested if’s (cont) • Now say we still wanted to sell all our red cars but we wanted to sell (red) cars made before 1980 for $500 and all other (red) cars for $2000. • For this we could use a nested if, shown on the next slide.

  4. Nested if’s (cont 2) WHILE (all cars not checked) DO BEGIN if (current car is red then) Begin if (current car made before 1980) then begin sell car for $500 end { if car pre-1980) else begin sell car for $2000 end; End; { if car is red } END; { while not all cars checked }

  5. Nested if’s (cont 3) • Notice that we have an if inside the body of a previous if. This is called a nested if.

  6. Coins, Nested if example • Suppose we were asked to write a program that: • Requests an amount of change from 1 to 99 cents and • Tells the user a combination of coins that equals that amount.

  7. Coins, Nested if example 2 • So here is a sample run:Please enter the amount of change 3737 cents can be given asone quarter, one dime, and 2 pennies • How would you break this programming task into smaller subtasks?

  8. Coins, Nested if exampleTask Breakdown • Assume we broke the task as follows: • Write the heading. • If the number of quarters is not zero then write it out. • If the number of dimes is not zero then write it out preceded by a comma or a comma followed by ‘and’ if needed. • If the number of nickels is not zerothen write it out preceded by a comma or a comma followed by ‘and’ if needed. • If the number of pennies is not zerothen write it out preceded by a comma or a comma followed by ‘and’ if needed.

  9. Coins, Nested if exampleTask Breakdown 2 • We will use nested if’s to perform tasks 2, 3, 4 and 5. • We will also use a boolean variable as a flag to indicate whether we output any previous coins. • This variable is named prev_coins. • It will be set = true when we output a [type of coin]’s amount.

  10. Coins, Nested if exampleVariable Declarations • Assume the following variable declarations:quarters, dimes : integer;nickels, pennies : integer;prev_coins : boolean; • Now on to the pseudocode…

  11. Coins, Nested if examplePseudocode for DIMES { Here we have a pre-condition to the if } { prev_coins = true if some number of quarters has been output } if ( dimes > 0 ) then begin if (prev_coins) then { or if ( prev_coins = true) } begin if (there are more coins to follow) then begin output a comma and needed spaces end else { dimes is the last type of coin to be output } begin output a comma followed by the word ‘and’ along with needed spaces end; end; { if previous coin type output } Output the number of dimes prev_coins := true; end; { dimes > 0 }

  12. Coins, Nested if examplePseudocode for NICKELS { prev_coins = true if some number of dimes has been output } if ( nickels > 0 ) then begin if (prev_coins) then { or if ( prev_coins = true) } begin if (there are more coins to follow) then begin output a comma and needed spaces end else { nickels is the last type of coin to be output } begin output a comma followed by the word ‘and’ along with needed spaces end; end; { if previous coin type output } Output the number of nickels prev_coins := true; end; { nickels > 0 }

  13. Coins, Nested if example Pseudocode • The pseudocode for quarters and pennies would be virtually identical. • Notice that in all cases we used a nested if to determine if we should output: , [space]OR , and [space]

  14. Multiway if-then-else • A standard if-then-else has TWO branches or TWO possible actions. • if [something] then [action 1]else [action 2] • Sometimes we will want more branches.

  15. Multiway if-then-else (cont 1) • Say for example you asked someone to guess a number. • After they guess you could just tell them right or wrong. • This would be done in Pascal by:if (guess <> my_number) then begin writeln(‘wrong’) endelse begin writeln(‘right’); end

  16. Multiway if-then-else (cont 2) • But that wouldn’t help them much with their next guess. • Perhaps instead you wished to tell them: too high too low correct • You could do this in Pascal as shown on the next slide.

  17. Multiway if-then-else (cont 3) if (guess > my_number) then begin writeln(‘too high’); end else if (guess < my_number) then begin writeln(‘too low’); end else begin writeln(‘correct’); end

  18. Multiway if-then-else (cont 4) • And that is an example of a multiway if-then-else. • Notice you could add as manyelse if [expr] thenlines as you desired. • Which would add as many branches as you might possibly need.

  19. Challenge ( no grade ) • Write a program that asks the user to enter a single character. • Use a 5 way if-then-else branch to output: • North if the user entered ‘N’ or ‘n’ • South if the user entered ‘S’ or ‘s’ • East if the user entered ‘E’ or ‘e’ • West if the user entered ‘W’ or ‘w’ • Not a direction if the user entered anything else

  20. General Rule about if-else • Every ‘else’ will “link back to” the nearest ‘if’ above it. • It is best to ALWAYS place BEGIN and END around the body of an if, e.g. if [expr] then BEGIN [if’s body] ENDelse BEGIN [else’s body] END

  21. The CASE statement • The case statement is similar in behavior to the multiway if-else. • If you have n values to check you can say: CASE [variable] OF [value 1] : [statement 1]; [value 2] : [statement 2]; : : [value n] : [statement n]; END; { case }

  22. The CASE statement (cont 1) • Notice the case statement really only checks for equality which is a bit limiting. • However if you want to do the same thing for 2 or more different values the case statement makes things a little bit easier.

  23. The CASE statementSame action different values • Consider the if statement: if (x = 5) OR ( x = 7) OR (x = 8) then [statement 1] else if (x = 34) OR (x = 67) then [statement 2] • Contrast with CASE x OF 5, 7, 8 : [statement 1]; 34, 67 : [statement 2]; END; { case }

  24. The CASE statement Same action, different val • Which of the above would be easier to type? • Hard to say, but if there were five or six values associated with each statement and there were 9 or 10 statements, I’m thinking the CASE would be less typing.

  25. CASE StatementDownside • Because CASE statements only check on EQUALITY, they will NOT allow you to use a variable of type real with them. • Remember that. • They also do NOT let you check for inequality.

  26. CASE StatementBonus • The case statement will let you test on a RANGE of values: CASE x OF ‘A’ .. ‘Z’ : [action 1]; ‘0’ .. ‘9’ : [action 2]; else [default action]; END;

  27. Case StatementDefault action • Notice that you may, but do not have to, put an else right before the end of a case statement. e.g. CASE x OF ‘a’ : [statement 1] ‘b’ : [statement 2] else [default statement] end; • This allows you to add a default action should your variable not have any of the checked for values.

  28. Challenge ( no grade ) • Write a program that will select a random number from 1 to 100 and then ask the user to guess it. • After each guess the computer should tell the user if they were too high or too low or correct. • The program should end when the user has guessed 7 times or when the user correctly guesses the number. • Thought question: why 7 tries? Will the user always be able to guess the correct number? Why? Hint: start at 50 (then 25 or 75 then 12 or 87 then …) • Ask your TA about the functions random(100) and randomize.

  29. Suggested Problems (no grade) • page 229, 23011 – 17 • page 233 – 23520, 23, 24, 25, 28, (30 maybe)

  30. End 6.3, Multiway Branches

More Related