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.
Chapter 3 - Structured Program Development
Introduction to Computers and Programming Lecture 7:
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
The University of Texas – Pan American
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
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.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
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.
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 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
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
- Standard C Statements
Lecture 7: Repeating a Known Number of Times
Week 4 – Repetition Structures / Loops
Ch 7: JavaScript Control Statements I.
Chapter 4 – Control Structures Part 1
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Lecture 4B More Repetition Richard Gesick
Arrays, Part 1 of 2 Topics Definition of a Data Structure
The while Looping Structure
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Structured Program
1) C program development 2) Selection structure
Chapter 3 - Structured Program Development
The while Looping Structure
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
UMBC CMSC 104 – Section 01, Fall 2016
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
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
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
More Loops Topics Counter-Controlled (Definite) Repetition
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 cookies /* give one cookie to one child */ subtract one from the # of children subtract one from the # of cookies end_while

Our example while loop while ( children > 0 ) { children = children - 1; cookies = cookies - 1; } /* Note that if the condition is FALSE, the loop body will never be entered. */

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 What is the first thing we do to these variables?

Another While Loop Example We initialize them! 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); /* input from console */ total = total + grade ; counter = counter + 1; } average = total / 10 ; 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);