LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.

Slides:



Advertisements
Similar presentations
The if-else Statements
Advertisements

Fundamental of C programming
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
True or false A variable of type char can hold the value 301. ( F )
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
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.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
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.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 05 (Part III) Control Statements: Part II.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Chapter 5: Structured Programming
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
1 CS161 Introduction to Computer Science Topic #8.
Week 4 Program Control Structure
Chapter 4, cont: Control Structures if else nested if switch.
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Control Statements: Part1  if, if…else, switch 1.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
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)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Branching statements.
Chapter 3 Control 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.
CSC113: Computer Programming (Theory = 03, Lab = 01)
DKT121: Fundamental of Computer Programming
Programming Fundamentals
Bools & Ifs.
Bools & Ifs.
3 Control Statements:.
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Controlling Program Flow
Presentation transcript:

LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel

Content 2  Control structures  Types of selection statements  if single-selection statement  if..else double-selection statement  Nested if..else statements  Dangling else problem  switch multiple-selection statement  Common errors

Control Structure (Logic Structure)  Used to design data flow in modules and program as a whole  Basic structures 1. Sequential structure Processes one instruction after another 2. Selection structures Decision structure Make choices by using relational or logical operators Case structure Enable to pick up one of a set of tasks 3. Loop structure Enable to repeat tasks 3

Selection Statements  Three types  Single-selection statement Select or ignore a single group of actions  Double-selection statement Select between two groups of actions  Multiple-selection statement Select among many group of actions 4

if single-selection Statement  Syntax  Single action  Multiple actions  If condition is true, action is performed  Otherwise, action is ignored 5 if (condition) { action1; action2;.. actionN; } if(grade >= 60) Cout << “Passed”; if(grade >= 60) Cout << “Passed”; if (condition) action;

if.. else double-selection Statement  Begin with if followed by condition; then action or group of actions are listed  End with else then action or group of actions are listed  If condition is true, action that followed by if is performed Otherwise, action that followed by else is performed 6 if (condition) action1; else action2; if(grade >= 60) cout << “Passed”; else cout << “failed ”; if(grade >= 60) cout << “Passed”; else cout << “failed ”; 6

Question ? Which is the operator that provide the similar result of if..else double-selection statement? 7

Nested if.. else Statements  One inside another, test for multiple cases  Once condition met, other statements skipped 8 if (condition1) action1; else if (condition2) action2; else if (condition3) action3;... else actionN; if (condition1) { if (condition2) action1; else { if (condition3) action2; else action3; } else action4; 8

Example 9 if ( grade >= 90 ) cout << "A"; else if ( grade >= 80 ) cout << "B"; else if ( grade >= 70 ) cout << "C"; else if ( grade >= 60 ) cout << "D"; else cout << "F"; if ( grade >= 90 ) cout << "A"; else if ( grade >= 80 ) cout << "B"; else if ( grade >= 70 ) cout << "C"; else if ( grade >= 60 ) cout << "D"; else cout << "F"; 9

Dangling- else Problem  Each else associated with immediately preceding if  There is exception when placing braces { } 10 int x = 10, y = 2; if ( x > 5) if( y > 5) cout 5"<< endl; else cout<<"x is <=5“; int x = 10, y = 2; if ( x > 5) if( y > 5) cout 5"<< endl; else cout<<"x is <=5“; Have logic error int x = 10, y = 2; if ( x > 5) { if( y > 5) cout 5"<< endl; } else cout<<"x is <=5"; int x = 10, y = 2; if ( x > 5) { if( y > 5) cout 5"<< endl; } else cout<<"x is <=5"; Correctness

Example:Determine the output for each of the following when x is 9 and y is 11 and when x is 11 and y a) if ( x < 10 ) if ( y > 10 ) cout << "*****" << endl; else cout << "#####" << endl; cout << "$$$$$" << endl; ANS: b) if ( x < 10 ) { if ( y > 10 ) cout << "*****" << endl; } else { cout << "#####" << endl; cout << "$$$$$" << endl; } ANS: 11

Using bool Variables bool flag1 = true, flag2 = false; if ( flag1 ) … else … if ( flag1 || flag2 ) … … … else … … … 12

Implicit Typecasting int x1 = -15, x2 = 0; if ( x1 ) … else … if ( x1 || x2 ) … … … else … … … 13

Confusing ==  Confusing the equality operator == with the assignment operator = results in logic errors if ( x==2 ) cout<<“x is equal to 2”; else cout<<“x is not equal to 2”; if ( x=2 ) cout<<“x is equal to 2”; else cout<<“x is not equal to 2”; 14 This message will always be printed !!!

switch Multiple-selection Statement  Perform actions based on possible values of variable or expression  Begin with switch followed by controlling expression  Value of expression compared to case labels then execute action for that case  No matching, the execution go to the optional default statement  break causes immediate exit from switch statement 15 if (condition1) switch (expression) { case value1: action1; break; case value2: action2; break;... case valueN: actionN; break; default: action; }

Example 16 switch (number) { case 0: cout << "Too small, sorry!"; break; case 5: cout << "Good job! " << endl; // fall through case 4: cout << "Nice Pick!" << endl; // fall through case 3: cout << "Excellent!" << endl; // fall through case 2: cout << "Masterful!" << endl; // fall through case 1: cout << "Incredible!" << endl; break; default:cout << "Too large!" << endl; break; } switch (number) { case 0: cout << "Too small, sorry!"; break; case 5: cout << "Good job! " << endl; // fall through case 4: cout << "Nice Pick!" << endl; // fall through case 3: cout << "Excellent!" << endl; // fall through case 2: cout << "Masterful!" << endl; // fall through case 1: cout << "Incredible!" << endl; break; default:cout << "Too large!" << endl; break; }

Printing Values of Enumerated Type 17 enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY }; Day today = FRIDAY ; switch (today) { case 0: cout << “MONDAY”; break; case 1: cout << “TUESDAY”; break; case 2: cout << “WEDNESDAY”; break; case 3: cout << “THURSDAY”; break; case 4: cout << “FRIDAY”; break; } enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY }; Day today = FRIDAY ; switch (today) { case 0: cout << “MONDAY”; break; case 1: cout << “TUESDAY”; break; case 2: cout << “WEDNESDAY”; break; case 3: cout << “THURSDAY”; break; case 4: cout << “FRIDAY”; break; }

Common Compilation Errors  Placing semicolon (;) after if condition or else keyword  Omitting spaces between case keyword and value  Specifying expression including variables (a + b) in case label of switch statement  Providing identical case labels  Forgetting a break statement when one is needed in a switch 18

Exercise  Write a program that asks for an integer and reports whether the number is odd or even. Use if.. else statement.  Write another version of program using switch statement.

End 20