Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Logic & program control part 3: Compound selection structures.
Chapter 4: Making Decisions.
1 CSC103: Introduction to Computer and Programming Lecture No 8.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
Single selection syntax if ( expression ) { statements; } TRUE FALSE expression if clause 1. Statemens are executed when expression is true 2. { } can.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems/Headington.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Control Structures I (Selection)
Conditions, Logical Expressions, and Selection Control Structures Sumber dari :
Chapter 4 Logical Expressions & If-Else. 2 Overview  More on Data Type bool u Using Relational & Logical Operators to Construct & Evaluate Logical Expressions.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
Conditional Statement
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
1 CS161 Introduction to Computer Science Topic #8.
1 Conditions, Logical Expressions, and Selection Control Structures.
Control statements Mostafa Abdallah
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Selection in C++ If statements. Control Structures Sequence Selection Repetition Module.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
C++ Programming Control Structures I (Selection).
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
A First Book of C++ Chapter 4 Selection.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 4: Making Decisions.
Decisions Given hours worked and pay rate, calculate total pay
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Control Structures – Selection
Decisions Given hours worked and pay rate, calculate total pay
Chapter 4: Control Structures I (Selection)
Control Statements.
Selection Control Structure
Presentation transcript:

Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor

Lecture 5 Topics u If statement, more examples u Nested Structures u Switch statement

Selection statements are used to choose an action depending on the current situation in your program as it is running

Control structures use logical expressions which may include: 6 Relational Operators >= == != >= == != 3 Logical Operators !&&||

If statement is a selection of whether or not to execute a statement (which can be a single statement or an entire block) TRUE FALSE statement expression

if ( Expression ) Statement NOTE: Statement can be a single statement, a null statement, or a block. If Syntax

Write If or If-Else for each If taxCode is ‘T’, increase price by adding taxRate times price to it. If age is bigger than 60, give a discount of 10% on a purchase. If age is bigger than 60, give a discount of 10% on a purchase. If A is strictly between 0 and 5, set B equal to 1/A, otherwise set B equal to A.

Example if (taxCode == ‘T’) price = price + taxRate * price; if ( A > 0 && A < 5 ) B = 1 / A ; else B = A;

What output? and Why? int age; age = 20; if ( age == 16 ) { cout<<( “Did you get driver’s license?”) ; }

What output? and Why? int age; age = 30; if ( age < 18 ) cout<< ( “Do you drive?”); cout<< ( “Too young to vote”);

Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection statement.

Multi-alternative Selection is also called multi-way branching, and can be accomplished by using NESTED if statements.

if ( Expression1 ) Statement1 else if ( Expression2 ) Statement2. else if ( ExpressionN ) StatementN else Statement N+1 EXACTLY 1 of these statements will be executed. Nested if Statements

Each Expression is evaluated in sequence, until some Expression is found that is true. Only the specific Statement following that particular true Expression is executed. If no Expression is true, the Statement following the final else is executed. Actually, the final else and final Statement are optional. If omitted, and no Expression is true, then no Statement is executed. AN EXAMPLE...

if ( creditsEarned >= 90 ) cout << (“Fourth year student “ ) ; else if ( creditsEarned >= 60 ) cout << ( “Third year student “ ) ; else if ( creditsEarned >= 30 ) cout << ( “Second year student “ ) ; else cout << ( “First year student “ ) ; Multi-way Branching

Writing Nested if Statements 1. Display one word to describe the int value of number as “Positive”, “Negative”, or “Zero”. 2. Your city classifies a pollution index less than 35 as “Pleasant”, 35 through 60 as “Unpleasant”, and above 60 as “Health Hazard.” and above 60 as “Health Hazard.” Display the correct description of the pollution index value.

Answer (1) if (number > 0) cout << ( “Positive” ); else if (number < 0) cout << ( “Negative” ); else cout << ( “Zero” ) ;

Answer (2) if ( index < 35 ) cout << ( “Pleasant” ); else if ( index <= 60 ) cout << ( “Unpleasant” ); else cout << ( “Health Hazard” ) ;

Example An Air Conditioner is designed to turn on : between 7 a.m. and 6 p.m. If the temperature Exceeds 75 ° F and the humidity exceeds 40 %, or if the temperature exceeds 70 ° F and the humidity exceeds 80 %; or between 6 p.m. and 7 a.m. If the temperature exceeds 80 ° F and the humidity exceeds 80 %, or If the temperature exceeds 85 ° F regardless of the humidity. Develop a program that input temperature, humidity, and time of day and displays a message specifying whether the air conditioner is on or off.

Answer #include int main () { float tim, tem, hm; // declare the variables for time, temperature and humidity char status = ‘ ‘; // status of air conditioner on =‘N’ or off =‘F’ if (tim >= 7 && tim << 18) if ( tem > 75 && hm > 40) status = ‘N’ else if ( tem > 70 && hm > 80) status = ‘N’ else status = ‘F’;

Answer (Con.) else // next time, we don’t need to check it if ( tem > 80 && hm > 80) status = ‘N’ else if ( tem > 85) status = ‘N’ else status = ‘F’; cout << “the air conditioner now is: “<< status; return 0; }

Switch statement switch ( integer_expr ) { case constant_expr1: statement1; break; case constant_expr2: statement2; break; [default: statement3;] } –Useful to select from a number of alternative integer values –Constant_exprs must be byte, int, char, or short.

More on the switch Statement – –Case labels must be constants. – –Use break to jump out of a switch. – –It is recommended to always provide a default. switch (choice) { case 37: cout << "Coffee?"; break; case 45: cout << "Tea?"; break; default: cout << "???"; }

break vs return n break means exit the switch statement and continue on with the rest of the program. n return means exit the whole program. n They could both be used anywhere in the program.

Example char grade ; cout << (“Enter your letter grade: “); cin >> grade; switch ( grade ) { case ‘A’ : cout << (“ Excellent Job”); break; case ‘B’ : cout << ( “ Very Good “); break; case ‘C’ : cout << (“ Not bad “); break; case ‘F’ : cout << (“Failing”); break; default : cout << (“ Wrong Input “); }