PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

If Statements & Relational Operators Programming.
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 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
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.
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
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
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: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Part 1: Selection
CONTROLLING PROGRAM FLOW
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
CPS120: Introduction to Computer Science Decision Making in Programs.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Branching statements.
Chapter 3 Selection Statements
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.
Chapter 4: Making Decisions.
Chapter 7 Conditional Statements
Selection Control Structure
Chapter 4: Control Structures I (Selection)
Branching statements Kingdom of Saudi Arabia
Selection Control Structure
Presentation transcript:

PEG200/Saidatul Rahah 1

 Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if Statements  The switch Statement  Applications PEG200/Saidatul Rahah 2

 if-else statement: implements a decision structure for two alternatives Syntax: if (condition) statement executed if condition is true; else statement executed if condition is false; 3 true false PEG200/Saidatul Rahah

 Relational expression: compares two operands or expressions using relational operators 4 PEG200/Saidatul Rahah

 Relational Operators: › True  1 › False  0 PEG200/Saidatul Rahah 5 ExpressionValueInterpretationComment “Hello”>”Goodbye”1trueThe first ‘H’ in ‘Hello’ is greater than ‘G’ in ‘Goodbye’ “SMITH” > “JONES”1TrueThe first ‘S’ in SMITH is greater than ‘J’ in JONES. “Behop”>”Beehive”1TrueThe 3 rd char., ‘h’ is “Behop” is greater than the 3 rd char. ‘e’ in “Beehive”

 Logical Operators PEG200/Saidatul Rahah 6 AND&& condition is true only if both expressions are true OR|| condition is true if either one or both of the expressions is true NOT! changes an expression to its opposite state ; true becomes false, false becomes true

pqp&&qp||q!p TTTTF TFFTF FTFTT FFFFT PEG200/Saidatul Rahah 7

 Precedence and Associativity of Operators (Table 4.2 page 190) PEG200/Saidatul Rahah 8

 Using parentheses, determine the value of the following expression, assuming Note: Show your work 6 % 2 * 4 > 5 || 2 % 2 * 6 2 PEG200/Saidatul Rahah 9

1pt parentheses ( (((6 % 2) * 4) > 5) || (((2 % 2) * 6) 2))) (((0 * 4) > 5 ) || ((0 * 6) < (7 && 1)) ((0 > 5) || (0 < 7 && 1) (0 || 1 && 1) 2pt for the working flow 0 || 1 1 1pt for answer PEG200/Saidatul Rahah 10

 if-else performs instructions based on the result of a comparison  Place statements on separate lines for readability Syntax: PEG200/Saidatul Rahah 11 true false

#include int main() { int number = 5; int guess; cout > guess; if (guess == number) { cout << "Incredible, you are correct" << endl; } else { cout << "Sorry, try again" << endl; } return 0; } PEG200/Saidatul Rahah 12

 Compound statement: a sequence of single statements contained between braces, creating a block of statements  Any variable declared within a block is usable only within that block  Scope: the area within a program where a variable can be used; a variable’s scope is based on where the variable is declared PEG200/Saidatul Rahah 13

PEG200/Saidatul Rahah 14

 Write the appropriate if statements for each of the following conditions: › If the slope is less than 0.5 set the variable flag to zero, else set flag to one. › If x is greater than y and z is less than 20, read in a value for p. › If distance is greater than 20 and it less than 35, read in a value for time. PEG200/Saidatul Rahah 15

 Nested if statement: an if- else statement completely contained within another if-else PEG200/Saidatul Rahah 16 true false true false

 General form of an if-else chain PEG200/Saidatul Rahah 17 if(gender ==‘f’) cout<< “Female”; else if (gender==‘m’) cout<< “Male”; else cout<< “An invalid code for gender was entered”;

PEG200/Saidatul Rahah 18 #include int main() { int number = 5; int guess; cout > guess; if (guess == number) { cout << "Incredible, you are correct" << endl; } else if (guess < number) { cout << "Higher, try again" << endl; } else { cout << "Lower, try again" << endl; } return 0; }

 An angle is considered acute if it less than 90 degrees, obtuse if it greater than 90 degrees, and a right angle if it is equal to 90 degrees. Using this information, write a C++ program that accepts an angle, in degrees, and displays the type of angle corresponding to the degrees entered. PEG200/Saidatul Rahah 19

 switch statement: provides for one selection from many alternatives  switch keyword starts the statement; is followed by the expression to be evaluated  case keyword identifies a value to be compared to the switch expression; when a match is found, statements in this case block are executed  All further cases after a match is found are executed unless a break statement is found  default case is executed if no other case value matches were found (is optional) PEG200/Saidatul Rahah 20

PEG200/Saidatul Rahah 21 S2SnS1 switch (gender) { case ‘f’: case ‘F’: cout<< “Female”; break; case ‘m’: case ‘M’: cout<< “Male”; break; default: cout<< “An invalid code for gender was entered”; }

 Rewrite the following if-else chain using a switch statement: if (factor==1) pressure =25.0; else if (factor==2) pressure =36.0; else if (factor==3)||if (factor==4)||if(factor==5) pressure =49.0; PEG200/Saidatul Rahah 22

Using the assignment operator ( = ) instead of the relational operator ( == ) for an equality test Assuming a structural problem with an if-else causes the error instead of focusing on the data value being tested Using nested if statements without braces to define the structure PEG200/Saidatul Rahah 23

 Write a program to input a student IQ and gender. Display “Intelligent Male” if the student is a male with IQ of at least 100. Display “Not so intelligent Male” if the student is a male with IQ is less than 100. Display the same message for female students as well.  Thank You for Listening….. PEG200/Saidatul Rahah 24