{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

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.

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

View Full Document
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 ("%c",&answer ); 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; }
Class Exercise: Write a program that computes 1+3+...+99 #include <stdio.h>

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online