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.

Slides:



Advertisements
Similar presentations
More on Psuedo-Code Sangeetha Parthasarathy 1/23/01.
Advertisements

CMSC 104, Version 9/011 Top-Down Design Topics Top-Down Design Top-Down Design Examples The Function Concept Reading Sections 3.9, 3.10.
Decisions If statements in C.
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
An Introduction to Programming with C++ Fifth Edition
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
CMSC 1041 Algorithms III Problem Solving and Pseudocode.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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.
Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
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.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
CMSC 104, Version 8/061L05Algorithms2.ppt Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode Control Structures Reading Section 3.1.
Introduction to Computer Programming
while Repetition Structure
- Standard C Statements
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Control Statements: Part 1
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Loop Structures.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Ch 7: JavaScript Control Statements I.
Relational & Logical Operators
The while Looping Structure
Relational and Logical Operators
2008/09/22: Lecture 5 CMSC 104, Section 0101 John Y. Park
Structured Program
Relational and Logical Operators
1) C program development 2) Selection structure
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
The while Looping Structure
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
The ‘while’ loop ‘round and ‘round we go.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Relational & Logical Operators, if and switch Statements
Algorithms, Part 3 of 3 Topics In-Class Project: The Box
Relational and Logical Operators
Let’s all Repeat Together
A LESSON IN LOOPING What is a loop?
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Relational and Logical Operators
Loops.
Relational and Logical Operators
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Dale Roberts, Lecturer IUPUI
The while Looping Structure
Relational and Logical Operators
The while Looping Structure
Presentation transcript:

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 a while Loop Reading Section 3.7

CMSC 104, Version 9/012 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.

CMSC 104, Version 9/013 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.

CMSC 104, Version 9/014 Example while ( children > 0 ) { children = children - 1 ; cookies = cookies * 2 ; }

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

CMSC 104, Version 9/016 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? o the exam grades What are the program outputs? o the average exam grade

CMSC 104, Version 9/017 The Pseudocode = 0 = 1 While ( <= 10) Display “Enter a grade: ” Read = + = + 1 End_while = / 10 Display “Class average is: “,

CMSC 104, Version 9/018 #include int main ( ) { int counter, grade, total, average ; total = 0 ; counter = 1 ; 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 ; } The C Code

CMSC 104, Version 9/019 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 : o 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.

CMSC 104, Version 9/0110 New Pseudocode = 0 = 1 Display “Enter the number of students: “ Read While ( ) Display “Enter a grade: ” Read = + = + 1 End_while = / Display “Class average is: “,

CMSC 104, Version 9/0111 New C Code #include int main ( ) { int numStudents, counter, grade, total, average ; total = 0 ; counter = 1 ; 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 ; }

CMSC 104, Version 9/0112 Why Bother to Make It Easier? Why do we write programs? o 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.

CMSC 104, Version 9/0113 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.

CMSC 104, Version 9/0114 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.

CMSC 104, Version 9/0115 New Pseudocode = 0 = 1 Display “Enter a grade: “ Read While ( != -1 ) = + = + 1 Display “Enter another grade: ” Read End_while = / Display “Class average is: “,

CMSC 104, Version 9/0116 New C Code #include int main ( ) { int counter, grade, total, average ; total = 0 ; counter = 1 ; printf(“Enter a grade: “) ; scanf(“%d”, &grade) ; while (grade != -1) { total = total + grade ; counter = counter + 1 ; printf(“Enter another grade: “) ; scanf(“%d”, &grade) ; } average = total / counter ; printf (“Class average is: %d\n”, average) ; return 0 ; }

CMSC 104, Version 9/0117 Final “Clean” C Code #include 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 = 1 ;

CMSC 104, Version 9/0118 Final “Clean” C Code (con’t) /* Get grades from user */ /* Compute grade total and number of grades */ printf(“Enter a grade: “) ; scanf(“%d”, &grade) ; while (grade != -1) { total = total + grade ; counter = counter + 1 ; printf(“Enter another grade: “) ; scanf(“%d”, &grade) ; } /* Compute and display the average grade */ average = total / counter ; printf (“Class average is: %d\n”, average) ; return 0 ; }

CMSC 104, Version 9/0119 Using a while Loop to Check User Input #include 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: “) ; scanf (“%d”, &number) ; } printf (“You entered: %d\n”, number) ; return 0 ; }