Life is Full of Alternatives

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.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
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.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CPS120: Introduction to Computer Science Operations Lecture 9.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
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.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
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 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Bill Tucker Austin Community College COSC 1315
The Ohio State University
Introduction to Computer Programming
Chapter 3 Selection Statements
Chapter 3 Control Statements
What Actions Do We Have Part 1
Bill Tucker Austin Community College COSC 1315
Computing Fundamentals
for Repetition Structures
Computing Fundamentals
Data Types, Identifiers, and Expressions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Introduction to C++ October 2, 2017.
Conditionals & Boolean Expressions
Introduction to C++ Programming
Programming Funamental slides
Flow Control Statements
Summary Two basic concepts: variables and assignments Basic types:
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Decision I (if Statement)
CS150 Introduction to Computer Science 1
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Life is Full of Alternatives
Let’s all Repeat Together
Understanding Conditions
Arithmetic Operations
Summary of what we learned yesterday
What Actions Do We Have Part 1
Capitolo 1 – Introduction C++ Programming
Life is Full of Alternatives
COMS 261 Computer Science I
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Life is Full of Alternatives Part 3
Presentation transcript:

Life is Full of Alternatives 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last Time So far we have learnt the basic statements in C++. These are Input/output statements Assignment statements Arithmetic statements When we compiled and ran our programs all the statements were executed in sequential order. In other words, the statements were executed in order, one after the other. These are called sequential structures 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Today Today we will begin examining how C++ statements can be executed out of sequence, and some statements could be skipped altogether Specifically, we will be looking at selection structures 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 UML Activity Diagrams UML: Unified Modelling Language Used to represent algorithms that will later be translated into code Give the programmer a visual representation of the solution to a problem Can also help the programmer see a solution to a problem 09/13/04 CS150 Introduction to Computer Science 1

Example Write a program that will read in the current temperature in degrees Fahrenheit and convert it into Celsius display message asking user for temperature in Fahrenheit read in temperature and store it convert temperature to Celsius display temperature in Celsius 09/13/04 CS150 Introduction to Computer Science 1

if Selection Structure The if selection structure allows a program to make a decision based on the truth or falsity of some condition The format is if (some condition is true) execute some statement(s) Example if (weight > 100.0) shipCost = 10.00; 09/13/04 CS150 Introduction to Computer Science 1

if Selection Structure If the condition in the if selection structure evaluates to false, then the statement following the if will be skipped if( grade > 59 ) cout << “passed!”; “passed!” will only be output if the grade is greater than 59. If the grade is 59 or less the passed will not be output and the program will continue 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 UML Activity Diagram if( grade > 59 ) cout << “passed!”; [grade > 59] output “passed!” [grade <= 59] 09/13/04 CS150 Introduction to Computer Science 1

Equality and Relational Operators Conditions in if selection structures are formed using equality and relational operators < less than relational > greater than relational <= less than or equal to relational >= greater than or equal to relational == equal to equality != equal to equality 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Examples if( height >= 6.2 ) if( age == 18 ) Notice the difference between == and = == is only used to compare two values = is used to assign the value to right into the variable on the left 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example Your local bookstore has asked you to write a program to help them determine the cost of shipping of customers orders. If the order is $30 or less then shipping will cost $5, if the order is over $30 then shipping will be $3. Write an algorithm to solve this problem 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Solution #include <iostream> #include “stdafx.h” using namespace std; int main() { double order, shipping; cout << "Enter the total amount of the order: "; cin >> order; if( order <= 30 ) shipping = 5.00; if( order > 30 ) shipping = 3.00; cout << "The cost of shipping is $" << shipping << endl; return 0; } 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem The bookstore has now changed it’s shipping policy so that If the order is $30 or less, shipping is $5 If the order is over $30 but less than $50, shipping is $3 If the order is over $50 then shipping is $2 What would we need to change in the program? 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Logical Operators If we want to check for more than one condition then we need to use logical operators These combine logical expressions (i.e. expressions that have a true/false value) There are three logical operators && and || or ! Not 09/13/04 CS150 Introduction to Computer Science 1

Examples of Logical Operators if( ( x > 7 ) && ( x < 20 ) ) if( ( temp > 90.0 ) && ( humidity > 0.9 ) ) if( ( salary < minSalary ) || ( dependents > 5 ) ) 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Using logical expressions, how can we solve the bookstore problem The bookstore has now changed it’s shipping policy so that If the order is $30 or less, shipping is $5 If the order is over $30 but less than $50, shipping is $3 If the order is over $50 then shipping is $2 09/13/04 CS150 Introduction to Computer Science 1

Evaluating Expressions: And && (expr1) && (expr2) For the complete expression to be true, both expr1 and expr2 have to be true Example: (temp > 90.0) && (humidity > 0.9) These are unbearable heat and humidity conditions Both must be true for the entire expression to be true 09/13/04 CS150 Introduction to Computer Science 1

Evaluating Expressions: Or || (expr1 || expr2) The complete expression is true if either expr1 or expr2 is true Examples: (salary < minSalary) || (dependents > 5) To qualify for financial aid, salary has to be less than some minimum salary or the number of dependents is greater than 5 Only one condition has to be true 09/13/04 CS150 Introduction to Computer Science 1

Evaluating Expressions: Not ! Unary operator Examples: !((salary < minSalary) && (dependents > 5)) What makes this true? False? 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Operator Precedence We have now added relational, equality and logical operators to the mathematical operators that were introduced last week Where do the new operators fit in the precedence table? 09/13/04 CS150 Introduction to Computer Science 1

Operator Precedence & Associativity () L->R Parentheses !, +, - R->L Negation, Unary +, - *,/,% L->R Mult, div, mod +, - L->R Add, Subtract <<, >> L->R Insertion/extraction <, <=, >, >= L->R Relational ==, != L->R Equality && L->R And || L->R Or = R->L Assignment 09/13/04 CS150 Introduction to Computer Science 1

Expression Evaluation According to the operator precedence and associativity rules given on the previous slide, how will the following expressions be evaluated? x < min + max min <= x && x <= max !x == y + 2 x = a + b % 7 * 2 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 bool Data Type bool: boolean Variables of type bool can be either true or false They cannot be any other value Boolean variable names should start with b See coding standards Example bool bCanVote; int age; cin >> age; bCanVote = age >= 18; cout << bCanVote; 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Data Types So far we have been introduced to the following C++ data types int double char string bool 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Examples Assume that double x = 3.0; double y = 4.0; double z = 2.0; bool bFlag = false; What is the value of the following expressions !bFlag x + y/z <= 3.5 !bFlag || (y + z >= x - z) !(bFlag || (y + z >= x - z) 09/13/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we covered UML activity diagrams Simple if selection structure Relational and equality operators Logical operators bool data type Readings P. 34 - 39: simple if, equality and relational operators P. 71 - 77: if, UML, bool P. 124 - 128: logical operators, confusing = and == 09/13/04 CS150 Introduction to Computer Science 1