The ‘while’ loop ‘round and ‘round we go.

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

Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
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.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
The ‘while’ loop ‘round and ‘round we go.
Introduction to Computers and Programming Lecture 7:
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Introduction to Computer Programming
while Repetition Structure
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
- Standard C Statements
Lecture 7: Repeating a Known Number of Times
Week 4 – Repetition Structures / Loops
REPETITION STATEMENTS
Repetition-Counter control Loop
Ch 7: JavaScript Control Statements I.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Control Structures Lecture 7.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
The while Looping Structure
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Structured Program
Chapter 8 The Loops By: Mr. Baha Hanene.
1) C program development 2) Selection structure
Chapter 3 - Structured Program Development
The while Looping Structure
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
Let’s all Repeat Together
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays I Handling lists of data.
CPS125 Week
EPSII 59:006 Spring 2004.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Loops.
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
The while Looping Structure
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
Incremental Programming
The while Looping Structure
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

The ‘while’ loop ‘round and ‘round we go

A Repetitive Structure - The while Loop A repetitive structure allows the programmer to specify that an action is to be repeated while some condition remains true. Example in pseudo-code: while there are still more children subtract one from the # of children multiply the # of cookies by 2 end_while

Our example while loop while ( children > 0 ) { children = children - 1; cookies = cookies * 2 ; }

Another while loop example Problem: We want a program that calculates the average exam grade for a class of 10 students. What variables will we need ? total counter Do we need to initialize them to some value ? YES total = 0 counter = 1

The pseudo-code Set total to zero Set grade counter to one While grade counter is less than or equal to ten Print “Enter another grade” Read grade Add the grade into the total Add one to the grade counter end_while Set the class average to the total divided by ten Print the class average

Class Grade Average for 10 Students #include <stdio.h> main ( ) { int counter, grade, total, average ; total = 0 ; counter = 1 ; while ( counter <= 10 ) printf (“Enter grade : “); scanf (“%d”, &grade); total = total + grade ; counter = counter + 1; } average = total / 10 ; printf (“Class average is %d\n”, average);

Class Grade Average Using a Constant #include <stdio.h> #define STUDENTS 10 main ( ) { int counter, grade, total, average ; total = 0 ; counter = 1 ; while ( counter <= STUDENTS ) printf (“Enter grade : “); scanf (“%d”, &grade); total = total + grade ; counter = counter + 1; } average = total / STUDENTS; printf (“Class average is %d\n”, average);

Versatile ? How good is this program ? Only works with class sizes of 10 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.

A better algorithm Grade Average for N Students Set total to zero Set grade counter to one Get number of students, nrStudents While grade counter is <= nrStudents Input the next grade Add the grade into the total Add one to the grade counter end_while Set the class average to the total / nrStudents Print the class average

Grade Average for N students #include <stdio.h> main ( ) { int nrStudents; counter, grade, total, average ; total = 0 ; counter = 1 ; printf (“Enter Number of Students: “); scanf (“%d”, &nrStudents); while ( counter <= nrStudents) { printf (“Enter grade : “); scanf (“%d”, &grade); total = total + grade ; counter = counter + 1; } average = total / nrStudents ; printf (“Class average is %d\n”, average);

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 the 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 grade. (i.e.. can’t use 0 as the sentinel )

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.

Pseudo-code for Using a Sentinel to End a while Loop’s Execution Initialize total to 0 Initialize counter to 0 Get the first grade from the user While the grade != the sentinel value Add grade to total Add 1 to counter Get the next grade (could be sentinel) end_while average = total / counter Print the average

Using a Sentinel to End a while Loop #include <stdio.h> main ( ) { float average; int counter, grade, total; total = counter = 0; /* tell user the sentinel value in the prompt*/ printf (“Enter grade, -1 to end : “); scanf (“%d”, &grade) ; /* priming read */

Using a Sentinel (continued) while (grade != -1) { total = total + grade ; counter = counter + 1 ; printf (“Enter grade, -1 to end : “); scanf (“%d”, &grade); } average = ( float ) total / counter ; printf (“The average was %.2f\n”, average;

The cast operator ( ) We can use a cast operator to create a temporary value of the desired type, to be used in a calculation. Does NOT change the variable’s type or how it is stored. Is only good for the statement it’s in. Often used to avoid integer division. Used anytime we want to temporarily change a type for a calculation.

What Happens When We Cast ? Before the calculation statement During the calculation After the calculation counter total average 38 2890 garbage int int float same value as total counter total average 38 2890 garbage 2890.0000 int int float float counter total average 38 2890 76.0526

Using a while Loop to Check User Input main ( ) { int num ; printf (“Enter a positive integer : “) ; scanf (“%d”, &num) ; while ( num < 0 ) printf (“\nThat’s incorrect, try again\n”); } printf (“You entered %d\n”,num);