CMPT 201 if-else statement

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 4: Making Decisions.
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
The If/Else Statement, Boolean Flags, and Menus Page 180
Chapter 4 Making Decisions
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
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
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
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.
Copyright 2003 Scott/Jones Publishing Making Decisions.
+ Chapter 4: Making Decisions Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
 Type Called bool  Bool has only two possible values: True and False.
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Chapter 3 Control Statements
Midterm Review.
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Making Decisions.
Selections Java.
Topic: Conditional Statements – Part 2
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: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Announcements General rules about homeworks
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions
Topics The if Statement The if-else Statement Comparing Strings
Relational Operators Operator Meaning < Less than > Greater than
Chapter 7 Conditional Statements
Topics 4.1 Relational Operators 4.2 The if Statement
Chapter 4 Selection.
Summary Two basic concepts: variables and assignments Basic types:
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Control Structure Chapter 3.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 2 Programming Basics.
Boolean Expressions to Make Comparisons
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
The Selection Structure
Control Structure.
Presentation transcript:

CMPT 201 if-else statement

Syntax for if-else if (condition) Yes_Statement else No_Statement Chooses between two alternative actions

Boolean Expression The type bool Two possible values: true or false bool flag; Two possible values: true or false 1 < 2 2 > 3 number > 10 num1 < num2

Boolean Expression if (number > 10) //when it is true cout<<“The number is greater than 10\n”; else //when it is false cout<<“The number is less than 10\n”;

Comparison Operators > greater than < less than >= greater than or equal to <= less than or equal to == equal to != not equal to a = 2; if (a == 2)

Exercises Read an integer from the user input, print a message showing if the number is even/odd.

Omitting the else part .... if (score < 60) score = 60; cout<<“Your final score is ”<<score;

Exercise: Guess a number

The And Operator If score is greater than 0 and less than 100... (score > 0) && (score < 100) is true only if both are true

The Or Operator (score1 >= 60) || (score2 >= 60) is true if one or both are true

The Not Operator !(score > 60) is equivalent to score <= 60

What is the result of the following expression !( (y < 3) || (y > 7) ) when y is 8?

Precedence Rules

if ((score > 0) && (score < 100)) even though the inner parentheses are not required x + 1 > 2 || x + 1 < -3 ((x + 1 ) > 2) || ((x + 1) < -3) Arithmetic operators (highest) Comparison operators Logical operators

Note if (x < z < y ) // Wrong!! cout<<“z is between x and y”; if ((z > x) && (z < y))

Short-Circuit Evaluation exp1 && exp2 if (((slices/students)>=2) && (students! =0)) cout<<“Each student may have two slices of pizza!”; if ((students!=0) && ((slices/students)>=2)) cout<<“Each student may have two slices of pizza!”;

Nested Statements

Guess a Number (Validate the User Input)

Guess a Number

Find the right match! if (fuel < 0.75) if (fuel < 0.25) cout<<“Fuel very low. Caution!”; else cout<<“Fuel over 3/4. Don’t stop now.”;

if (fuel < 0.75) { if (fuel < 0.25) cout<<“Fuel very low. Caution!”; } else cout<<“Fuel over 3/4. Don’t stop now.”;

Multiway if-else Sometimes things can not be solved in two ways. Driver license example: age<16: no way.. 16<=age<18: youth license 18<=age<70: standard license age>=70: special license

Multiway if-else syntax if (condition_1) Action_1; else if (condition_2) Action_2; ... else if (condition_N) Action_N; else Action_For_All_Other_Cases;

Under 16: You have to wait Between 16 and 18: You may have a youth license Between 18 and 70: You may have a standard license Over 70: You need a special license

Exercise Write a program to print the grade result based on the user input (score). Be sure to validate the user input. A 90-100 B 80-90 C 70-80 D 60-70 F <60

The Conditional Operator Can be used to create short if/else statements Format: expr ? expr : expr; x<0 ? y=10 : z=20; First Expression: Expression to be tested 2nd Expression: Executes if first expression is true 3rd Expression: Executes if the first expression is false

x > 100? a = 0 : a = 1 a = x > 100 ? 0 : 1

Blocks and Scope Scope of a variable is the block in which it is defined.

Type Casting static_cast<Type>(Value) double -> int static_cast<int>(9.2) static_cast<int>(number)

Type Casting (cont.) double m; m = static_cast<double>(y2-y1) Useful for floating point division using ints: double m; m = static_cast<double>(y2-y1) /(x2-x1); Useful to see int value of a char variable: char ch = 'C'; cout << ch << " is " << static_cast<int>(ch);

Comparing Characters Characters are compared using their ASCII values. 'A' < 'B' The ASCII value of 'A' (65) is less than the ASCII value of 'B'(66) How could you determine if a character is a capital letter? Lowercase letters have higher ASCII codes than uppercase letters, so 'a' > 'Z'

Homework 2 Due on Tuesday, October 4