CS 1400 Chapter 4, sections 1 - 6. Relational operators Less than< Greater than> Less than or equal<= Greater than or equal>= Equal== Not equal!=

Slides:



Advertisements
Similar presentations
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Advertisements

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.
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/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
CS Sept 2006 Pick ups from chapters 4 and 5.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
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!=
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
The If/Else Statement, Boolean Flags, and Menus Page 180
C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
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.
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.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
If Statements & Relational Operators, Part 2 Programming.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
Basic Of Computer Science
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Fundamental Programming: Fundamental Programming Introduction to C++
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
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
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.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Selection Structures: if and switch Statements. 2 Selection Statements –In this chapter we study statements that allow alternatives to straight sequential.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators.
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,
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Selection in C++ If statements. Control Structures Sequence Selection Repetition Module.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Decision making If.. else statement.
The Ohio State University
Bill Tucker Austin Community College COSC 1315
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
Chapter 4: Making Decisions.
Chapter 7 Conditional Statements
Decision making If statement.
Selection Control Structure
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Life is Full of Alternatives
CS2011 Introduction to Programming I Selections (I)
Selection Control Structure
Presentation transcript:

CS 1400 Chapter 4, sections 1 - 6

Relational operators Less than< Greater than> Less than or equal<= Greater than or equal>= Equal== Not equal!=

Relational expressions A comparison is a relational expression A simple relational expression compares two operands and is either true or false Examples cost < 56 time >= != age

bool variables A bool variable can hold true or false Examples: bool answer, flag; answer = age < 25; flag = cost == price;

What is “truth”? In C++: true is non-zero, false is zero The words true and false are actually reserved constants. bool valid, correct; valid = true; correct = false;

Condition statements Recommended forms if (rel-expr) { statements } else { statements }

Condition statements… Each condition statement divides a program into two separate paths –the true path –the false path (which may be absent) Only one of these two paths are taken!

Examples… Write a program to output ADULT if a person’s age is 21 or over. Write a program to output ADULT for 21 and over and MINOR otherwise. Write a program to output TWENTY-ONE if a person’s age is exactly 21.

What do braces mean? Braces are a way to associate a set of statements to be treated as one statement. –In other words, any place you can put one statement, you can put a set of statements in braces! A set of statements in braces is commonly referred to as a block.

Condition statements revisited… General forms if (rel-expr) statement statement else statement

Braces are not required for a single statement But an else is always associated with the closest possible if above it! Example: Skill level >1 is advanced, level >2 is advanced master, lower levels are beginners. if (skill > 1) cout << “advanced “; if (skill > 2) cout << “master”; else cout << “beginner”;

Solution – use braces! if (skill > 1) {cout << “advanced “; if (skill > 2) {cout << “master”; } else cout << “beginner”;

Nested conditions (if/else/if) Example: Output a letter grade based upon a test score –90 to 100 is an A –80 to below 90 is a B –70 to below 80 is a C –60 to below 70 is a D –below 60 is an F Solution: Divide the program into two parts Stepwise Decomposition!

cout << “Enter grade: “; cin >> grade; if (grade >= 90) {cout << “A”; } else { } grade must be B or below First, the A’s and non A’s // cloud of non A’s

Now divide the non-A’s into two parts (B’s and non B’s)… if (grade >= 80) {cout << “B”; } else { } grade must be C or below // cloud of non B’s

and so on… if (grade >= 70) {cout << “C”; } else { } grade must be D or below // cloud of non C’s

Finally, grade must be D or F if (grade >= 60) cout << “D”; else cout << “F”;

#include using namespace std; int main() {float grade; cout << "Enter grade: "; cin >> grade; if (grade >= 90) {cout << "A"; } else {if (grade >= 80) {cout << "B"; } else {if (grade >= 70) {cout << "C"; } else {if (grade >= 60) cout << "D"; else cout << "F"; }

#include using namespace std; int main() {float grade; cout << "Enter grade: "; cin >> grade; if (grade >= 90) cout << "A"; else if (grade >= 80) cout << "B"; else if (grade >= 70) cout << "C"; else if (grade >= 60) cout << "D"; else cout << "F"; } Braces are actually not needed for a single statement!

Examples… Input a grade and output “passing” if the grade is 60 or above. Input an age and output one of three clasifications: adult (21 to 64), minor (under 21), or senior (65 and over). Input a price and validate with the message “valid” if the price is between $1500 and $3500 (inclusive)

Question: What would the output be for inputs of 1, 2, and 3? int answer; cin >> answer; if (answer == 1) cout << “A “; if (answer < 3) cout << “B”; else cout << “C”;

Example; A person 15 years of age may acquire a driver’s lic. with permission from parents. A person over 15 is eligible without parental permission. A person under 15 is not eligible. Write a small program to input an age and output one of 3 messages: “permission required”, “eligible”, or “not eligible”.

Example; A person 15 years of age and female or 16 years of age and male may acquire a driver’s lic. with permission from parents. A person over 16 is eligible without parental permission. A person under 15 is not eligible.

Summary relational operators relational expressions bool variables truth condition statements braces and blocks stepwise decomposition