Difference between revisions of "Loops"
From CompSciWiki
Line 33: | Line 33: | ||
==[[Additional Information]]== | ==[[Additional Information]]== | ||
*[[Additional Information#Multiple Control Statments|Multiple Control Statements]] | *[[Additional Information#Multiple Control Statments|Multiple Control Statements]] | ||
− | * | + | *[[Additional Information#Do-While Loops|The Evil Do-While Loop]] |
*Running Totals and Sentinel Values | *Running Totals and Sentinel Values | ||
*Review Questions and Exercises | *Review Questions and Exercises |
Revision as of 16:24, 6 March 2007
Contents
An Introduction
A loop is a control structure that repeatedly executes a sequence of steps for as long as a test condition evaluates to true. What does iterate mean?
Types of Loops
- while
- for -Roger
- nested loops -Roger
Test Condition
Loops use the test condition to decide whether or not to enter the body of the loop.
Scope
A loop will enter the body if and only if the test condition evaluates to true.
Infinite Loops
An infinite loop is any loop that continues to repeat forever. There are situations where this is useful, but if you didn't intentionally program it in, then it causes a fatal error for your program. These occur if the test condition in your loop will never be false. Here are a few examples of infinite loops:
while(true) //always true { } ------------------------------------------------ for(int i = 0; i < 20; i++) //counter reset to 0 every time { i = 0; } ------------------------------------------------ for(int i = 0; i < 5e9; i++) //integers can't be as big as 5e9(5*10^9), so i will overflow to negative { }
Additional Information
- Multiple Control Statements
- The Evil Do-While Loop
- Running Totals and Sentinel Values
- Review Questions and Exercises