Presentation is loading. Please wait.

Presentation is loading. Please wait.

Previously Repetition Structures While, Do-While, For.

Similar presentations


Presentation on theme: "Previously Repetition Structures While, Do-While, For."— Presentation transcript:

1 Previously Repetition Structures While, Do-While, For

2 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

3 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

4 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 ; }

5 Class Exercise Convert the above while loop to a for loop

6 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.

7 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

8 #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 ; }

9 The Switch Statement

10 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

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

12 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

13 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

14 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

15 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' :

16 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; }

17 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


Download ppt "Previously Repetition Structures While, Do-While, For."

Similar presentations


Ads by Google