Loops1 - While Loop Purpose of a Loop: Often times we will...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
While Loop Purpose of a Loop: Often times we will want to repeat a particular group of statements multiple times. A loop gives us a control structure to do so, without having to write out the steps multiple times. Here is the general syntax of a while loop: while (<boolean expression>) stmt; Remember that just as we can use a block of statements in an if statement, we can do the same with a while. In fact, the body of most while loops is more than a single statement. Thus, most while loops look like: while (<boolean expression>) { stmt1; stmt2; ... stmtn; } stmtA; Here is how this executes: 1) Evaluate the boolean condition. 2) If it's true, execute statements 1 through n in order. 3) If it's false, skip to after the end of the while loop and execute stmtA. 4) After you execute stmtn, you have complete a loop iteration. How, go back to step #1 in these directions and repeat.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithm to figure out all the money you have for a booze run #include <stdio.h> int main() { double value, total = 0; char answer, dummy ; printf ("Does anyone have any money?\n"); scanf ); while ( answer == 'y' || answer == 'Y' ) { printf ("Enter the amount of your donation.\n"); scanf ("%lf",&value); total += value; printf ("Does anyone have any money?\n"); scanf ("%c%c",&dummy,&answer); } printf("As a group, you have collected $%lf for beer.\n", total); return 0; }
Background image of page 2
Class Exercise: Write a program that computes 1+3+. ..+99 #include <stdio.h> int main() { int val = 1 ; int sum = 0 ;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/09/2009 for the course COP 3223 taught by Professor Guha during the Fall '08 term at University of Central Florida.

Page1 / 10

Loops1 - While Loop Purpose of a Loop: Often times we will...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online