Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 6 Control Structures.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Objectives You should be able to describe:
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.
Chapter 5: Control Structures II (Repetition)
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Previously Repetition Structures While, Do-While, For.
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 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
2Object-Oriented Program Development Using C++ 3 Basic Loop Structures Loops repeat execution of an instruction set Three repetition structures: while,
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Fundamental Programming Fundamental Programming More on Repetition.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Controlling Behavior The if and for Statements. Function Behavior The behavior of a function is determined by the statements within the function. Statements.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Chapter 4 Repetition Structures
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
while Repetition Structure
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Programming Fundamentals
Additional Control Structures
3 Control Statements:.
Chapter 2.2 Control Structures (Iteration)
Looping III (do … while statement)
Alternate Version of STARTING OUT WITH C++ 4th Edition
2.6 The if/else Selection Structure
Objectives You should be able to describe: The while Statement
Repetition Statements (Loops) - 2
Control Statements Paritosh Srivastava.
Chapter 4 Repetition Structures
Presentation transcript:

Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression can be type int or char, but not type double. First the expression is evaluated, then the list of case labels is searched until one matches the expression value. Statements following the matching case level are executed until a break statement is encountered. The break causes an exit from the switch statement. Execution continues with the statement that follows the closing brace of the switch statement body. If no case level matches the controlling expression value, the statement following the default label are executed. If there is no default label, the entire switch statement body is skipped.

Switch structure switch (grade) { case ‘A’ : cout << “Excellent” << endl; break; case ‘B’ : cout << “Good” << endl; break; case ‘C’ : cout << ”O.K.” << endl; break; case ‘D’ : cout << “Poor” << endl; break; case ‘F’ : cout << “Failed” << endl; break; default : cout << “Invalid letter grade” << endl; break; }

Case A Flowchart for switch structure Case B Case C Case D Case F Excellentbreak Good O.K. Poor Failed break invalid

Switch Structure How many lines of output will be produced by the following program fragment ? int x; x = 0; switch (x) { case 0 : cout << "got 0“ << endl; case 1 : cout << "got 1“ << endl; case 2 : cout << "got 2“ << endl; default : cout << "do not have 0, 1 or 2“ << endl; }

Switch Structure switch(grade) { case ‘A’ : case ‘B’ : cout << “Good Work”; break; case ‘C’ : cout << “Average Work”; break; case ‘D’ : case ‘F’ : cout <<“Poor Work”; break; default : cout << grade << “ is an invalid letter grade”; break; }

Switch and if /else if /else structure switch (grade) { case ‘A’ : case ‘B’ : cout << “Good Work”; break; case ‘C’ : cout << “Average Work”; break; case ‘D’ : case ‘F’ : cout <<“Poor Work”; break; default : cout << grade << “ is an invalid letter grade”; break; } if (grade == ‘A’ || grade == ‘B’) cout << “Good Work”; else if (grade == ‘C’) cout << “Average Work”; else if (grade == ‘D’ || grade == ‘F’) cout << “Poor Work”; else cout << grade << “ is an invalid letter grade”; switch if / else if /else

Switch and if /else if /else structure switch (a) { case 5 : c = c + b; case 2 : c = c + 2*b; break; case 3 : c = 7; break; case 6 : break; case 7 : c = c + 9; break; case 4 : case 1 : c *= c; break; default : c %= 2; break; } if (a == 5) { c = c + b; c = c + 2*b; } else if (a == 2) c = c +2*b; else if (a == 3) c = 7; else if (a == 6) ; else if (a == 7) c = c + 9; else if ((a == 4) || (a == 1) c * = c; else c % = 2; switch if / else if /else

if /else and switch structures if ((i > 0) && ( i < 5)) { if ( i > 2) { j = k + 3; ++k; } if ( i < 4) { j = k - 2; k += 3; } else j = k + 5; Switch (i) { case 1 : case 2 : j = k - 2; k += 3; break; case 3 : j = k + 3; ++k; j = k - 2; k += 3; break; case 4 : j = k + 3; ++k; break; default : j = k + 5; break; } if / else structureSwitch structure

Switch Structure What will be printed by the following code fragment if n is equals 4? switch (n) { case 1: cout << “One”; case 2: cout << “Two”; case 3: cout << “Three”; case 4: cout << “Four”; case 5: cout << “Five”; default: cout << “Out of the range”; } Output: FourFiveOut of the range

Do-While Statement Both the for statement and the while statement check a loop repetition condition before the first execution of the loop body. This pretest prevents the loop execution when there may be no data items to process or when the initial value of the loop control variable is outside the range. In most cases, this pretest is desirable. However, there are some situations (generally involving interactive input) when we know that a loop must execute at least once. The pseudocode for an input validation loop is as follows: 1. Get a data value 2. If data value in not in the range, go back to step 1.

Do-While Statement Syntax template for the do-while: do Statement while (Expression); Note: do-while ends with a semicolon.

Do-While Statement do { cout << “Enter a number from 1 through 5”; cin >> num; } while (num 5) Prompts the user to enter one of the numbers 1 through 5. After cin gets a number, the loop repetition condition checks whether num contains one of the numbers in the range. If so, the repetition condition will be false and the next statement after the loop will be executed. If num contains some other number, the condition will be true and the loop body will be repeated. Since we know the program user must enter at least one number, the do-while is an ideal statement to use to implement this loop.

The For Statement for (count = 1; count <= 10; count++) cout << count << endl; initialization Loop repetition condition Updating condition

Rewrite the code using while loop for (count = 1; count <= 10; count++) cout << count << endl; for loop count = 1; while(count <= 10) { cout << count << endl; count++; } While loop

The Break Statement #include int main() { int x; for (x = 1; x <= 10; x++) { if (x == 5) break; // break loop only if x == 5 cout << x << “ “; } cout << endl; cout << “Broke out loop at x == “ << x << endl; return 0; } Broke out of loop at x == 5 Output

The Continue Statement #include int main() { int x; for (x = 1; x <= 10; x++) { if (x == 5) continue; // skip remaining code in loop // only if x == 5 cout << x << “ “; } cout << endl; cout << “Use continue to skip printing the value 5” << endl; return 0; } Use continue to skip printing the value 5 Output