1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.

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.
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/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.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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 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.
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
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
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.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
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
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
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.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
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.
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,
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
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):
The Ohio State University
Chapter 3 Selection Statements
Chapter 3 Control Statements
Selection (also known as Branching) Jumail Bin Taliba by
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.
for Repetition Structures
A mechanism for deciding whether an action should be taken
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
If statement.
Summary Two basic concepts: variables and assignments Basic types:
Let’s all Repeat Together
Decision I (if Statement)
Life is Full of Alternatives
Understanding Conditions
Arithmetic Operations
CS150 Introduction to Computer Science 1
do/while Selection Structure
Life is Full of Alternatives
Life is Full of Alternatives
CS150 Introduction to Computer Science 1
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Life is Full of Alternatives Part 3
Presentation transcript:

1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2

2 09/15/04CS150 Introduction to Computer Science 1 Last Time  We covered o UML activity diagrams o Simple if selection structure o Relational and equality operators o Logical operators  Today we will look at the if selection structure in more detail

3 09/15/04CS150 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) o These are unbearable heat and humidity conditions o Both must be true for the entire expression to be true

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

5 09/15/04CS150 Introduction to Computer Science 1 Evaluating Expressions: Not !  !expr  Unary operator  Examples: o !((salary 5)) o What makes this true? False?

6 09/15/04CS150 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?

7 09/15/04CS150 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

8 09/15/04CS150 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? o x < min + max o min <= x && x <= max o !x == y + 2 o x = a + b % 7 * 2

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

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

11 09/15/04CS150 Introduction to Computer Science 1 Examples  Assume that o double x = 3.0; o double y = 4.0; o double z = 2.0; o 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)

12 09/15/04CS150 Introduction to Computer Science 1 Let ’ s Review  Write the C++ expressions for each of the following o x and y are greater than z o x is equal to 1.0 or 3.0 o x is the range z to y inclusive o x is outside the range z to y

13 09/15/04CS150 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.

14 09/15/04CS150 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 { }

15 09/15/04CS150 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; }

16 09/15/04CS150 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

17 09/15/04CS150 Introduction to Computer Science 1 if/else Selection Structure  This is the multiple alternative if  Used when different statements should execute if the condition is falseif (condition) statementT;{ else statementsT; statementF;} else { statementsF; }

18 09/15/04CS150 Introduction to Computer Science 1 UML Activity Diagram if (x >= 0) cout << “x is positive” << endl; else cout << “x is negative” << endl; output “positive” [x >= 0][x < 0] output “negative”

19 09/15/04CS150 Introduction to Computer Science 1 Examples x = 25.0; if (y != (x )) x = x ; else x = x / 2.0; if ((y = 0.0)) x = 5 * y; else x = 2 * y;

20 09/15/04CS150 Introduction to Computer Science 1 Program  Write a program that inputs an integer number and outputs if its even or odd  Write a program that computes the area of a triangle or a rectangle based on the user typing in ‘t’ or ‘r’ first

21 09/15/04CS150 Introduction to Computer Science 1 Problem  What is the output of the following program segment i = 5; j = 2; if((i % j) == 0) i = j; j = i; cout << i << j; cout << "That's all folks" << endl;

22 09/15/04CS150 Introduction to Computer Science 1 Conditional Operator ?:  C++ provides the conditional operator ?: as a shortcut way of writing simple if/else structures  For example, the structure if (x >= 0) cout << “positive” << endl; else cout << “negative” << endl;  Could be written as cout = 0 ? “positive” : “negative” ) << endl;

23 09/15/04CS150 Introduction to Computer Science 1 Conditional Operator ?:  The format of the operator is o ( condition ? true-statement : false-statement )  The conditional operator works if there is only one statement for a true evaluation and only one statement for a false evaluation

24 09/15/04CS150 Introduction to Computer Science 1 Nested if/else Selection Structures  What if there are more than two alternatives? if (condition1) statement1; else if (condition2) statement2; … else default statement;

25 09/15/04CS150 Introduction to Computer Science 1 Problem  Write a C++ program segment that allows the user the ability to input an integer from the keyboard. If the integer is positive, increment a variable poscount by 1. If the integer is negative, increment a variable negcount by 1. If neither, increment zerocount by 1

26 09/15/04CS150 Introduction to Computer Science 1 Solution cin >> intvalue; if(intvalue > 0) poscount = poscount + 1; else if(intvalue < 0) negcount = negcount + 1; else zerocount = zerocount + 1;  Can you come up with another way of doing this?

27 09/15/04CS150 Introduction to Computer Science 1 Solution  Will this solution work? cin >> intvalue; if(intvalue > 0) poscount = poscount + 1; if(intvalue < 0) negcount = negcount + 1; if(intvalue = 0) zerocount = zerocount + 1;

28 09/15/04CS150 Introduction to Computer Science 1 Problem  Write a program that displays a letter grade corresponding to an exam score A B C D 0-59 F

29 09/15/04CS150 Introduction to Computer Science 1 Summary  In today’s lecture we covered o if/else selection structures o if structures with multiple statements { } o Nested if/else selection structures  Readings o P : if/else selection structures o P : conditional operator ?: o P : nested if/else selection structures and if structures with multiple statements