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.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
If Statements & Relational Operators Programming.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous.
Friday, December 15, 2006 “When you come to a fork in the road, take it.” - Yogi Berra.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
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,
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
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.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 4 Selection Structures: Making Decisions.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
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.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Fundamental Programming: Fundamental Programming Introduction to C++
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
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.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Chapter 4 Selection: the if-else and switch-case instructions.
Week 8: Decisions Bryan Burlingame 21 October 2015.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
1 SELECTION using IF and IF-ELSE Chapter 4. 2 Agenda Background  One Way selection (if) Two Way selection (if-else) Compound Statements Nested if-else.
Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang.
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
CPS120: Introduction to Computer Science Decision Making in Programs.
CONTROL STRUCTURES (SELECTION). PROGRAM COMPONENTS  SEQUENCE  Groups Of Sequential Steps  SELECTION  Making Choices  IF-THEN (one way)  IF-THEN-ELSE.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
The Ohio State University
Chapter 3 Selection Statements
Chapter 3 Control Statements
Data Types and Expressions
Data Types and Expressions
Selection (also known as Branching) Jumail Bin Taliba by
A mechanism for deciding whether an action should be taken
Factoring if/else code
Flow Control Statements
Selection Control Structure
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Types, Truth, and Expressions
Presentation transcript:

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 the Cat. - LEWIS CARROL, Alice in Wonderland.

§Concept of true and false §true is any value other than zero §false is zero

bool b=true; cout<<b<<endl; b=false; cout<<b<<endl; //zero is false and non-zero is true b=35; cout<<b<<endl; b=-45; cout<<b<<endl; b=0; cout<<b<<endl;

bool b=true; cout<<b<<endl; //prints 1 b=false; cout<<b<<endl; //prints 0 //zero is false and non-zero is true b=35; cout<<b<<endl; //prints 1 b=-45; cout<<b<<endl; //prints 1 b=0; cout<<b<<endl; //prints 0

Relational Operators > >= < <= = != Logical Operators && | !--

Relational expressions int p=5, q=2; bool bl; bl = true; cout<<bl<<endl; bl = (p<=q); cout<<bl<<endl; bl=!bl; cout<<bl<<endl;

Relational expressions int p=5, q=2; bool bl; bl = true; cout<<bl<<endl; //prints 1 bl = (p<=q); cout<<bl<<endl; //prints 0 bl=!bl; cout<<bl<<endl; //prints 1

Boolean expressions §Truth tables

Simple Flow of Control §Flow of control l The order in which statements are executed §Branch l Lets program choose between two alternatives

If: selective execution §The "if" statement allows us to execute a piece of code or not, based on the value of the expression we pass to it.  Pseudocode: if( ) { }  If is TRUE (non-zero), then are executed.  If is FALSE (zero), then are not executed.

If: selective execution int main () { int age; cout << "Please enter your age: "; cin >> age; if(age <= 0) { cout 0\n"; return 0; } cout << "Your age is " << age << endl; return 0; }

If: selective execution int p=5; if (p) cout<<“done”;

Boolean expressions Difference between == and =

Boolean expressions Programming help:  Instead of x == 3 use 3 == x. Then, the compiler will catch the error if you write only one =

If: selective execution §If: choosing between two alternatives  Pseudocode: if( ) { } else { }  If is TRUE (non-zero), then are executed.  If is FALSE (zero), then are executed.

Example gross_pay = rate * * rate * (hours - 40);

Branch Example §To calculate hourly wages there are two choices l Regular time ( up to 40 hours) gross_pay = rate * hours; l Overtime ( over 40 hours) gross_pay = rate * * rate * (hours - 40); l The program must choose which of these expressions to use

Designing the Branch §Decide if (hours >40) is true l If it is true, then use gross_pay = rate * * rate * (hours - 40); l If it is not true, then use gross_pay = rate * hours;

Implementing the Branch §if-else statement is used in C++ to perform a branch if (hours > 40) gross_pay = rate * * rate * (hours - 40); else gross_pay = rate * hours;

Boolean expressions Logical AND is represented by && (score >= 0) && (score <= 10) Logical OR is represented by || (score 10)

Boolean expressions Logical AND is represented by && (score >= 0) && (score <= 10) is true if score is between 0 and 10 (including 0 and 10). Logical OR is represented by || (score 10)

Boolean expressions Logical AND is represented by && (score >= 0) && (score <= 10) is true if score is between 0 and 10 (including 0 and 10). Logical OR is represented by || (score 10) is true exactly in the cases where the previous condition would be false.

Boolean expressions  Logical NOT is represented by an exclamation mark: ! (score 10) is equivalent to ! ( (score >= 0) && (score <= 10) )  Pitfall: & and | are also operators in C++. However, they do something different!

Boolean expressions Pitfall: comparison operator for equality is = = x == y is true if x has the same value as y. x = y on the other hand, assigns the value of y to the variable x.

int x=2, y=9; cout<< (x==y) <<endl; cout<< (x=y) <<endl;

int x=2, y=9; cout<< (x==y) <<endl; // prints 0 cout<< (x=y) <<endl; // prints 9

§Another example

Boolean expressions Pitfall: Be careful with precedences. When in doubt, use parentheses.  Relational and logical operators have lower precedence than arithmetic operators  15< is evaluated as 15<(100-90)  cout << (a && b); //parenthesis are necessary

Boolean expressions Pitfall: Be careful with precedences. When in doubt, use parentheses. int a=3, c=4; cout<<(a && c)<<endl; //parenthesis are necessary