1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.

Slides:



Advertisements
Similar presentations
Chapter 4: Making Decisions.
Advertisements

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
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/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Computer Science 1620 Programming & Problem Solving.
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!=
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection Structures (if & switch statements) (CS1123)
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
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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
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.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
1 CS161 Introduction to Computer Science Topic #8.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
CPS120: Introduction to Computer Science Decision Making in Programs.
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 3 Selection Statements
Chapter 3 Control Statements
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 4: Making Decisions.
If statement.
Summary Two basic concepts: variables and assignments Basic types:
Let’s all Repeat Together
Life is Full of Alternatives
Life is Full of Alternatives
Life is Full of Alternatives
Life is Full of Alternatives Part 3
Presentation transcript:

1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement

2 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/26/08CS150 Introduction to Computer Science 1

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

4 Q.2 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/26/08CS150 Introduction to Computer Science 1

5 If/Else: Syntax and Formatting 9/26/08CS150 Introduction to Computer Science 1 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

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

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

8 Q.3 Practice  Turn this code into an if/else statement: 9/26/08CS150 Introduction to Computer Science 1 int x, y; if(x > y) { x += y; } if(y <= x) { y += x; }

9 Q.4 Practice  Are these two code snippets equivalent? int x, y; if(x > y) { x += y; } if(y < x) { y += x; } int x, y; if(x > y) { x += y; } else { y += x; } 9/26/08CS150 Introduction to Computer Science 1

10 if/else/if statements (4.4)  What if there are more than two alternatives? if(expression1) { statement1; } else if(expression2) { statement2; } else { default statement; } 9/26/08CS150 Introduction to Computer Science 1

11 Q.5 Problem  Write a C++ program segment that allows the user the ability to input an integer from the keyboard. If the integer is positive, increment a variable poscount by 1. If the integer is negative, increment a variable negcount by 1. If neither, increment zerocount by 1 9/26/08CS150 Introduction to Computer Science 1

12 Q.6 Problem  Write a program that displays a letter grade corresponding to an exam score A B C D F 9/26/08CS150 Introduction to Computer Science 1

13 Nested if Statements (4.6) 9/26/08CS150 Introduction to Computer Science 1 if(x > y) { } else { if (x == 9) { } else { }  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

14 Q.7 Example  Write nested if statements that perform the following test: o If amount1 is greater than 10 and amount2 is less than 100, display the greater of the two  Can you write the solution to the above problem without nested if statements? 9/26/08CS150 Introduction to Computer Science 1

15 Using nested ifs  Write a snippet of code that will do all of the following, where x and y are integers: o add y to x if x == y o add x to y if y > x o add 1 to x if (2 * y) == x 9/26/08CS150 Introduction to Computer Science 1

16 Logical Operators (4.7)  If we want to check for more than one condition then we need to use logical operators  These combine logical expressions (i.e. expressions that have a true/false value)  There are three logical operators o &&and o ||or o !Not 9/26/08CS150 Introduction to Computer Science 1

17 Q.8 Examples of Logical Operators  if((x > 7) && (x < 20))  if((temp > 90.0) && (humidity > 0.9))  if((salary 5)) 9/26/08CS150 Introduction to Computer Science 1

18 Evaluating Expressions: And &&  (expr1) && (expr2)  For the complete expression to be true, both expr1 and expr2 have to be true  Example: (temp > 90.0) && (humidity > 0.9) o These are unbearable heat and humidity conditions o Both must be true for the entire expression to be true 9/26/08CS150 Introduction to Computer Science 1

19 Evaluating Expressions: Or ||  ( expr1 || expr2 )  The complete expression is true if either expr1 or expr2 is true  Examples: o (salary 5) o To qualify for financial aid, salary has to be less than some minimum salary or the number of dependents is greater than 5 o Only one condition has to be true 9/26/08CS150 Introduction to Computer Science 1

20 Evaluating Expressions: Not !  !expr  Unary operator  Examples: o !((salary 5)) o What makes this true? False? 9/26/08CS150 Introduction to Computer Science 1

21 Q.9 Example  Your local bookstore has asked you to write a program to help them determine the cost of shipping of customers orders. If the order is $30 or less then shipping will cost $5, if the order is over $30 then shipping will be $3 9/26/08CS150 Introduction to Computer Science 1

22 Q.10 Problem  The bookstore has now changed it’s shipping policy so that o If the order is $30 or less, shipping is $5 o If the order is over $30 but less than $50, shipping is $3 o If the order is over $50 then shipping is $2 9/26/08CS150 Introduction to Computer Science 1

23 Operator Precedence  We have now added relational, equality and logical operators to the mathematical operators that were introduced last week  Where do the new operators fit in the precedence table? 9/26/08CS150 Introduction to Computer Science 1

24 Precedence Precedence Operators (Highest to Lowest) - (unary negation), ! (Logical NOT) * / % - + > < == != && || = += -= *= /= %= 9/26/08CS150 Introduction to Computer Science 1

25 Q.11 Expression Evaluation  According to the operator precedence and associativity rules given on the previous slide, how will the following expressions be evaluated? o x < min + max o min <= x && x <= max o !x == y + 2 o x = a + b % 7 * 2 9/26/08CS150 Introduction to Computer Science 1

26 exit()  To terminate a program we can use the exit(int status) function o This is a function, not part of the language  #include o The status is returned to the operating system to denote program success or failure  Success: 0  Failure: non-zero 9/26/08CS150 Introduction to Computer Science 1

27 Q.12 Practice  Write a complete program that will ask the user for two integers. Display both integers to the screen if they are each greater than 1000 and terminate the program with exit() otherwise. Use exactly one if/else 9/26/08CS150 Introduction to Computer Science 1

28 Floating Point and Relational Operators  Floating point math may not work out as you expect because of round off errors.  In Math o 6 * 2/3 = 4  In C++, where is equivalent to 2/3 o 6.0 * = o 6.0 * = o 6.0 * = o 6.0 * ( 2.0 / 3.0 ) = 9/26/08CS150 Introduction to Computer Science 1

29 Q.1 Example 9/26/08CS150 Introduction to Computer Science 1 double result; result = 6.0 * ; if(result == 4.0) { cout << “result == 4.0” << endl; } cout << setprecision(6) << fixed; cout << result << endl; cout << setprecision(2) << result; cout << endl;

30 Example 9/26/08CS150 Introduction to Computer Science 1