Difference between revisions of "Loops"

From CompSciWiki
Jump to: navigation, search
(Infinite Loops)
m (Infinite Loops)
Line 21: Line 21:
  
 
==Infinite Loops==
 
==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 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:
+
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:
  
 
<pre>
 
<pre>

Revision as of 15:55, 6 March 2007

TO DO LIST (in any order)

  • Discuss WHAT a loop is. (Definition)
  • Types: while, for, do, etc...
  • Bottom tested, top tested
  • Terms:
    • Initializing
    • Condition (basic definition because we have a section below) -done
    • etc...

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.

Types of Loops

Test Condition

Scope

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

  • more on loops
    • multiple statements in the Initialization and Update
    • brief explanation of do-while loops (and why bottom testing loops are not ideal)
    • Running Totals and Sentinel Values
    • Review Questions and Exercises