Life is Full of Alternatives

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
Chapter 6 Control Structures.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
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 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
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.
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.
Chapter 4 Selection Structures: Making Decisions.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
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.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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.
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,
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
 Type Called bool  Bool has only two possible values: True and False.
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
Computing and Statistical Data Analysis Lecture 2
Bill Tucker Austin Community College COSC 1315
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.
Computing Fundamentals
for Repetition Structures
A mechanism for deciding whether an action should be taken
Introduction to C++ October 2, 2017.
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Chapter 4: Control Structures I (Selection)
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
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
CS150 Introduction to Computer Science 1
Understanding Conditions
Arithmetic Operations
do/while Selection Structure
What Actions Do We Have Part 1
Life is Full of Alternatives
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 9/16/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last time we Introduced the idea of selection structures Illustrated selection structures using if statements Today 9/16/05 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 9/16/05 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; } 9/16/05 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 8.2: What would we need to change in the program? 9/16/05 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 9/16/05 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 ) ) 9/16/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Using logical expressions, how can we solve the bookstore problem 8.2 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 9/16/05 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 9/16/05 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 9/16/05 CS150 Introduction to Computer Science 1

Evaluating Expressions: Not ! Unary operator Examples: !((salary < minSalary) && (dependents > 5)) What makes this true? False? 9/16/05 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? 9/16/05 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 Relational <<, >> L->R Insertion/extraction ==, != L->R Equality && L->R And || L->R Or = R->L Assignment 9/16/05 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 9/16/05 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; 9/16/05 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 9/16/05 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) 9/16/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Examples Write the C++ expressions for each of the following x and y are greater than z x is equal to 1.0 or 3.0 x is the range z to y inclusive x is outside the range z to y 9/16/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Single Alternative if The if selection structures we saw last time all have a single alternative if (condition) or if (condition) one statement; { next statement; multiple statements; } next statement; If condition is true, statement(s) following if execute if condition is false, statement(s) following if are skipped. 9/16/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Multiple Statements If you have multiple statements that need to be executed if the condition is true, they should be surrounded by curly braces { } 9/16/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Examples if (x >= 0) cout << “x is positive” << endl; if (x < 0) x = -x; if ((x == 0) && (y == 0)) { x = 1; y = 1; } 9/16/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Program Write a program segment that allows the user to input two integer values into variables num1 and num2. Your program is to then exchange the values in the variables num1 and num2 only if num1 is greater than num2 9/16/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we covered Logical operators bool data type if structures with multiple statements { } Readings P. 71 - 77: if, UML, bool P. 124 - 128: logical operators, confusing = and == 9/16/05 CS150 Introduction to Computer Science 1