Difference between revisions of "Loops"

From CompSciWiki
Jump to: navigation, search
(Infinite Loops)
(Added a link to the operators section from chapter 2 into the intro.)
 
(67 intermediate revisions by 8 users not shown)
Line 1: Line 1:
==TO DO LIST (in any order)==
+
{{1010Chapter|ChapterNum=5
* Discuss WHAT a loop is. (Definition)
+
|Picture=Wiki_loops03.jpg
* Types: while, for, do, etc...
+
|Introduction=Until now all your programs have been working from top to bottom. Imagine if you were to write a game to guess a secret number from 1 to 100. The entire program would consist of one hundred [[Control Structures#The If Statement|if statements]] to account for each turn to check and see if the number chosen is correct; a situation like this is where a loop will come in to play and reduce those one hundred statements into one.
* Bottom tested, top tested
+
* Terms:
+
** Initializing
+
** [[test condition|Condition]] (basic definition because we have a section below) -done
+
** etc...
+
  
==An Introduction==
+
A loop is a [[Control Structures|control structure]] that allows you to repeat the same sequence of code as long as a given [[test condition]] evaluates to true. Remember that the test condition uses [[Java Fundamentals#Operators|operators]]. Every passage through this sequence of code is called an ''iteration''.  If you repeat the same sequence of code 20 times, then you have performed 20 iterations.
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==
+
|Body=
*[[While Loops|while]]
+
==[[Looping]]==
*[[for]]
+
==[[While Loops]]==
*[[nested loops]]
+
==[[Do-While Loops]]==
 +
==[[For Loops]]==
 +
==[[Nested Loops]]==
 +
==[[Scope]]==
 +
==[[Infinite Loops]]==
 +
==[[Multiple Control Statements]]==
 +
==[[Running Totals and Sentinel Values]]==
 +
==[[Loop Review Questions and Exercises|Review Questions and Exercises]]==
  
==[[Test_condition|Test Condition]]==
 
 
==[[Scope]]==
 
  
==Infinite Loops==
+
|PChapterNum=4
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:
+
|PChapterLink=[[Control Structures]]
 +
|NChapterNum=6
 +
|NChapterLink=[[User Defined Methods]]
 +
}}
  
<pre>
 
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
 
{
 
}
 
</pre>
 
  
==Additional Information==
+
{{Category:COMP 1010}}
*[[more on loops|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
+

Latest revision as of 21:38, 8 December 2011


Wiki 1010 Table of Contents

Wiki loops03.jpg

Chapter 5

Until now all your programs have been working from top to bottom. Imagine if you were to write a game to guess a secret number from 1 to 100. The entire program would consist of one hundred if statements to account for each turn to check and see if the number chosen is correct; a situation like this is where a loop will come in to play and reduce those one hundred statements into one.

A loop is a control structure that allows you to repeat the same sequence of code as long as a given test condition evaluates to true. Remember that the test condition uses operators. Every passage through this sequence of code is called an iteration. If you repeat the same sequence of code 20 times, then you have performed 20 iterations.

  Write a Program a Day Case Studies

Looping

While Loops

Do-While Loops

For Loops

Nested Loops

Scope

Infinite Loops

Multiple Control Statements

Running Totals and Sentinel Values

Review Questions and Exercises




Chapter 4: Control Structures Table of Contents Chapter 6: User Defined Methods



Template loop detected: