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)

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
While Loops. Challenge: ● Ask the user a simple math questions ● Continue asking the question until the user gets it right.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Introduction to Computing Science and Programming I
For loops For loops are controlled by a counter variable. for( c =init_value;c
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
CSE115/ENGR160 Discrete Mathematics 02/28/12
Week 5 - Friday.  What did we talk about last time?  Repetition  while loops.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Logo Lesson 5 TBE Fall 2004 Farah Fisher. Prerequisites  Given a shape, use basic Logo commands and/or a procedure to draw the shape, with and.
CIS101 Introduction to Computing Week 12 Spring 2004.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Control Structures FOR Statement Looping.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal.
Fundamental Programming Fundamental Programming More on Selection.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part D (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
© Jalal Kawash Programming Peeking into Computer Science 1.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CS101 Computer Programming I Chapter 4 Extra Examples.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part B (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Welcome to MM570 Psychological Statistics
Top Down Design Brent M. Dingle Texas A&M University Chapter 4 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Conditional Statements.  Quiz  Hand in your jQuery exercises from last lecture  They don't have to be 100% perfect to get full credit  They do have.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
1 CS161 Introduction to Computer Science Topic #8.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
5.04 Apply Decision Making Structures
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal.
Linda L. Jung EDIT 3318 Coins: Identity and Value 1 st Grade Begin Resource page.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
CHAPTER 9 ANNISA FAIZAH( ) RAHAJENG H. RARAS( ) ANA CLARISTI( ) DAMARINA( ) ASKING AND EXPLAINING.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part C (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Fall 2001(c)opyright Brent M. Dingle 2001 Multidimensional Arrays Brent M. Dingle Texas A&M University Chapter 10 – Section 2, part B (and some from Mastering.
Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering.
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
(c)opyright Brent M. Dingle 2001
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Brent M. Dingle Texas A&M University Chapter 12 – section 1
Introduction to TouchDevelop
Turbo Pascal Units (TPU)
Procedures Brent M. Dingle Texas A&M University
3.1 Iteration Loops For … To … Next 18/01/2019.
Java Programming Loops
Simple Branches and Loops
Chapter 2 – part a Brent M. Dingle Texas A&M University
(c)opyright Brent M. Dingle 2001
Complex Array Structures
Presentation transcript:

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. For example say we wanted to sell all the red cars we have. We would say: While (all cars not checked) DO BEGIN if (current car is red) then BEGIN Sell the current car END; END;

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.

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 }

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

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.

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

Coins, Nested if example Task Breakdown Assume we broke the task as follows: 1. Write the heading. 2. If the number of quarters is not zero then write it out. 3. If the number of dimes is not zero then write it out preceded by a comma or a comma followed by ‘and’ if needed. 4. If the number of nickels is not zero then write it out preceded by a comma or a comma followed by ‘and’ if needed. 5. If the number of pennies is not zero then write it out preceded by a comma or a comma followed by ‘and’ if needed.

Coins, Nested if example Task 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.

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

Coins, Nested if example Pseudocode 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 }

Coins, Nested if example Pseudocode 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 }

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]

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.

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’) end else begin writeln(‘right’); end

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.

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

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

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: 1. North if the user entered ‘N’ or ‘n’ 2. South if the user entered ‘S’ or ‘s’ 3. East if the user entered ‘E’ or ‘e’ 4. West if the user entered ‘W’ or ‘w’ 5. Not a direction if the user entered anything else

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] END else BEGIN [else’s body] END

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 }

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.

The CASE statement Same 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 }

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.

CASE Statement Downside 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.

CASE Statement Bonus 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;

Case Statement Default 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.

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.

Suggested Problems (no grade) page 229, – 17 page 233 – , 23, 24, 25, 28, (30 maybe)

End 6.3, Multiway Branches