Single selection syntax if ( expression ) { statements; } TRUE FALSE expression if clause 1. Statemens are executed when expression is true 2. { } can.

Slides:



Advertisements
Similar presentations
CSC 142 G 1 CSC 142 Conditionals [Reading: chapter 5]
Advertisements

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Logic & program control part 2: Simple selection structures.
Chapter 4: Making Decisions.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
1 CSC103: Introduction to Computer and Programming Lecture No 8.
If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 Chapter 5 Branching and Method Algorithm Design.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
CS 1400 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 4: Control Structures I (Selection)
Use Precedence Chart int number ; float x ; number ! = 0 && x < 1 / number / has highest priority < next priority != next priority && next priority What.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
1 Chapter 4 Selection and Encapsulation. 2 Chapter 4 Topics l Java Control Structures l boolean Data Type l Using Relational and Logical Operators in.
The If/Else Statement, Boolean Flags, and Menus Page 180
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems/Headington.
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.
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
Control Structures I (Selection)
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Beginning C for Engineers Fall 2005 Lecture 2 – Section 2 (9/7/05) Section 4 (9/8/05)
Conditions, Logical Expressions, and Selection Control Structures Sumber dari :
Chapter 4 Logical Expressions & If-Else. 2 Overview  More on Data Type bool u Using Relational & Logical Operators to Construct & Evaluate Logical Expressions.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions, logical expressions, and selection Introduction to control structures.
Flow of Control There are 5 general types of Java control structures: Sequence (by default) Selection (also called branch or decision) Loop (also called.
Lecture no 3 Control statements.
Flow of Control Part 1: Selection
Simple Control Structures IF, IF-ELSE statements in C.
CONTROLLING PROGRAM FLOW
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
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.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
1 Conditions, Logical Expressions, and Selection Control Structures.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Lesson thirteen Conditional Statement "if- else" ©
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection in C++ If statements. Control Structures Sequence Selection Repetition Module.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
C++ Programming Control Structures I (Selection).
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 5 Topics Data Type bool
The Ohio State University
Decisions Given hours worked and pay rate, calculate total pay
Conditionals & Boolean Expressions
Conditionals & Boolean Expressions
Decisions Given hours worked and pay rate, calculate total pay
Conditionals & Boolean Expressions
Conditionals & Boolean Expressions
Topics Data Type bool Using Relational and Logical Operators to Construct and Evaluate Logical Expressions If-Then-Else Statements If-Then Statements Nested.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Presentation transcript:

Single selection syntax if ( expression ) { statements; } TRUE FALSE expression if clause 1. Statemens are executed when expression is true 2. { } can be omitted if only one staement

if ( Expression ) StatementA else StatementB NOTE: StatementA and StatementB each can be a single statement, a null statement, or a block. Double selection Syntax

if... else provides two-way selection between executing one of 2 clauses (the if clause or the else clause) TRUE FALSE expression if clauseelse clause

Use of blocks recommended if ( Expression ) { } else { } “if clause” “else clause”

int carDoors, driverAge ; float premium, monthlyPayment ;... if ( (carDoors == 4 ) && (driverAge > 24) ) { premium = ; cout << “ LOW RISK “ ; } else { premium = ; cout << “ HIGH RISK ” ; } monthlyPayment = premium / ;

What happens if you omit braces? if ( (carDoors == 4 ) && (driverAge > 24) ) premium = ; cout << “ LOW RISK “ ; else premium = ; cout << “ HIGH RISK ” ; monthlyPayment = premium / ; COMPILE ERROR OCCURS. The “if clause” is the single statement following the if.

Braces can only be omitted when each clause is a single statement if ( lastInitial <= ‘K’ ) volume = 1; else volume = 2; cout << “Look it up in volume # “ << volume << “ of NYC phone book”;

If-Else for a mail order Assign value.25 to discountRate and assign value to shipCost if purchase is over Otherwise, assign value.15 to discountRate and assign value 5.00 to shipCost Either way, calculate totalBill

These braces cannot be omitted if ( purchase > ) { discountRate =.25 ; shipCost = ; } else { discountRate =.15 ; shipCost = 5.00 ; } totalBill = purchase * (1.0 - discountRate) + shipCost ;

Terminating your program int number ; cout << “Enter a non-zero number ” ; cin >> number ; if (number == 0 ) { cout << “Bad input. Program terminated ”; return 1 ; } // otherwise continue processing

if (number == 0 ) if ( ! number ) {... } Each expression is only true when number has value 0. These are equivalent. Why?

Write If-Then or If-Then-Else for each If taxCode is ‘T’, increase price by adding taxRate times price to it. If code has value 1, read values for income and taxRate from user, and calculate and display taxDue as their product. If A is strictly between 0 and 5, set B equal to 1/A, otherwise set B equal to A.

Some Answers if ( code == 1) { cin >> income >> taxRate; taxDue = income * taxRate; cout << taxDue; } if (taxCode == ‘T’) price = price + taxRate * price if ( ( A > 0 ) && (A < 5) ) B = 1/A; else B = A;

What output? and Why? int age; age = 20; if ( age = 16 ) { cout << “Did you get driver’s license?” ; }

What output? and Why? int age; age = 30; if ( age < 18 ) cout << “Do you drive?”; cout << “Too young to vote”;

What output? and Why? int code; code = 0; if ( ! code ) cout << “Yesterday”; else cout << “Tomorrow”;