if and if-else: Part 1



Similar documents
Chapter 8 Selection 8-1

Free Report. My Top 10 Tips to Betting Like a Pro With Zero Risk

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Conditions & Boolean Expressions

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Lecture 2 Notes: Flow of Control

Visual Logic Instructions and Assignments

How To Increase Your Odds Of Winning Scratch-Off Lottery Tickets!

Math Games For Skills and Concepts

Common Beginner C++ Programming Mistakes

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

F ahrenheit = 9 Celsius + 32

Book of over 45 Spells and magic spells that actually work, include love spells, health spells, wealth spells and learning spells and spells for life

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Boolean Logic in MATLAB

Programming Your App to Make Decisions: Conditional Blocks

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Computer Programming In QBasic

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Multiplication Rules! Tips to help your child learn their times tables

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

CHAPTER 18 Programming Your App to Make Decisions: Conditional Blocks

Lecture Notes on Linear Search

Term Project: Roulette

Chapter 5. Selection 5-1

Iteration CHAPTER 6. Topic Summary

Module 6.3 Client Catcher The Sequence (Already Buying Leads)

Lab 11. Simulations. The Concept

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Example of a Java program

Kenken For Teachers. Tom Davis June 27, Abstract

Sources: On the Web: Slides will be available on:

J a v a Quiz (Unit 3, Test 0 Practice)

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

The first program: Little Crab

MATH 140 Lab 4: Probability and the Standard Normal Distribution

Creating Maze Games. Game Maker Tutorial. The Game Idea. A Simple Start. Written by Mark Overmars

Building Java Programs

Principles of Modeling: Real World - Model World

Ready, Set, Go! Math Games for Serious Minds

2 The first program: Little Crab

Informatica e Sistemi in Tempo Reale

Successful People. By John Celestand Field Associate of World Leadership Group

Android Programming Family Fun Day using AppInventor

CSC 221: Computer Programming I. Fall 2011

Question: What is the probability that a five-card poker hand contains a flush, that is, five cards of the same suit?

Moving from C++ to VBA

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

Not agree with bug 3, precision actually was. 8,5 not set in the code. Not agree with bug 3, precision actually was

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

LOOPS CHAPTER CHAPTER GOALS

The Challenge of Helping Adults Learn: Principles for Teaching Technical Information to Adults

Probabilistic Strategies: Solutions

Chapter 14: Boolean Expressions Bradley Kjell (Revised 10/08/08)

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Basics of I/O Streams and File I/O

Independent samples t-test. Dr. Tom Pierce Radford University

Pseudo code Tutorial and Exercises Teacher s Version

Microsoft Excel Tips & Tricks

Average producers can easily increase their production in a larger office with more market share.

Math: Study Skills, Note Taking Skills, And Test Taking Strategies

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Getting the best from your 360 degree feedback

Lesson 26: Reflection & Mirror Diagrams

CS1010 Programming Methodology A beginning in problem solving in Computer Science. Aaron Tan 20 July 2015

Understanding class definitions

Everyday Math Online Games (Grades 1 to 3)

Tutorial: Creating Platform Games

Introduction to Python

Quick Tricks for Multiplication

JavaScript: Control Statements I

>> My name is Danielle Anguiano and I am a tutor of the Writing Center which is just outside these doors within the Student Learning Center.

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Data Structures. Algorithm Performance and Big O Analysis

Python Programming: An Introduction To Computer Science

Will Dormann: Sure. Fuzz testing is a way of testing an application in a way that you want to actually break the program.

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

Object Oriented Software Design

6. Control Structures

Chapter One Introduction to Programming

Appendix K Introduction to Microsoft Visual C++ 6.0

Math Workshop October 2010 Fractions and Repeating Decimals

I Miss My Pet: A workbook for children about pet loss

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

A whole new stream of income - For many, affiliate commissions are like "found money."

Debugging Strategies

This explains why the mixed number equivalent to 7/3 is 2 + 1/3, also written 2

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Week 5: Expected value and Betting systems

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

JavaScript For Cats. An introduction for new programmers. Table of contents. Don't be a scaredy-cat. So easy your human companion could do it too!

Moving from CS 61A Scheme to CS 61B Java

Pizza. Pizza with Everything Identifying & Estimating Fractions up to 1. Serves 2-4 players / Grades 3+ (Includes a variation for Grades 1+)

SAT Math Facts & Formulas Review Quiz

Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6

Transcription:

if and if-else: Part 1 Objectives Write if statements (including blocks) Write if-else statements (including blocks) Write nested if-else statements We will now talk about writing statements that make decisions. In other words statements that decide whether to run other statements or not. 1 of 21

if Statements So far you know that C++ can do arithmetic, print integer values, print string values, accept integer values from the keyboards, create and update variables. You should expect more! (Otherwise C++ is pretty dumb.) For instance you would expect programs to make decisions. Now wouldn't it be nice if C++ can do this: or if alien is killed: score = score + 10 if my ship's energy is 0: end the game Note that for the first case, if the alien is not killed, you do not want to execute the statement score = score + 10. Otherwise everyone would keeping getting the same score! What kind of dumb game is that? In fact C++ does understand the if statement. Run this program if (x > 0) std::cout << "spam!" << std::endl; std::cout << "eggs" << std::endl; Run the program several time, entering different values for x. Note that the statement printing "spam!" is controlled by the if. For readability, the if statement is written like this: if (x > 0) std::cout << "spam!" << std::endl; std::cout << "eggs" << std::endl; Big picture: The if statement looks like this if ([bool expr]) [stmt] 2 of 21

where [bool expr] is a boolean expression this means that it's an expression that can be evaluated to a boolean value and [stmt] is a C++ statement (of course there's a semi-colon at the end). Since we need boolean expressions it's a good time now to recall the following:! not && "and" "or" == "is equal to"!= "is not equal to" < "is less than" <= "is less than or equal to" > "is greater than" >= "is greater than or equal to" An example of a boolean expression is [variable] [bool op] [value] where [variable] is a variable of int or double or float type, [bool op] is one of the above boolean operators and [value] is an int or double or float. Don't forget that is there is a type mismatch, C++ will try to type promote one value to match the type of the other. For instance if x is an int variable and the boolean expression is x < 1.23 then C++ will use the double of x: double(x) < 1.23 Exercise. Modify the above program so that spam! is printed when the integer entered is 42. Test your program. Exercise. Modify the above program so that the program prompts the user for his/her favorite number and print you don't like 42?!? if the integer entered is not 42. Here's an execution: what's your favorite number? 42 Here's another: what's your favorite number? 1 you don't like 42?!? Exercise. Write a program that prompts the user for an integer and prints c++: that's even. am i smart? if the integer entered is even. Here's an execution 100 3 of 21

c++: that's even. am i smart? Here's another 101 Exercise. Write a program that prompts the user for his/her age and prints c++: you're lying! think you can fool me??? if the value entered is less than 18. Here's an execution: enter your age: 16 c++: you're lying! think you can fool me??? Here's another: enter your age: 100 Of course this is another boolean expression: [var1] [bool op] [var2] Exercise. Write a program that prompts the user for two integer values and prints first is bigger is the first integer entered is larger than the second. Here's an execution of the program: 1 2 Here's another 2-1 first is bigger Here's yet another boolean expression: [expr] [bool op] [value] where [expr] is an expression. Exercise. Write a program that prompts the user for his/her height (in ft) and weight (in lbs) and print you will live more than 100 years!!! if the product of the height and weight is at least 200.45. (This is not supported by any form of research.) Here's an execution of the program: 6.1 180.180 you will live more than 100 years!!! Here's another 3.4 23.4 Exercise. Write a program that prompts the user for the year of his/her date of birth and the current year and print his/her approximate age and then print you must have watched lots of movies by now if the difference of the two values is at least 20. Here's an execution: 4 of 21

1900 2000 you are about 100 years old you must have watched lots of movies by now Here's another: 2000 2006 you are about 6 years old One last one... here's one more boolean expression: [expr1] [bool op] [expr2] Exercise. Write a program that prompts the user for the month, day of his/her date of birth and also his/her height (in ft) and weight (in lbs). Print the approximate number of days from January 1 of the year he/she was born and the sum of his/her height and weight. If the first printed number is greater than the second, print according to the theory of relativity, you are going to win the next powerball.. enter the month, day of your DOB: 1 1 enter your height and weight: 6 100 1 106 Here's another: enter the month, day of your DOB: 12 25 enter your height and weight: 6 200 355 206 according to the theory of relativity, you are going to win the next powerball. (You may assume the number of days in a month is 30.) Go ahead and try this: int numheads = 0; std::cout << "How many heads do you have? "; std::cin >> numhead; if (numheads == 2) std::cout << "Are you Zaphod?" << std::endl; And then this: int numheads = 0; std::cout << "How many heads do you have? "; std::cin >> numheads; if (numheads!= 2) std::cout << "You are not Zaphod" << std::endl; We can combine both features into one single program so that I get the following execution: How many heads do you have? 1 You are not Zaphod And here's an execution of the same program: How many heads do you have? 2 5 of 21

Are you Zaphod? Note that the code will look like this: int numheads = 0; std::cout << "How many heads do you have? "; std::cin >> numheads; if (numheads == 2) std::cout << "Are you Zaphod?" << std::endl; if (numheads!= 2) std::cout << "You are not Zaphod" << std::endl; Although it works THIS IS NOT THE WAY TO DO IT. Here's the correct program: int numheads = 0; std::cout << "How many heads do you have? "; std::cin >> numheads; if (numheads == 2) std::cout << "Are you Zaphod?" << std::endl; else std::cout << "You are not Zaphod" << std::endl; This is called the if-else statement. The else part is executed if the boolean condition of rthe if part is false. This is better for many reasons. One of which is that that computer need not compute the boolean value of (numhead2 == 2) and (numheads!= 2) which is TWO computations. The correct version computes only ONE boolean condition. There is a whole section on the if-else statement. So I don't want to go too much into this yet. Exercise. Modify the program so that, in addition to the above requirements, if the number of heads entered is greater than 2, the program prints "No kidding!". Finally, modify the program so that, in addition to the above, if the number of heads is 1, the program prints "Let me introduce you to our surgeons." Test your program by entering 0, 1, 2, 3. 6 of 21

Mental Picture: Flow of Execution for if The if statement is difficult for some beginning programmers because it alters the flow of execution of a program. Before this set of notes, C++ executes one statement at a time from top to bottom; it executes every statement. The if statement is different because it has a statement inside the if statement. This is an if-statement if (x > 0) std::cout << "spam!" << std::endl; std::cout << "eggs" << std::endl; The statement in the if statement is executed only when the x > 0 is true. If the condition is not true, the statement in the if statement is not executed. This is a statement inside the if-statement In the following diagram, you can follow the arrows to get a sense of the flow of execution: 7 of 21

if (x > 0) true false std::cout << "spam!" << std::endl; std::cout << "eggs" << std::endl; Of course the program is still executing one statement at a time from the top to the bottom if you view the if statement as a statement (i.e. forget about the internals of the if statement). 8 of 21

Some Gotchas Exercise. Try this: if x > 0 std::cout << "spam!" << std::endl; std::cout << "eggs" << std::endl; Fix it. Exercise. Try this: if (x > 0); std::cout << "spam!" << std::endl; std::cout << "eggs" << std::endl; What do you learn from this example? Exercise. Here's another wacky example... if (x > 0); std::cout << "eggs" << std::endl; 9 of 21

Multiplication Game: Part 1 Exercise. Write a program that tests if the user can do two digit multiplication. Right now we will just ask the one question: the product of 97 and 94. The program congratulates the user if the right answer is entered. Otherwise the program stops immediately. Here's an execution of the program: What is the product of 97 and 94? 1 Here's another execution of the program: What is the product of 97 and 94? 9118 You smart dawg! This is kind of a dumb program because it asks the same question again and again. Here's the pseudocode (pseudocode = informal description of steps in a program): Declare integer variable guess Print a prompt string to user Prompt user for integer value for guess If guess is 9118 print congratulatory message Note that this is not a C++ program yet!!! You can't run it!!! 10 of 21

Blocks What if you want to execute several statements when a boolean expression is true? Here's one way: if (x > 0) std::cout << "spam!" << std::endl; if (x > 0) std::cout << "ham!" << std::endl; std::cout << "eggs" << std::endl; Oh... by the way... that's the BAD way of executing two statements when the same condition holds. This is the right way: if (x > 0) std::cout << "spam!" << std::endl; std::cout << "ham!" << std::endl; std::cout << "eggs" << std::endl; If you compare the two version, you see immediately that the first version has two exact same boolean expressions that your computer must evaluate, i.e. (x > 0). In the second version, there's only one. That makes your computer happy. So don't write your code the first way. The... is called a block or block of statements. (You've actually seen this already. Look at your main() ) Exercise. Write program that prompts the user for his/her/its phone number (7 digit integer), declare a double variable prize and initialize it to 0. If the phone number entered is even, print it's your lucky day! and set prize to 1234.5678. Before the program ends, print the value of prize. Here's an execution of the program: 1234567 you won $0. Here's another: 1234568 it's your luck day! you won $1000. 11 of 21

Exercise. Can you also have a block of statements for the else part of an if-else statement? Modify the above program so that if the phone number if not even, print not your luck day! and set prize to -5432. 1234567 not your luck day! you won $-5432. Here's another: 1234568 it's your luck day! you won $1000. I've shown you how an if and the if-else statement can control the execution (or not) of not just one statement but a whole bunch (called a block): if (x > 0) std::cout << "spam!" << std::endl; std::cout << "ham!" << std::endl; std::cout << "eggs" << std::endl; But there's something else to a block besides bunching up statements for a single if statement... 12 of 21

Scopes and Blocks A variable has a name, a value, and you should know by now, a variable also has a scope. This refers to the place in your code where you can refer to that variable. Here's an old example (very old...): x = 42; // x not declared yet bozo!!! std::cout << x << std::endl; In general the scope of a variable is from the point of declaration to the end of the block where it is declared. During the execution of the program, when the point of execution exits that block, the variable is destroyed. Try this: #include <iostream> int main() if (x == 42) std::cout << "here we go..." << std::endl; int y = x + 1; std::cout << "y: " << y << std::endl; std::cout << "x: " << x << std::endl; Scope of y Scope of x return 0; Therefore this won't work (try it): #include <iostream> int main() if (x == 42) std::cout << "here we go..." << std::endl; int y = x + 1; std::cout << "y: " << y << std::endl; 13 of 21

std::cout << "y: " << y << std::endl; return 0; BAD!!! y is already destroyed!!! In this case, if you really, really need y after the if statement, then you need to declare y outside the if statement to enlarge the scope of y: #include <iostream> int main() int y = 0; if (x == 42) std::cout << "here we go..." << std::endl; y = x + 1; std::cout << "y: " << y << std::endl; std::cout << "y: " << y << std::endl; return 0; Phew!!! y is still alive!!! This is your first introduction to the concept of scopes, a very important concept in CS. (Actually this is also very important in Math too for instance in logic. But in logic it goes by a different name.) That's not too surprising since scope is more or less like the idea of context and that's an everyday concept. There's a complete of notes on scopes so we won't go into every aspect of scopes now. This is just a quick introduction to scopes to get you ready that that set of notes on scopes. 14 of 21

Random Integers Generating a random integer occurs commonly in computer programs. For instance if you want to write a strategy role-playing game, you might have different rooms in a mansion and you want to put different things in different rooms. Suppose you have a magic potion in a bottle. Now suppose the rooms are numbered from 1 to 1000. You don't want your magic potion bottle to be in room 5 whenever you start the game. You want each game to be different. At a more complex level you might want to randomly make doors between two rooms. This creates a random maze. So you'd better learn how to generate random things. Try this: #include <iostream> int main() std::cout << "the potion is in room " << rand() << std::endl; std::cout << "the potion is in room " << rand() << std::endl; std::cout << "the potion is in room " << rand() << std::endl; std::cout << "total number of rooms: " << RAND_MAX << std::end; return 0; (Note: You might need #include <cstdlib> just after #include <iostream>. This depends on your compiler.) Looks like you're getting random integers. But wait... run the program a second time... a third time... a fourth time. Notice something? OK. Let's modify the program: #include <iostream> #include <ctime> int main() srand((unsigned int) time(null)); std::cout << "the potion is in room " << rand() << std::endl; std::cout << "the potion is in room " << rand() << std::endl; std::cout << "the potion is in room " << rand() 15 of 21

<< std::endl; std::cout << "total number of rooms: " << RAND_MAX << std::end; return 0; Run this program several times. Notice that the room number is different each time you run the program. I will not go into details on the statement srand((unsigned int) time(null)); The only thing I will say is that it seeds the random generator so as to make it more random. The important thing to remember that you must execute this statement and furthermore you execute it once (usually) and before you call the rand() function. The standard practice is execution the seeding in main() and at the beginning: #include <iostream> #include <ctime> int main() srand((unsigned int) time(null));... return 0; Since rand() gives you a random number from 0 to RAND_MAX, rand() / RAND_MAX will give you a random double between 0.0 and 1.0. Well not quite don't forget that in C++ / is an integer division. So if you want to generate random doubles from 0.0 to 1.0, you should use this: double(rand()) / RAND_MAX Exercise. Check that I was not lying by printing 10 random doubles from 1.0 to 0.0. By the way, if the numbers are truly random, you would expect their average to be close to 0.5. Check that too. There are times when you need to generate a random integer between two bounds. For instance if you have a maze game and the rooms are numbered 1 to 100, you want a random room number for a magic potion. How many values are there from 1 to 100? You have 100. Since rand() gives you a random integer from 0 to RAND_MAX, if you do rand() % 100, you get 100 numbers, from 0 to 99. You add 1 to it and you get a 16 of 21

random integer from 1 to 100. In other words 1 + rand() % 100 does the trick. It's easy to test this: std::cout << 1 + rand() % 100 << std::endl; Copy and paste this 99 times. Run the program. You should get almost all the integers from 1 to 100. This is probabilistic, so your program will probably miss a few, i.e. there will be some repeats. Exercise. How do you generate random integers from 0,..., 5? Test it with a program by generating and printing 50 random integers in this range. Make sure nothing printed is outside the given range. Exercise. Write a show program that simulates the rolling of two dice and print you win! if the face values are both 6. Here's an execution: face values: 3 6 Here's another face values: 2 1 Here's another face values: 6 6 you win! :) (This program has not inputs since the values used for computation is generated automatically.) Exercise. Modify the above program so that it behaves like this Here's an execution: face values: 3 6 you lose :( Here's another face values: 2 1 you lose :( Here's another face values: 6 6 you win! :) Exercise. How do you generate random integers from 3,...10? Test it as above. Exercise. How do you generate random integers from -5,...5? Test it as above. 17 of 21

The Important Swap Trick Recall the following extremely important trick that you must know. It allows you to swap the values in two variables: int a = 42, b = 24; std::cout << a << ' ' << b << std::endl; int t = a; a = b; b = t; std::cout << a << ' ' << b << std::endl; Exercise. Write a program that declares two doubles initializing them with 1.234 and 4.567. Print the values of the variables. Swap their values. Print the values of the variables. 18 of 21

First Sorting Example Here's your first sorting problem: Prompt the user for two integer values and assignment them to x and y. Sort the values so that the value of x is less than or equal to the value of y Exercise. Write the above program. Here are some test cases: Test 1 1 2 1 2 Test 2 2 1 1 2 Test 3-1 5-1 5 Test 4 5-1 -1 5 Test 5 2 2 2 2 19 of 21

More Sorting Examples: Bubblesort Now suppose you have three integer variables and you want to sort the values in the variables. What I mean is this. Suppose your variables are x, y, and z. After some operations you want x <= y && y <= z to be true (Remember NOT x <= y <= z!!!!!!!!!!!) Here's the idea. First do this: Swap (if necessary) the values of x and y so that x <= y. Swap (if necessary) the values of y and z so that y <= z. This will guarantee that z is the largest. (Right?) Exercise. Take a piece of paper. And write down three integer values for x, y, and z and follow the above steps. Do you see that the largest will always be in variable z's box? Exercise. Does this means that the smallest will be in x? The above two steps is said to be one pass of this sorting process. Now do this: Swap (if necessary) the values of x and y so that x <= y. This will guarantee that the larger value between x and y will be in y. Now everything is in ascending order, i.e. x <= y && y <= z Altogether these are the steps: Pass 1: To get largest among x, y, z into z do this: Swap (if necessary) the values of x and y so that x <= y. Swap (if necessary) the values of y and z so that y <= z. Pass 2: To get largest among x, y into y Swap (if necessary) the values of x and y so that x <= y. Such a recipe (a finite number of steps to achieve a goal) is called an algorithm. This sorting algorithm is called bubblesort. There are actually many different sorting algorithms. Exercise. Write a program that prompts the user for three integers and prints the three integers in ascending order. Here are some executions: 1 2 3 1 2 3 20 of 21

1 3 2 1 2 3 2 1 3 1 2 3 2 3 1 1 2 3 3 1 2 1 2 3 3 2 1 1 2 3 Exercise. What if you have four variables a, b, c, and d? How would you take the values from a, b, c, d, sort them into ascending order, and put them into a, b, c, d? Exercise. Now, you have four variables a, b, c, d. You know that one pass of the bubblesort will put the largest value of a, b, c, d into d. What would you do if I ask you to write a program to prompt the user for four values and then print the second largest. Now write the program and test it. [Hint: You need two passes.] Exercise. Again suppose you have four variables a, b, c, d. Suppose during the second pass, you notice that no swap was performed. Do you see that you do not need to perform any more passes? 21 of 21