Chapter 3 Selection Statements

Slides:



Advertisements
Similar presentations
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Introduction to Control Statements JavaScript – Part 3 George Mason University June 3, 2010.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Selection Structures: if and switch Statements. 2 Selection Statements –In this chapter we study statements that allow alternatives to straight sequential.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
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,
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Introduction to Control Statements IT108 George Mason University.
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved
Lecture 3 Selection Statements
The Ohio State University
Branching statements.
Chapter 4: Control Structures I
Chapter 3 Control Statements
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Control Structures I
Selections Java.
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.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Control Structures I
Compound Assignment Operators in C++
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Chapter 7 Conditional Statements
Chapter 4 Selection.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Control Structure Chapter 3.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Branching statements Kingdom of Saudi Arabia
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Control Structure.
Presentation transcript:

Chapter 3 Selection Statements Boolean Logic boolean variables can have only 2 possible values -- true or false bool flag = true; Relational Operators Ordering < <= > >= Equality == != Warning: Don’t use = to compare! Evaluate: (7 == 7) (7 != 7) (4 >= 5) (4 <= (5 + 3)) (‘A’ == ‘a’) (‘B’ >= ‘A’)

IF Statements Syntax: if (BooleanExpression) Single C++ Statement; where Statement is any C++ statement. Semantics: If BooleanExpression is true, then execute the Single C++ Statement. (Otherwise, skip the Statement.) a = 4; b = 5; if (a > b) a = a + b; cout << a; cout << b;

What if we want to execute several statements in the then-part? Make a compound statement by using { and } if (num1 > num2) { num1 = num1 + 1; num2 = num2 - 1; } //if What is the output of these C++ statements? (i) int x = 7; if(x > 8) x = x * 2; cout << x; } cout << “Bye!”; (ii) int x = 7;

Write code that reads in 2 numbers, and then prints the larger number, followed by the smaller number. cout << “enter two numbers ”; cin >> num1 >> num2; if (num1 > num2) { cout << num1 << “ is larger”; cout << num2 << “ is smaller”; } if (num1 <= num2) cout << num1 << “ is smaller”; cout << num2 << “is larger”;

if (BooleanExpression) Statement1; else Statement2; Symantics Can you write code to just print out the larger of the two? if … else Syntax if (BooleanExpression) Statement1; else Statement2; Symantics If BooleanExpression is true, execute Statement1; otherwise, execute Statement2. if (num1 > num2) cout << "Larger one:" << num1; cout << "Larger one:" << num2;

Boolean Operators Truth Table Examples AND (&&) P Q (P && Q) True True True True False False False True False False False False OR (||) P Q (P || Q) True False True False True True NOT (!) P !(P) True False False True

Operator Precedence Chart First: the unary operators: +, -, and ! Second: the binary arithmetic operators: *, /, % Third: the binary arithmetic operators: +, - Fourth: the comparison operators: <, >, <=, >= Fifth: the Boolean operators: ==, != Sixth: the Boolean operator && Seventh: the Boolean operator || Eighth: the assignment operator = TIP: use the ()s for an expression to make the meaning of the expression perfectly clear.

What is the output of these C++ statements with the given values of x and y? if ((x >= y) || (x == 3)) { x = x - 1; y = y + 1; } //if else x = x + 1; y = y - 1; } //else cout << x << endl; cout << y; Case 1. x = 5 and y = 3 Case 2. x = 3 and y = 5 Case 3. x = 4 and y = 5

Nested if…else Statements if (cond1) statement1; else if (cond2) statement2; else if (cond3) statement3; else statement4; if (temp >= 100) ans = “Very Hot”; else if (temp >= 90) ans = “Hot”; else if (temp >= 70) ans = “Warm”; else if (temp >= 50) ans = “Cool”; ans = “Cold”; Write code that assigns a letter grade for a student, based on his/her score.

#include <iostream> use namespace std; int main() { int score; char letterGrade; cout << "Enter your score "; cin >> score; if (score > 89) letterGrade = ‘A’; else if (score > 79) letterGrade = ‘B’; else if (score > 69) letterGrade = ‘C’; else if (score > 59) letterGrade = ‘D’; else letterGrade = ‘F’; cout << "Your letter grade is :" << letterGrade; return 0; }

if (gender == ‘M’) if (age < 21) premium = 800.00; else premium = 600.00; IMPORTANT: an else is matched with the most recent non-matched if!!! Indentation does NOT matter { } //if

switch statements The switch statement provides an alternative to nested if/else statements. Syntax: switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; … case valueN: statement(s)N; default: statement(s)-for-default; }

Rules of switch statement The switch-expression must yield a value of int (byte, long) or char type since each case evaluates an == test. The value1, …, and valueN must have the same data type as the value of the switch-expression. They cannot contain variables in the expression. The keyword break is optional. The break statement immediately ends the switch statement. The case statements are checked in sequential order, but the order of the cases (including the default case) does not matter. However, it is good programming style to follow the logical sequence of the cases and place the default case at the end. See pages 93-95 for more detail.

int score = ???; switch (score/10) { case 10: case 9: letterGrade = ‘A’; break; case 8: letterGrade = ‘B’; case 7: letterGrade = ‘C’; case 6: letterGrade = ‘D’; default: letterGrade = ‘F’; } //switch cout << “Your letter grade is “ << letterGrade); //Rewrite using nested if-else.

Write an equivalent nested if-else. //suppose score is out of 100 ans = score / 10; switch (ans) { case 0: case 1: case 2: case 3: case 4: case 5: cout << “Grade is F”; break; case 6: cout << “Grade is D”; case 7: cout << “Grade is C”; case 8: cout << Grade is B”; case 9: case 10: cout << Grade is A”; default: cout << “invalid grade!”; } // switch Write an equivalent nested if-else.

Formatting Floating Point Output setprecision(n) is used to specify the number of digits to the right of the decimal point. fixed specifies that trailing 0s will be printed. Using setprecision(2) will allow a currency look to your floating point output. cout << “Your total is $” << fixed << setprecision(2) << total << endl; setwidth(n) is used to specify the width of the output field. Using them together you can generate tables of numbers where the decimal places all match up. cout << setwidth(10) << fixed << setprecision(4) << num << endl; Tells the computer to output num with 5 spaces or digits to the left of the decimal point and 4 digits to the right. The decimal point itself counts as part of the width. Must #include <iomanip> for these to work.

C++ string The string datatype in C++ is actually an object of the class string. We can use it as a primitive variable, but it is not a primitive. #include <string> int main() { string first; cout << “enter first name “; cin >> first; cout << “you entered “ << first ; return 0; }

Chapter 3 Skip Section 3.10 for now Review Questions 3.1 – 3.4 3.14 – 3.20 3.22 – 3.23 3.27 3.33 – 3.34