1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.

Slides:



Advertisements
Similar presentations
 Control structures  Algorithm & flowchart  If statements  While statements.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 3 - Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
Control Structures in C++ while, do/while, for switch, break, continue.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Structured Program Development in C
Lecture 3 Structured Program Development in C
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
The University of Texas – Pan American
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Previously Repetition Structures While, Do-While, For.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
1 Lecture 3 Control Structures else/if and while.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
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.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
while Repetition Structure
Ch 7: JavaScript Control Statements I.
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
COMS 261 Computer Science I
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 2.2 Control Structures (Iteration)
Chapter 3 - Structured Program Development
Let’s all Repeat Together
2.6 The if/else Selection Structure
EPSII 59:006 Spring 2004.
Presentation transcript:

1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11

2 Announcements Homework 3 –Due Wednesday, 9/28/05 –Page –4.1, 4.2, 4.3, 4.10, 4.11, 4.12, 4.33, 4.37, 4.39, 4.47

3 Review Control Construct –Conditional or selection constructs

4 Outline Switch

5 Selection It is often the case that depending upon the value of an expression we want to perform a particular action Two major ways of accomplishing this choice –if-else-if statement if-else statements “glued” together –Switch statement An advanced construct

6 A sequence of if statements if (die == 1) cout << “1” << endl; else if (die == 2) cout “2” << endl; else if (die == 3) cout “3” << endl; else if (die == 4) cout “4” << endl; else if (die == 5) cout “5” << endl; else cout “6” << endl; cout << “Roll again, sucker!” << endl;

7 A switch statement switch (die) { case 1: cout << “1” << endl; break; case 2: cout << “2” << endl; break; case 3:cout << “3” << endl; break; case 4:cout << “4” << endl; break; case 5:cout << “5” << endl; break; default: cout << “6” << endl; } Which code would you rather deal with?? What if I wanted to allow for the roll for two dice?

8 A switch statement More than one statement is allowed in a case, without turning it into a block –The statements after the corresponding case are executed until A break statement occurs The end of the switch block

9 A switch statement switch (die) { case 1: cout << “1”; cout << “ is a lot of fun”; break; case 2: cout << “2”; cout << “ now that’s cool”; break; case 3: cout << “3”; break; case 4: cout << “4”; break; case 5: cout << “5”; break; default: cout << “6”; } cout << endl;

10 A switch statement switch (die) { case 1: case 2: case 3: cout << “die between 1 and 3”; case 4: case 5: case 6: cout << “die between 1 and 6”; } cout << endl;

11 A switch statement Notice that is a case is satisfied and does not have a corresponding break, all the following cases are evaluated The default case is not required

12 A Switch Statement switch (ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': cout << ch << " is a vowel" << endl; break; default: cout << ch << " is not a vowel" << endl; }

13 cout << "Enter simple expression: "; int left; int right; char operator; cin >> left >> operator >> right; cout << left << " " << operator << " " << right << " = "; switch (operator) { case '+' : cout << left + right << endl; break; case '-' : cout << left - right << endl; break; case '*' : cout << left * right << endl; break; case '/' : cout << left / right << endl; break; default: cout << "Illegal operation" << endl; }

14 The while Repetition Structure Repetition structure –Programmer specifies an action to be repeated while some condition remains true –Psuedocode while there are more items on my shopping list Purchase next item and cross it off my list –Body of the while loop is repeated until condition becomes false

15 The while Repetition Structure Example int product = 2; while ( product <= 1000 ) product = 2 * product;

16 The while Repetition Structure Expression Action true false

17 Repetition Example Counter-controlled repetition –Loop repeated until counter reaches a certain value. Definite repetition –Known number of repetitions A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz.

18 Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average Repetition Example Pseudocode

19 #include using namespace std; int main() { int total = 0; // sum of the grades int grade; // individual grade int gradeCnt = 1; // number of grades entered while (gradeCnt <= 10) { cout << “Enter grade: ” << gradeCnt << “ ”; cin >> grade; total += grade; gradeCnt++; } // end while(gradeCnt <= 10) cout << “Class average is: “ << total / gradeCnt; cout << endl; return 0; } // end main() Repetition Example

20 Enter grade: 1 98 Enter grade: 2 76 Enter grade: 3 71 Enter grade: 4 87 Enter grade: 5 83 Enter grade: 6 90 Enter grade: 7 57 Enter grade: 8 79 Enter grade: 9 82 Enter grade: Class average is: 81 Repetition Example

21 Sentinel-Controlled Repetition Suppose the problem becomes: Develop a class-averaging program that will process an arbitrary number of grades each time the program is run. –Unknown number of students How will the program know to end?

22 Sentinel-Controlled Repetition Sentinel value –Indicates “end of data entry” –Loop ends when sentinel inputted –Sentinel value selected so it cannot be confused with a regular input Such as -1 in this case

23 Sentinel-Controlled Repetition Top-down, stepwise refinement –Begin with a pseudocode representation of the top: Determine the class average for the quiz –Divide top into smaller tasks and list them in order: Define and initialize variables Input, sum and count the quiz grades Calculate and print the class average

24 Sentinel-Controlled Repetition Many programs can be divided into three phases: –Definition and initialization Define and initialize program variables –Processing Inputs data values Adjusts program variables –Termination Calculates and prints the final results

25 Sentinel-Controlled Repetition Initialize total to zero Initialize counter to zero Input the first grade (possibly the sentinel) While the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade counter Input the next grade (possibly the sentinel) If the counter is not equal to zero Set the average to the total divided by the counter Print the average Else Print “No grades were entered” Pseudocode solution

26 #include using namespace std; int main() { int total = 0; // sum of the grades int grade; // individual grade int gradeCnt = 0; // number of grades entered cout << “Enter grade, -1 to quit “; cin >> grade; while (grade != -1) { total += grade; gradeCnt++; cout << “Enter grade, -1 to quit “; cin >> grade; } // end while(grade != -1) Repetition Example

27 if (gradeCnt != 0) { cout << “Class average: “ << total / gradeCnt; cout << endl; } else { cout << “No grades to average, duh!” << endl; } return 0; } // end main() Repetition Example