Previously Repetition Structures While, Do-While, For.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
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.
Selection Structures (if & switch statements) (CS1123)
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 05 (Part III) Control Statements: Part II.
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.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
1 CS161 Introduction to Computer Science Topic #8.
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.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.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.
Branching statements.
Introduction to Computer Programming
Decision Making in C.
REPETITION CONTROL STRUCTURE
ECE Application Programming
while Repetition Structure
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Engineering Problem Solving with C++, Etter/Ingber
Programming Fundamentals
Control Statements Kingdom of Saudi Arabia
Programming Fundamentals
The while Looping Structure
The while Looping Structure
Control Structures Part 3
Let’s all Repeat Together
2.6 The if/else Selection Structure
Branching statements Kingdom of Saudi Arabia
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
The while Looping Structure
The while Looping Structure
Programming Fundamental
Presentation transcript:

Previously Repetition Structures While, Do-While, For

Example Problem: Write a program that calculates the average exam grade for a class of 10 students. What are the program inputs? –the exam grades What are the program outputs? –the average exam grade

The Pseudocode Set total to 0 Let the grade_counter = 1 While (grade_counter <= 10) Display “Enter a grade: ” Read grade total = total + grade grade_counter = grade_counter + 1 End while average = total / 10 Display “Class average is: “, average

The C++ Code #include using namespace std; int main ( ) { int counter, grade, total, average ; total = 0 ; counter = 1 ; while ( counter <= 10 ) { cout<<“Enter a grade : “ ; cin>>grade; total = total + grade ; counter = counter + 1 ; } average = total / 10 ; cout<<“Class average is: ”<< average<<“\n” ; return 0 ; }

Class Exercise Convert the above while loop to a for loop

The above program only works with class sizes of 10. We would like it to work with any class size. A better way : –Ask the user how many students are in the class. Use that number in the condition of the while loop and when computing the average.

New Pseudocode Set total to 0 Let grade_counter = 1 Display “Enter the number of students: “ Read num_students While (grade_counter ) Display “Enter a grade: ” Read grade total = total + grade grade_counter = grade_counter + 1 End_while average = total / Display “Class average is: “, average

#include using namespace std; int main ( ) { int numStudents, counter, grade, total, average ; total = 0 ; counter = 1 ; cout<<“Enter the number of students: “ ; cin>>numStudents ; while ( counter <= numStudents) { printf (“Enter a grade : “) ; cin>>grade ; total = total + grade ; counter = counter + 1 ; } average = total / numStudents ; cout<<“Class average is: ”<< average<<“\n” ; return 0 ; }

The Switch Statement

It allows a variable to be tested for equality against a list of values Each value is called a case The variable being switched on is checked for each case

Syntax for a Switch Statement switch (expression) { case constant – expression : Statement(s); break; /* you can have any number of case statements*/ default: statement(s); }

Rules for a Switch Statement The expression used in a switch must have an integral type Have any number of case statements within a switch –Each case is followed by the value to be compared to and a semi colon The constant expression in the switch should be of the same data type as the variable in the switch

Rules for a Switch When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached The switch terminates when a break statement is reached and the flow of control jumps to the next line following the switch statement

Rules for a Switch Not every case needs a break statement. –If there is no break, the flow of control will fall through to subsequent cases until a break is reached A switch statement can have an optional default case which must appear at the end of the switch –If none of the cases is true, the default case is executed –No break is required for the default

Example 1 #include using namespace std; int main (){ // local variable declaration: char grade = 'D'; switch(grade) { case 'A' : cout << "Excellent!"<<endl; break; case 'B' : case 'C' :

cout << "Well done“ <endl; break; case 'D' : cout<<"You passed"<<endl; break; case 'F' : cout<<"Better try again”<<endl; break; default : cout << "Invalid grade\n“; } cout << "Your grade is “; cout<<grade << endl; return 0; }

Class Exercise Using a Switch statement, –Write a program in C++ that captures an integer in the range 1-7 from a user and prints out the corresponding day of the week. Let the user know if a value entered is out of range. Write the above program using if-else Make your code re-usable –Use any loop to let your program run 15 times