Logical Operators and if/else statement. If Statement. If/Else (4.3)

Similar documents
CS150 Introduction to Computer Science 1. Logical Operators and if/else statement

9/18/11. Type casting, logical operators and if/else statement. Explicit Type Conversion. Example of Type Casting

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

CSI33 Data Structures

Chapter 4: Making Decisions

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Chapter 4: Making Decisions

More Complex Versions of the if Statement. Class 13

LECTURE 04 MAKING DECISIONS

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Score score < score < score < 65 Score < 50

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

C/C++ Programming Lecture 7 Name:

Add Subtract Multiply Divide

5. Control Statements

Lecture 3 Tao Wang 1

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

CSCE 206: Structured Programming in C++

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

5. Selection: If and Switch Controls

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The following expression causes a divide by zero error:

Test Bank for Problem Solving with C++: The Object of Programming, 8/e Chapter 2 C++ Basics

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Decisions. Arizona State University 1

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

If Control Construct

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Chapter 4: Basic C Operators

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Control Structures in Java if-else and switch

Outline. Review Choice Statements. Review Sequential Flow. Review Choice Before Loops. Review Choice After Loops

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Chapter 2 - Control Structures

Introduction to Computer Science Midterm 3 Fall, Points

COMP Primitive and Class Types. Yi Hong May 14, 2015

Control Structures in Java if-else and switch

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Getting started with C++ (Part 2)

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

Lecture 5 Tao Wang 1

CS 103 Lecture 3 Slides

Increment and the While. Class 15

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

C++ PROGRAMMING SKILLS Part 2 Programming Structures

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Fundamental of Programming (C)

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Chapter 2: Overview of C++

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

To become familiar with array manipulation, searching, and sorting.

Chapter 4 - Notes Control Structures I (Selection)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

CS242 COMPUTER PROGRAMMING

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

Computer Programming I - Unit 5 Lecture page 1 of 14

Review of the C Programming Language for Principles of Operating Systems

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Starting Out With C++: Early Objects, Seventh Edition Solutions to End-of-Chapter Review Questions

CS 1428 Review. CS 2308 :: Spring 2016 Molly O Neil

Flow Control. CSC215 Lecture

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

REPETITION CONTROL STRUCTURE LOGO

bool bool - either true or false

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Maciej Sobieraj. Lecture 1

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Lecture 5: Making Decisions

Chapter Four: Loops II

Reviewing all Topics this term

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Checking Multiple Conditions

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

Transcription:

Logical Operators and if/ statement 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 2 If/Else (4.3) C++ provides a shortcut to combine two if statements: The statements in the clause are executed only when the expression is false. if( expression ) // do stuff // do other stuff 3

Q.2 Example int number; cout << Enter a number, I ll tell you ; cout << if it is odd: ; cin >> number; // use an if/ statement here 4 If/Else: Syntax and Formatting if(expression) // do stuff // do other stuff Note the braces with the keyword and the alignment of the under the if on its own line 5 If/Else: Braces if(expression) // do stuff x += 9; Always use braces with the! 6

If/Else: Commenting // the expression I m using here // checks for... if(expression) // if the expression is true // I need to... // if the expression is false // I need to... 7 Q.3 Practice Turn this code into an if/ statement: int x, y; if(x > y) x += y; if(y <= x) y += x; 8 Q.4 Practice Are these two code snippets equivalent? int x, y; if(x > y) x += y; if(x < y) y += x; int x, y; if(x > y) x += y; y += x; 9

if//if statements (4.4) What if there are more than two alternatives? if(expression1) statement1; if(expression2) statement2; default statement; 10 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 11 Q.6 Problem Write a program that displays a letter grade corresponding to an exam score 90-100 A 80-89 B 70-79 C 60-69 D 0-59 F 12

Nested if Statements (4.6) 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 and the second if if(x > y) if (x == 9) 13 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? 14 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 15

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 && o o! and or Not 16 Q.8 Examples of Logical Operators if((x > 7) && (x < 20)) if((temp > 90.0) && (humidity > 0.9)) if((salary < minsalary) (dependents > 5)) 17 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 18

Evaluating Expressions: Or (expr1 expr2) The complete expression is true if either expr1 or expr2 is true Examples: o (salary < minsalary) (dependents > 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 19 Evaluating Expressions: Not!!expr Unary operator Examples: o!((salary < minsalary) && (dependents > 5)) o What makes this true? False? 20 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 21

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 22 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? 23 Precedence Precedence Operators (Highest to Lowest) - (unary negation),! (Logical NOT) * / % - + <= => > < ==!= && = += -= *= /= %= 24

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 25 exit() To terminate a program we can use the exit(int status) function o This is a function, not part of the language #include<stdlib.h> o The status is returned to the operating system to denote program success or failure Success: 0 Failure: non-zero 26 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/ 27

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 0.66666 is equivalent to 2/3 o 6.0 * 0.66666 = o 6.0 * 0.66667 = o 6.0 * 0.666666 = o 6.0 * ( 2.0 / 3.0 ) = 28 Q.1 Example 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; 29 Example 30