CS 1400 Jan 2007 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
If Statements & Relational Operators Programming.
Advertisements

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.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
CS Sept 2006 Pick ups from chapters 4 and 5.
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.
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.
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:
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.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
If Statements & Relational Operators, Part 2 Programming.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Switch Statements. Switch Statement Often you want to do a series of tests –if i==0 … else if i==1 …. else if i==2 … else if i==3 …. C++ provides the.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
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.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Chapter 4: Making Decisions. Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.1 Relational Operators Relational operators allow you to compare.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Fundamental Programming: Fundamental Programming Introduction to C++
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.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
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.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
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,
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
The Ohio State University
Introduction to Computer Programming
Chapter 3 Selection Statements
Chapter 3 Control Statements
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
Introduction to C++ October 2, 2017.
Chapter 4: Making Decisions.
Counting Loops.
Chapter 7 Conditional Statements
Summary Two basic concepts: variables and assignments Basic types:
Life is Full of Alternatives
Types, Truth, and Expressions
Presentation transcript:

CS 1400 Jan 2007 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 = true; What is truth? In C++, true is non-zero, false is zero

Condition statements General forms if (expr) { statements } else { statements }

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

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

Now divide the remainder two parts (B’s and non B’s) if (grade >= 80) {cout << “B”; } else { } grade must be C or below

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

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!