Chapter 05 (Part III) Control Statements: Part II.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.11 Assignment Operators 2.12 Increment and Decrement Operators.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Control Statements (II) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Control Structures in C++ while, do/while, for switch, break, continue.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Chapter 5: Control Structures II (Repetition)
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline Assignment Operators Increment and Decrement Operators Essentials of Counter-Controlled.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 02 (Part III) Introduction to C++ Programming.
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
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.
1 Chapter 9 Additional Control Structures Dale/Weems.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
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
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
COMP Loop Statements Yi Hong May 21, 2015.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 4 – C Program Control
Chapter 4 C Program Control Part I
Chapter 2.1 Control Structures (Selection)
JavaScript: Control Statements.
Control Statements: Part 2
Control Statements: Part 2
مبانی برنامه‌سازی با C++ جلسه دوم
- Additional C Statements
Introduction to C++ Programming
Structured Program Development in C
Chapter 7 Additional Control Structures
Chapter 4: Control Structures I (Selection)
switch Selection Structure
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

Chapter 05 (Part III) Control Statements: Part II

Objectives In this part you will learn: To understand multiple selection using the switch selection statement. To use the break and continue program control statements to alter the flow of control.

5.6 switch Multiple-Selection Statement switch statement –Used for multiple selections –Tests a variable or expression

5.6 switch Multiple-Selection Statement switch statement –Controlling expression Expression in parentheses after keyword switch –case labels Compared with the controlling expression Statements following the matching case label are executed –Braces are not necessary around multiple statements in a case label

The Use of switch Statement int option; cin >> option; switch (option) { case 0: …; break; case 1: …; break; default: …; break; } Controlling Expression compared with the controlling expression

5.6 switch Multiple-Selection Statement –default case Executes if no matching case label is found This case is optional –If no match and no default case »Control simply continues after the switch A break statements causes execution to proceed with the first statement after the switch »Without a break statement, execution will fall through to the next case label Note: Forgetting a break statement when one is needed in a switch statement is a logic error.

5.6 switch Multiple-Selection Statement The default clause does not require a break statement. Some programmers include break for clarity and for symmetry with other cases. Specifying an expression in the case label is a syntax error. – Incorrect example: switch (option) { case a+b: …; break; … }

Fig | switch multiple-selection statement with break statements.

Comparison int option; cin >> option; switch (option) { case 1: result = a + b; break; case 2: result = a – b; break; default: cout << “Incorrect option!” << endl; break; } int option; cin >> option; if (option == 1) { result = a + b; } else if (option == 2) { result = a – b; } else { cout << “Incorrect option!” << endl; }

Calculating number of letters Declare a variable with character. Examples of character constant: ‘A’, ‘b’, ‘C’, ‘\n’… Note: Do not confuse string constant with character constant. A string is a sequence of characters. String: “A” Character: ‘A’

Reading character input –Function cin.get() Reads one character from the keyboard –EOF End-of-file. Defined in d in UNIX/Linux z in Windows and then press ‘Enter’. –while ((letter = cin.get()) != EOF) 1.Read one character from keyboard and assign it to letter. 2.Compare letter with EOF If letter != EOF (End-of-file), enter the loop. Otherwise (letter == EOF), leave the loop.

switch Multiple-Selection Statement This statement executes when letter == ‘A’ or letter == ‘a’. Without a break statement, execution will fall through to the next case label Identify newline, tab, and space character

Output Results Character constant String constant Note: A character constant is enclosed using single quotation marks. We can also represent a character constant using integer type. A string constant is enclosed using double quotation marks.

Integer Data Types Integer data types –short Abbreviation of short int Minimum range is -32,768 to 32,767 –long Abbreviation of long int Minimum range is -2,147,483,648 to 2,147,483,647 –int Equivalent to either short or long on most computers –char Can be used to represent small integers Note: Integer data types can vary in size between systems

5.7 break and continue Statements break / continue statements –Alter flow of control break statement –Causes immediate exit from control structure –Used in while, for, do…while or switch statements Example int option = 0; while (option != -1) { cin >> option; if (option == -1) break;//force immediate exit }

5.7 break and continue Statements continue statement –Skips remaining statements in loop body Proceeds to increment and condition test in for loops Proceeds to condition test in while / do…while loops –Then performs next iteration (if not terminating) –Used in while, for or do…while statements

Loop 10 times Exit for statement (with a break ) when count equals 5

Loop 10 times Skip line 14 and proceed to line 9 when count equals 5

Performance Tip 5.5 The break and continue statements, when used properly, perform faster than do the corresponding structured techniques.