Download If Else Statment-Fundamentals of Computer-Lecture Slides and more Slides Computer Fundamentals in PDF only on Docsity! . 18 3.6 The if…else Selection Statement • Flow chart of the if…else selection statement • Nested if…else statements – Test for multiple cases by placing if…else selection statements inside if…else selection statement – Once condition is met, rest of statements skipped – Deep indentation usually not used in practice true false print “Failed” print “Passed” grade >= 60 docsity.com . 19 3.6 The if…else Selection Statement – Pseudocode for a nested if…else statement If student’s grade is greater than or equal to 90 Print “A” else If student’s grade is greater than or equal to 80 Print “B” else If student’s grade is greater than or equal to 70 Print “C” else If student’s grade is greater than or equal to 60 Print “D” else Print “F” docsity.com . 22 3.7 The while Repetition Statement • 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 – while loop repeated until condition becomes false docsity.com . 23 3.7 The while Repetition Statement • Example: int product = 2; while ( product <= 1000 ) product = 2 * product; product <= 1000 product = 2 * product true false docsity.com . 24 3.8 Formulating Algorithms (Counter-Controlled Repetition) • Counter-controlled repetition – Loop repeated until counter reaches a certain value – Definite repetition: number of repetitions is known – Example: 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 – Pseudocode: 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 docsity.com . 27 3.9 Formulating Algorithms with Top- Down, Stepwise Refinement • 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? • Use sentinel value – Also called signal value, dummy value, or flag value – Indicates “end of data entry.” – Loop ends when user inputs the sentinel value – Sentinel value chosen so it cannot be confused with a regular input (such as -1 in this case) docsity.com . 28 3.9 Formulating Algorithms with Top- Down, Stepwise Refinement • 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: Initialize variables Input, sum and count the quiz grades Calculate and print the class average • Many programs have three phases: – Initialization: initializes the program variables – Processing: inputs data values and adjusts program variables accordingly – Termination: calculates and prints the final results docsity.com . 29 3.9 Formulating Algorithms with Top- Down, Stepwise Refinement • Refine the initialization phase from Initialize variables to: Initialize total to zero Initialize counter to zero • Refine Input, sum and count the quiz grades to 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) docsity.com
oOMOAnN Ooh WN =
10
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* Fig. 3.8: fig03_08.c
Class average program with sentinel-controlled repetition */
#include <stdio.h>
/* function main begins program execution */
int mainQ)
{
int counter; /* number of grades entered */
int grade; /* grade value */
int total; /* sum of grades */
float average; /* number with decimal point for average */
/* initialization phase */
total = 0; /* initialize total */
counter = 0; /* initialize loop counter */
/* processing phase */
/* get first grade from user */
printf( "Enter grade, -1 to end: " ); /* prompt for input */
scanf( “%d", &grade ); /* read grade from user */
/* loop while sentinel value not yet read from user */
while € grade != -1 ) {
total = total + grade; /* add grade to total */
counter = counter + 1; /* increment counter */
32
A Outline
v
fig03_08.c (Part 1
of 2)
docsity.com
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
printf( “Enter grade, -1 to end: " ); /* prompt for input */
scanf('%d", &grade) ; /* read next grade */
} /* end while */
/* termination phase */
/* if user entered at least one grade */
if ( counter !=0) {
/* calculate average of all grades entered */
average = ( float ) total / counter;
/* display average with two digits of precision */
printf( "Class average is %.2f\n", average );
} /* end if */
else { /* if no grades were entered, output message */
printf( "No grades were entered\n" );
} /* end else */
return 0; /* indicate program ended successfully */
48 } /* end function main */
e
A Outline
Vv
33
fig03_08.c (Part 2 of
2)
docsity.com
Outline . 34 Program Output Enter grade, -1 to end: 75 Enter grade, -1 to end: 94 Enter grade, -1 to end: 97 Enter grade, -1 to end: 88 Enter grade, -1 to end: 70 Enter grade, -1 to end: 64 Enter grade, -1 to end: 83 Enter grade, -1 to end: 89 Enter grade, -1 to end: -1 Class average is 82.50 Enter grade, -1 to end: -1 No grades were entered docsity.com