The If/Else Statement, Boolean Flags, and Menus Page 180

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CS150 Introduction to Computer Science 1
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Computer Science 1620 Programming & Problem Solving.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection Structures (if & switch statements) (CS1123)
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 CS161 Introduction to Computer Science Topic #8.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 3 Selection Statements
Chapter 3 Control Statements
Programming Fundamentals
CSC115 Introduction to Computer Programming
Bools & Ifs.
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Life is Full of Alternatives
Understanding Conditions
Branching statements Kingdom of Saudi Arabia
Life is Full of Alternatives Part 3
Presentation transcript:

The If/Else Statement, Boolean Flags, and Menus Page 180 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Formally defined if( expression ) { statement 1; statement 2; . . . statement n; } Just like a function, start at the top and execute in order to the bottom What is an expression? 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 More on Truth Expressions that evaluate to non-zero are considered true int x=5, y=0; // what will get executed? if ( x + y) { cout << “x+y is True” << endl; } if ( y ) cout << “y is True” << endl; 9/18/06 CS150 Introduction to Computer Science 1

Floating Point and Relational Operators Floating point math may not work out as you expect because of round off errors. Math Class: 6 * 2/3 = 4 C++: 6.0 * 0.66666 = 6.0 * 0.66667 = 6.0 * 0.666666 = 6.0 * ( 2.0 / 3.0 ) = 3.99996 4.00002 4.0 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example (page 180) double result; result = 6.0 * 0.666666; if ( result == 4.0 ) { cout << “result == 4.0” << endl; } cout << setprecision(6) << fixed; cout << result << endl; cout << setprecision(2) << result; cout << endl; 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example 9/18/06 CS150 Introduction to Computer Science 1

Floating Points in Relational Operators How can we get around this problem? 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Boolean Flags We have seen how to store the value of a relational expression to a bool variable. bool bIsSquare = ( length == width); if ( bIsSquare ) { } Why would you want to do this? Why not use the relational expression directly? 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Boolean Flags This use of a bool variable is called a flag. It is used to keep track of a condition so that the expression is evaluated only once 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 If Statement We may want to execute some code if an expression is true, and execute some other code when the expression is false. This can be done with two if statements… if( value >= LIMIT ) { // do something } if( value < LIMIT ) { // do something else 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 If/Else C++ provides a shortcut to combine two if statements: if( expression ) { // do stuff } else // do other stuff The statements in the else clause are executed only when the expression is false. 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example int number; cout << “Enter a number, I’ll tell you“; cout << “ if it is odd: “; cin >> number; // use an if/else statement here 9/18/06 CS150 Introduction to Computer Science 1

If/Else: Syntax and Formatting if( expression ) { // do stuff } else // do other stuff Note the braces with the else keyword and the alignment of the else under the if on its own line 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 If/Else: Braces if( expression ) { // do stuff } else x += 9; Always use braces with the else! 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 If/Else: Commenting // the expression I’m using here // checks for . . . if( expression ) { // if the expression is true // I need to ... } else // if the expression is false 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Practice Write a program that asks the user for two integers and output the floating point number produced by dividing the second number into the first. Be sure not to divide by zero! 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Practice Turn this code into an if/else statement: int x, y; if ( x > y ) { x += y; } if ( y <= x) y += x; 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Practice Are these two code snippets equivalent? int x, y; if ( x > y ) { x += y; } if ( y < x) y += x; else 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Nested Ifs The second if is only executed if the first if conditional is false Note the indentation of the inner if There may be code between the { with the first else and the second if if ( x > y ) { } else if ( x == 9 ) 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Using nested ifs… Write a snippet of code that will: add y to x if x > y add x to y if y > x add 1 to x if x == y int x, y; if ( If ( x > y ) { x += y; } Else { if ( x < y ) y +=x; x += 1; 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 C++ Shortcut if ( x > y ) { // do A } else if ( x == 9 ) // do B // do C if ( x > y ) { // do A } else if ( x == 9 ) // do B else // do C 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Using nested ifs… Write a snippet of code that will: add y to x if x > y add x to y if y > x add 1 to x if x == y int x, y; if ( If ( x > y ) { x += y; } Else { if ( x < y ) y +=x; x += 1; 9/18/06 CS150 Introduction to Computer Science 1

Chained If/Else statements if ( x > y ) { // do A } else if ( x == 9 ) // do B else if ( y > 1 ) // do C else 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Using nested ifs… Write a snippet of code that will only do one of the following: add y to x if x == y add x to y if y > x add 1 to x if (2 * x) == y int x = 5, y = 10; if ( If ( x > y ) { x += y; } Else { if ( x < y ) y +=x; x += 1; 9/18/06 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Exercise Write a snippet of code that will print the letter grade (A,B,C,D,F) of a student’s exam and set a bool flag to track if it is a passing grade. int examScore; // from 0 to 100 bool bPassingGrade; if ( 9/18/06 CS150 Introduction to Computer Science 1