Control Structures: The IF statement!

Similar documents
C++ Programming: From Problem Analysis to Program Design, Third Edition

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

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

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Conditions, logical expressions, and selection. Introduction to control structures

Chapter 4 - Notes Control Structures I (Selection)

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

Chapter 4: Control Structures I (Selection)

If Control Construct

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

5. Control Statements

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

Text Input and Conditionals

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Checking Multiple Conditions

Unit 7. 'while' Loops

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

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

C++ Programming: From Problem Analysis to Program Design, Third Edition

Unit 3. Operators. School of Science and Technology INTRODUCTION

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

5. Selection: If and Switch Controls

Chapter 2, Part III Arithmetic Operators and Decision Making

Functions! Objectives! 1E3! Topic 9! programming! n This topic should allow students to! n Read chapter 6 of the textbook now.!

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Boolean Algebra Boolean Algebra

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

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

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

Chapter 3 Problem Solving and the Computer

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

REPETITION CONTROL STRUCTURE LOGO

Conditional Statement

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

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

Chapter 2. C++ Basics

Python for Informatics

Chapter 4. Computation. Bjarne Stroustrup.

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

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

Tail recursion. Decision. Assignment. Iteration

Lecture 3 Tao Wang 1

Lesson 39: Conditionals #3 (W11D4)

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Chapter 6. Decision and Control Statements

Chapter 7. Expressions and Assignment Statements ISBN

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

cs1114 REVIEW of details test closed laptop period

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

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Hacettepe University Computer Engineering Department. Programming in. BBM103 Introduction to Programming Lab 1 Week 4. Fall 2018

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

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CPS122 Lecture: From Python to Java

Chapter 7. Expressions and Assignment Statements

Operator Precedence Lab

UEE1302(1102) F10: Introduction to Computers and Programming

Full file at C How to Program, 6/e Multiple Choice Test Bank

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Expressions & Assignment Statements

Control Structures in Java if-else and switch

The following expression causes a divide by zero error:

CS313D: ADVANCED PROGRAMMING LANGUAGE

Flow Control. CSC215 Lecture

Language Reference Manual

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

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

Algorithms and Programming I. Lecture#12 Spring 2015

Boolean Logic & Branching Lab Conditional Tests

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Decisions: Logic Java Programming 2 Lesson 7

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

C/C++ Programming Lecture 7 Name:

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Chapter 7. Expressions and Assignment Statements ISBN

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Chapter 4 Computation

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

CS 106 Introduction to Computer Science I

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Information Science 1

conditional statements

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

Chapter 7. Expressions and Assignment Statements

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

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

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

CS111: PROGRAMMING LANGUAGE II

Chapter 4: Making Decisions

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

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Transcription:

Control Structures: The IF statement! 1E3! Topic 5! 5 IF 1 Objectives! n To learn when and how to use an IF statement.! n To be able to form Boolean (logical) expressions using relational operators! n <, <=, >, ==,!=,.! n To be able to combine Boolean expressions using logical operators!! n AND (&&), OR ( ), NOT (!)! 5 IF 2 1

Textbook! n Read chapter 4! n Ignore SWITCH structures; and ASSERT.! n Things you need to get from the textbook! n Comparison of characters and strings! 5 IF 3 Control Structures! n A computer can proceed:! n In sequence! n Selectively (branch) - making a choice! n Repetitively (iteratively) - looping! n Some statements are executed only if certain conditions are met! n A condition is represented by a logical (Boolean) expression that can be true or false! n A condition is met if it evaluates to true! 5 IF 4 2

5 IF 5 The IF-ELSE statement! The IF-ELSE statement allows you to do different things depending on a condition, (temp > 37.0) in this case First we need to understand how to write conditions. 5 IF 6 3

Conditions! n A condition is an expression whose value is TRUE or FALSE.! temp > 37!true if the value of the variable temp is greater than 37! n == 0!true if the value of n is equal to 0! count!= n!true if the value of count is NOT equal to the value of n! (price / area) >= 5.5!true if the value of price divided by the value of area is greater than or equal to 5.5! 5 IF 7 Logical (Boolean) Expressions! n The bool Data Type and Logical (Boolean) Expressions! n The data type bool has logical (Boolean) values true and false n bool, true, and false are reserved words! n The identifier true has the value 1! n The identifier false has the value 0 5 IF 8 4

Relational Operators! n Relational operators:! n Allow comparisons! n Require two operands (binary)! n Return 1 if expression is true, 0 otherwise! n Comparing values of different data types may produce unpredictable results! n For example, 8 < '5' should not be done! n Any nonzero value is treated as true n This is a hangover from old C days. Clearer to use bool true and false, and not 0 and 1.! 5 IF 9 5 IF 10 5

Logical (Boolean) Operators! n Logical (Boolean) operators enable you to combine logical expressions! n Three logical (Boolean) operators:!! - not! && and! - or! n Logical operators take logical values as operands and yield logical values as results! n Putting! in front of a logical expression reverses its value! 5 IF 11 n (x > 0) && (x <= 50)! && and! n True if x is both greater than 0 and less than or equal to 50! n NB (0 < x <= 50) won t work properly!!! n Always true!! n (x%4!= 0) (x < 1538)! n True if x is not divisible by 4 or if x is less than 1538, or if both are true.! n Note x%4 is the remainder from dividing x by 4. If it s not equal to 0, x is not divisible by 0.! 5 IF 12 6

Precedence of Operators! n Relational and logical operators are evaluated from left to right! n The associativity is left to right! n Parentheses can override precedence! 5 IF 13 5 IF 14 7

int x = 10, y = 15, z = 20; Which is? A.!(x > 10) B. x <= 5 y <= 15 C. (x!= 5) && (y!= z) D. x >= z (x + y >= z) E. (x <= y- 2) && (y >= z) (z- 2 ==20) Note: textbook ch4 ex 3(e) answer is wrong Logical expressions! n (a + 2 <= b) &&!flag! n If a is 5 and b is 8 and flag is false what is the value of the above condition?! n Study Example 4.5 in the textbook.! 5 IF 16 8

Short-Circuit Evaluation! n Evaluation of a logical expression stops as soon as the value of the expression is known! n If the first part of an && is false, the whole lot must be false! n If the first part of an is true, the whole lot is true! 5 IF 17 n Example: Short-circuit - Example! (x!= 0) && (y/x > 4) If x is 0, (x!=0) is false, and the AND can t be true. The expression is false. There is no need to evaluate the second part (y/x > 4). So the (y/x) is not evaluated. This is handy because dividing by zero would be an error. 5 IF 18 9

IF branching! temp > 37 yes no cout << fever cout << fine 5 IF 19 IF-ELSE syntax! if ( condition )" what to do if true;! else what to do if false;!! This version is for when you only need one statement in each branch. if ( condition )" { what to do if true }! else! { what to do if false }!! Otherwise use {} braces to group together the multiple statements 5 IF 20 10

Example that needs {}! Suppose we want to do more things if someone has a fever: (see temp-if-multiple.cpp) The { } braces group together multiple statements to form the what to do if true part. 5 IF 21 Which numbers produce 1 3? if (n % m ==3) { n = 3; m = 1;} else if (n % m == 2) { n = 2; m = 2;} else {n = 1; m = 3;} cout << n << << m; A. n = 33, m = 10 B. n = 12, m = 5 C. n = 30, m = 10 D. n = 10, m = 4 11

Let s try! n Write a program to compute employee s PRSI given that (fictitious rates)! n Rate is 2% if weekly pay <= 256 euro! n Rate is 5% if weekly pay > 256 euro! n Use a variable to store the rate.! n Assign the rate value inside an IF statement.! 5 IF 23 IF Syntax! n Note that the condition must be in ( ) parentheses.! n Note that there is no ; after the condition.! n You can leave out the ELSE part if you don t want to do anything in that branch.! if (yards!= 0 ) cout << yards << yards. ; if (feet!= 0 ) cout << feet << feet. ; 5 IF 24 12

Exercises! n Write the statements that tell the user which pizza is better value, given that ppsi1 and ppsi2 have been computed.! n Handle case where both pizzas are equal value.! n Extend the quadratic roots program to handle a=0, discriminant = 0, or < 0.! n Write a program to determine if a year is a leap year.! n Note that since 1538, a year divisible by 100 is not a leap year, unless it is divisible by 400.! 5 IF 25 13