 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,

Slides:



Advertisements
Similar presentations
Control Structures Selections Repetitions/iterations
Advertisements

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.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
4 Control Statements.
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.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved. 4.
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.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter#3 Part1 Structured Program Development in C++
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Control Statements: Part1  if, if…else, switch 1.
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)
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Branching statements.
The if…else Selection Statement
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.
Control Statements: Part 1
JavaScript: Control Statements I
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 4 Control Statements: Part I
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Chapter 4: Control Structures I (Selection)
2.6 The if/else Selection Structure
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Structural Program Development: If, If-Else
Presentation transcript:

 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single, double and multiple selection statements. ◦ Identify and use the C++ conditional operator (?:) ◦ Use the nested control statement and explain its meaning. ◦ Determine the problems that require the user of nested control structure.

 Used to design data flow in modules and programs as whole  Basic structures: ◦ Sequence structure:  Process one instruction after another. ◦ Selection structures:  Decision structure  Make choices by using relational or logical operators  Case structure  Enable to pick up one of a set of tasks ◦ Repetition structure:  Enable to repeat tasks.

C++ Control Structure Sequence structure Selection Structure If, if… else, switch Repetition structure While, do…while, for

Add grade to total Add 1 to counter Corresponding C++ statement: total=total + grade; Corresponding C++ statement: counter=counter + 1;

 Three types: ◦ Single selection statement  Selects or ignores a single group of actions. ◦ Double-selection statement  Selects between two groups of actions. ◦ Multiple-selection statement  Selects among many group of actions.

 Use the keyword if  Begin with if followed by condition; then action or group of actions (compound statements or blocks) are listed.  Enclose compound statements between braces {} and indent these statements  If condition is true, action is performed. Otherwise, actions is ignored  Example: if (grade >=60) cout<<“Passed”; Pseudocode: If student grade is greater than or equal to 60 Print “Passed”

Grade >=60 Grade<60 Print “Passed”

if (condition) action; if (condition) { action1; action2; ….. …. actionN; } Condition can be relational or equality operators or any other expressions

 Use the keyword if and else  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.

if (condition) action1; else action2; Condition can be relational or equality operators or any other expressions

Grade >=60Grade<60 Print “Passed”Print “Failed”

 if … else double-selection statement  The following pseudocode prints “Passed” if the student’s grade is greater than or equal to 60, or “Failed” if the student’s grade is less than 60.  If student’s grade is greater than or equal to 60 Print “Passed” Else Print “Failed”  The preceding pseudocode If…Else statement can be written in C++ as if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

 One inside another, test for multiple cases  Once condition met, other statements skipped

 Provide similar result of if…else double selection statement  Ternary operator requires three operands: ◦ The first operand is a condition ◦ The second operand is the value for the entire conditional expression if the condition is true ◦ The third operand is the value for the entire conditional expression if the condition is false.  Syntax: Condition ? Condition’s true value : condition’s false value

 Example ◦ Grade >=60 ? Cout<<“Passed”: cout<<“Failed”;  Can be written as: cout =60 ? “passed”: “failed”);  Example: int i=1, j=2, max; max=(i>j ?i:j);

if (conditioin1) action1; else if (condition2) action2; else if(condition3) action3; … else actionN;

if (condition1) { if (condition2) action1; else { if (condtion3) action2; else action3; } else action4;

 Write the pseudocode for if…else statements that prints A for exam grades greater than or equal to 90, B for grades in the range 80 to 89, C for grades in the range 70 to 79, D for grades in the range 60 to 69 and F for all other grades.

 Nested if … else statements test for multiple cases by placing if … else selection statements inside other if … else selection statements.  If student’s grade is greater than or equal to 90 Print “A” Else If student’s grade is greater than or equal to 80 Print “B” Else If student’s grade is greater than or equal to 70 Print “C” Else If student’s grade is greater than or equal to 60 Print “D” Else Print “F”

 if ( studentGrade >= 90 ) // 90 and above gets "A" cout = 80 ) // gets "B" cout = 70 ) // gets "C" cout = 60 ) // gets //"D" cout << "D"; else // less than 60 gets "F" cout << "F";

 Each else associated with immediately preceding if  There is exception when placing braces {}  Example x=10; y=2; if (x>5) if(y>5) cout 5 “<<endl; else cout<<“x is <=5”; Logical error !!

 Most write the preceding if … else statement as  if ( studentGrade >= 90 ) // 90 and above gets "A" cout = 80 ) // gets "B" cout = 70 ) // gets "C" cout = 60 ) // gets "D" cout << "D"; else // less than 60 gets "F" cout << "F";

 Correctness x=10; y=2; if(x>5) { if(y>5) cout 5”<endl; } else cout<<“x is <=5”;

 The C++ compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ( { and } ).  This behavior can lead to what’s referred to as the dangling- else problem.  x=10; y=2;  if ( x > 5 ) if ( y > 5 ) cout 5"; else cout << "x is <= 5"; appears to indicate that if x is greater than 5, the nested if statement determines whether y is also greater than 5.

 The compiler actually interprets the statement as x=10; y=2;  if ( x > 5 ) if ( y > 5 ) cout 5"; else cout << "x is <= 5";  To force the nested if … else statement to execute as intended, use:  x=10; y=2; if ( x > 5 ) { if ( y > 5 ) cout 5"; } else cout << "x is <= 5";  Braces ( {} ) indicate that the second if statement is in the body of the first if and that the else is associated with the first if.

bool flag1, flag2; if (flag1) ---- else ---- if( flag1|| flag2) ---- else -----

int x1,x2; if(x1) … else … if(x1||x2) … else ….

 Confusing the equality operator == with the assignment operator = results in logic errors. #include using namespace std; int main () { int num=0,x=0; if (x=2) cout<<"x is equal to 2"; else cout<<"x is not equal to 2"; return 0; } This message will always be printed !!

 State the output for each of the following: ◦ When x is 9 and y is 11 ◦ When x is 11 and y is 9 a. if(x<10) if(y>10) cout<<“*****”<<endl; else cout<“#####”<<endl; cout<<“$$$$$”<<endl;

b. if(x<10) { if(y>10) cout<<“*****”<<endl; } else { cout<<“#####”<<endl; cout<<“$$$$$”<<endl; }

 Write a C++ program that compares two integers using if statements, relational operators and equality operators.  Sample output:

 Perform actions based on possible values of variable or expression  Use keywords switch, case, default and break.  Begin with switch followed by controlling expression.  Value of expression compared to case label then go to execute action for that case.  No matching, the execution go to the optional default statement.  Break causes immediate exit from switch statement.

switch (expression) { case value1: action1; break; case value2: action2; break; …. case valuen: actionN; break; default: action; }

 Example: 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<<“masterfull!”<<endl; //fall through case 1: cout<<“incredible!”<<endl; //fall through break; }

 The break keyword means "jump out of the switch statement, and do not execute any more code." To show how this works, examine the following piece of code: