Making Decisions (True or False) Relational Operators >greater than <less than >=greater than or equal to <=less than or equal to ==equal to !=not equal.

Slides:



Advertisements
Similar presentations
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Advertisements

Chapter 4 - Control Statements
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
CS 1400 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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
Chapter 4 Making Decisions
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
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.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Computer Science Department Relational Operators And Decisions.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 02 (Part III) Introduction to C++ Programming.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
CONTROLLING PROGRAM FLOW
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Operations Lecture 9.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
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,
Decision Statements, Short- Circuit Evaluation, Errors.
Controlling Program Flow with Decision Structures.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CSE202: Lecture 5The Ohio State University1 Selection Structures.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Rational Expressions relational operators logical operators order of precedence.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Basic concepts of C++ Presented by Prof. Satyajit De
Selection (also known as Branching) Jumail Bin Taliba by
Bill Tucker Austin Community College COSC 1315
Chapter 4: Making Decisions.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
Conditions and Ifs BIS1523 – Lecture 8.
Introduction to C++ Programming
Topics 4.1 Relational Operators 4.2 The if Statement
Relational Operators.
Presentation transcript:

Making Decisions (True or False) Relational Operators >greater than <less than >=greater than or equal to <=less than or equal to ==equal to !=not equal to Note: Remember to put spaces around operators for readability.

Boolean Expressions Given: a = 4, b = -3, c = 0, d = 8 Evaluate the following boolean expressions as True or False Ex. a < b means is 4 < -3 and the answer is false a – b <= d – 1? a + b > c? d/4 != c? d – 2*a == c?

Comparison of non-integer Numbers Remember that floating point numbers may not be stored exactly. Avoid comparing the EQUALITY of two non-integer expressions. Use = where feasible.

Value of True & False The boolean value ‘true’ is stored as 1. The boolean value ‘false’ is stored as 0. Boolean expressions evaluate to either ‘true’ or ‘false’ (i.e. 1 or 0) ONLY.

Single-sided alternative (if… ) Syntax: if (test condition) { stuff to execute if test condition is true } OR (if there is only one statement to execute) if (test condition) statement to execute; Note: The body of the single-sided alternative is indented. As a matter of preference braces {} may be used even if there is only one statement.

Examples int x, y, z; cout << “Enter three integers: “; cin >> x >> y >> z; if (x != 0) z = y / x; ________________________________________ (What is wrong here?) What about ? if (x = 0) cout >> “You cannot divide by 0”;

Double Sided Alternative if (test condition) { statements to execute if the test condition evaluates to true } else { statements to execute if the test condition evaluates to false } or if (test condition) statement to execute (true leg); else statement to execute (false leg); Note: DO NOT mix {} and no {} even if one side of the double- sided alternative has only one statement. This is a style requirement!

More Truth true and false are boolean values and bool variables can be initialed Ex. bool ok; ok = false; A relational value that is true has value 1 A relational value that is false has value 0 In if (test condition) structures, if the test condition evaluates to anything other than 0, it is considered true for decision making purposes. Because real numbers are not necessarily stored exactly you should never use == to compare two floats, two doubles, etc.

File Open Errors Filestream objects can be tested like a Boolean expression. This fact can be used to make sure that a file is actually opened successfully before the program tries to use its contents. ifstream fin; fin.open(“inputfile.txt”); if (fin) { process the file as the open was successful } else { display a message that the input file failed to open }

Nested if Statements Where appropriate, ‘if’ statements of all kinds can be nested inside another ‘if’ statement. Note the indentation used in the syntax example. Syntax: if (test condition 1) { if (test condition2) { … } else { … }

Careful! Indentation becomes even more critical Braces MUST be paired correctly Can become confusing so documentation of logic may be needed Check to see if multi-layered embedded ‘if’ statements can be simplified with an if … else if …. else structure (next slide)

if … else if … else if … else This structure may be useful if there are a series of conditions to be met. Note that it is just a cleaner appearing permutation of nested ifs. Syntax: if (test condition 1) { … statement(s) to be executed if test condition 1 is true } else if (test condition 2) { … statement(s) to be executed if test condition 1 is false and test condition 2 is true } else if (test condition 3) { … statement(s) to be executed if all previous test conditions are false and test condition 3 is true } else//optional { …statement(s) to be executed if all previous test conditions are false }

Boolean Flags A ‘flag’ is a value that is used to signal when a particular condition occurs in a program. Typically a flag is a boolean (bool) variable or named constant A flag can also be of type int since 0 is false and all other int values are considered true