The while Looping Structure

Slides:



Advertisements
Similar presentations
CMSC 104, Version 9/011 The while Looping Structure Topics The while Loop Program Versatility o Sentinel Values and Priming Reads Checking User Input Using.
Advertisements

COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
C Lecture Notes 1 Structured Program Development.
Previously Repetition Structures While, Do-While, For.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
The ‘while’ loop ‘round and ‘round we go.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
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.
Introduction to Computer Programming
Algorithm: procedure in terms of
while Repetition Structure
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
- Standard C Statements
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
REPETITION STATEMENTS
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Relational & Logical Operators
Arrays, Part 1 of 2 Topics Definition of a Data Structure
The while Looping Structure
Relational and Logical Operators
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Structured Program
Relational and Logical Operators
1) C program development 2) Selection structure
Chapter 3 - Structured Program Development
The ‘while’ loop ‘round and ‘round we go.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 3 - Structured Program Development
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
UMBC CMSC 104 – Section 01, Fall 2016
Relational and Logical Operators
Let’s all Repeat Together
Arrays, Part 1 of 2 Topics Definition of a Data Structure
EPSII 59:006 Spring 2004.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Relational and Logical Operators
More Loops Topics Counter-Controlled (Definite) Repetition
Loops.
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
Relational and Logical Operators
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
The while Looping Structure
Relational and Logical Operators
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
The while Looping Structure
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

The while Looping Structure Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Reading Section 3.7

Review: Repetition Structure A repetition structure allows the programmer to specify that an action is to be repeated while some condition remains true. There are three repetition structures in C, the while loop, the for loop, and the do-while loop.

The while Repetition Structure while ( condition ) { statement(s) } The braces are not required if the loop body contains only a single statement. However, they are a good idea and are required by the 104 C Coding Standards.

Example while ( children > 0 ) { children = children - 1 ; cookies = cookies * 2 ; }

Good Programming Practice Always place braces around the body of a while loop. Advantages: Easier to read Will not forget to add the braces if you go back and add a second statement to the loop body Less likely to make a semantic error Indent the body of a while loop 3 to 4 spaces -- be consistent!

Another while Loop 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 <total> = 0 <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 <stdio.h> int main ( ) { int counter, grade, total, average ; total = 0 ; counter = 0; while ( counter <= 10 ) printf (“Enter a grade : “) ; scanf (“%d”, &grade) ; total = total + grade ; counter = counter + 1 ; } average = total / 10 ; printf (“Class average is: %d\n”, average) ; return 0 ;

Versatile? How versatile is this program? It 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 <total> = 0 <grade_counter> = 1 Display “Enter the number of students: “ Read <num_students> While (<grade_counter> <= <num_students>) Display “Enter a grade: ” Read <grade> <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 End_while <average> = <total> / <num_students> Display “Class average is: “, <average>

New C Code #include <stdio.h> int main ( ) { int numStudents, counter, grade, total, average ; total = 0 ; counter = 0 ; printf (“Enter the number of students: “) ; scanf (“%d”, &numStudents) ; while ( counter <= numStudents) { printf (“Enter a grade : “) ; scanf (“%d”, &grade) ; total = total + grade ; counter = counter + 1 ; } average = total / numStudents ; printf (“Class average is: %d\n”, average) ; return 0 ;

Why Bother to Make It Easier? Why do we write programs? So the user can perform some task The more versatile the program, the more difficult it is to write. BUT it is more useable. The more complex the task, the more difficult it is to write. But that is often what a user needs. Always consider the user first.

Using a Sentinel Value We could let the user keep entering grades and when he’s done enter some special value that signals us that he’s done. This special signal value is called a sentinel value. We have to make sure that the value we choose as the sentinel isn’t a legal value. For example, we can’t use 0 as the sentinel in our example as it is a legal value for an exam score.

The Priming Read When we use a sentinel value to control a while loop, we have to get the first value from the user before we encounter the loop so that it will be tested and the loop can be entered. This is known as a priming read. We have to give significant thought to the initialization of variables, the sentinel value, and getting into the loop.

New Pseudocode <total> = 0 <grade_counter> = 1 Display “Enter a grade: “ Read <grade> While ( <grade> != -1 ) <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 Display “Enter another grade: ” Read <grade> End_while <average> = <total> / <grade_counter> Display “Class average is: “, <average>

New C Code #include <stdio.h> int main ( ) { int counter, grade, total, average ; total = 0 ; counter = 0 ; printf(“Enter a grade (-1 to quit): “) ; scanf(“%d”, &grade) ; while (grade != -1) { total = total + grade ; counter = counter + 1 ; printf(“Enter another grade (-1 to quit): “) ; }

New C Code (cont.) if (counter == 0){ printf(“No grades entered!!\n”); } else{ average = total / counter ; printf (“Class average is: %d\n”, average) ; return 0 ;

Final “Clean” C Code #include <stdio.h> int main ( ) { int counter ; /* counts number of grades entered */ int grade ; /* individual grade */ int total; /* total of all grades */ int average ; /* average grade */ /* Initializations */ total = 0 ; counter = 0;

Final “Clean” C Code (con’t) /* Get grades from user */ /* Compute grade total and number of grades */ printf(“Enter a grade: (-1 to quit)“) ; scanf(“%d”, &grade) ; while (grade != -1) { total = total + grade ; counter = counter + 1 ; printf(“Enter another grade (-1 to quit): “) ; }

Final “Clean” C Code (con’t) /* Check to make sure counter is not equal to 0 to */ /* avoid division by zero */ if (counter == 0){ printf(“No grades entered!!\n”); } else{ average = total / counter ; printf (“Class average is: %d\n”, average) ; return 0 ;

Using a while Loop to Check User Input #include <stdio.h> int main ( ) { int number ; printf (“Enter a positive integer : “) ; scanf (“%d”, &number) ; while ( number <= 0 ) printf (“\nThat’s incorrect. Try again.\n”) ; printf (“Enter a positive integer: “) ; } printf (“You entered: %d\n”, number) ; return 0 ;