Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.

Slides:



Advertisements
Similar presentations
Chapter 4. Making Decisions
Advertisements

If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
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.
C++ Typecasting. Math Library Functions.. Operator / Operands A = x + y.
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.
Computer Science 1620 Programming & Problem Solving.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
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
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
What is the out put #include using namespace std; void main() { int i; for(i=1;i
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.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 4.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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.
Starting Out with C++: From Control Structures through Objects 7 th edition By Tony Gaddis Source Code Chapter 4.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CONTROLLING PROGRAM FLOW
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Control Structures Repetition or Iteration or Looping Part II.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Lecture 8: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Lecture 12: Functions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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,
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Lecture 14: Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Lecture 12: Functions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Chapter 4. Making Decisions
The Ohio State University
Chapter 3 Selection Statements
Chapter 3 Control Statements
CMPT 201 if-else statement
Introduction to C++ October 2, 2017.
Repetition Statements
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Let’s all Repeat Together
Life is Full of Alternatives
Programming Fundamental
Presentation transcript:

Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

Relational Operators Just like math operators allow you to perform math, relational operators allow you to compare or test numeric and char values and determine greater than less than equal to not equal to

> means greater than Example: x > y < means less than Example: x < y >= means greater than or equal to Example: x >= y <= means less than or equal to Example: x <= y == means equal to x == y != means not equal to x != y

The value of a relationship All expressions have a value, even relational ones Relational expressions are known as Boolean Expressions Their value can only be true (1) False (0)

// This program displays the values of true and false states. #include using namespace std; int main() { bool trueValue, falseValue; int x = 5, y = 10; trueValue = x < y; falseValue = y == x; cout << "True is " << trueValue << endl; cout << "False is " << falseValue << endl; return 0; }

The if statement The if statement can cause other statements to execute only under certain conditions This is known as a conditional General format If (expression) { statement; // as many as you need }

// This program averages three test scores #include using namespace std; int main() { int score1, score2, score3; // To hold three test scores double average; // TO hold the average score // Get the three test scores. cout << "Enter 3 test scores and I will average them: "; cin >> score1 >> score2 >> score3; // Calculate and display the average score. average = (score1 + score2 + score3) / 3.0; cout << fixed << showpoint << setprecision(1); cout << "Your average is " << average << endl; // If the average is greater than 95, congratulate the user. if (average > 95) cout << "Congratulations! That's a high score!\n"; return 0; }

Note on comparing floating point numbers You may find an error when comparing two floating point numbers In comparison, sometimes numbers are rounded Solution: Stick to using > or <

Rules of if Relational expressions that are true have a value of 1 Relational expressions that are false have a value of 0 Any expression that has a value of 0 is considered to be false by if Any expression that has a value greater than 0 is considered to be true by if

Flags and Tips for if Flags are boolean or integer variables that signal when a condition exist Example: bool highScore = false; Don’t confuse == with =

If/else If (expression) { statement; } else { statement; }

// This program averages 3 test scores. // It demonstrates an if statement executing // a block of statements. #include using namespace std; int main() { int score1, score2, score3; // To hold three test scores double average; // TO hold the average score // Get the three test scores. cout << "Enter 3 test scores and I will average them: "; cin >> score1 >> score2 >> score3; // Calculate and display the average score. average = (score1 + score2 + score3) / 3.0; cout << fixed << showpoint << setprecision(1); cout << "Your average is " << average << endl; // If the average is greater than 95, congratulate the user. if (average > 95) { cout << "Congratulations!\n"; cout << "That's a high score.\n"; cout << "You deserve a pat on the back!\n"; } return 0; }

// This program asks the user for two numbers, num1 and num2. // num1 is divided by num2 and the result is displayed. // Before the division operation, however, num2 is tested // for the value 0. If it contains 0, the division does not // take place. #include using namespace std; int main() { double num1, num2, quotient; // Get the first number. cout << "Enter a number: "; cin >> num1; // Get the second number. cout << "Enter another number: "; cin >> num2; // If num2 is not zero, perform the division. if (num2 == 0) { cout << "Division by zero is not possible.\n"; cout << "Please run the program again and enter\n"; cout << "a number other than zero.\n"; } else { quotient = num1 / num2; cout << "The quotient of " << num1 << " divided by "; cout<< num2 << " is " << quotient << ".\n"; } return 0; }