Difference between revisions of "Repetition - Iteration"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
Line 1: Line 1:
 
Repetition is the use of a for loop to execute one piece of code over and over.
 
Repetition is the use of a for loop to execute one piece of code over and over.
 
There are multiple ways to create a for loop though. These are:
 
There are multiple ways to create a for loop though. These are:
* while uses one condition that which if met, will repeat the code.
+
* While uses one condition that which if met, will repeat the code.
 
<syntaxhighlight lang="csharp" line>            while (true)
 
<syntaxhighlight lang="csharp" line>            while (true)
 
                 {
 
                 {
 
                   x ++
 
                   x ++
 
                 }</syntaxhighlight>
 
                 }</syntaxhighlight>
 +
* For loops intialise a variable, check a condition and perform an action. This is done before each iteration.
 +
<syntaxhighlight lang="csharp" line>for (int i = 0; i <= 1000000; i++)
 +
                    {
 +
                    }</syntaxhighlight>
 +
*Do while is the same as a while, however it doesn't check until it has done one iteration
 +
<syntaxhighlight lang="csharp" line> do {
 +
                                          x ++
 +
                                        }
 +
                                        while (true)</syntaxhighlight>

Revision as of 11:02, 15 December 2016

Repetition is the use of a for loop to execute one piece of code over and over. There are multiple ways to create a for loop though. These are:

  • While uses one condition that which if met, will repeat the code.
1              while (true)
2                 {
3                    x ++
4                 }
  • For loops intialise a variable, check a condition and perform an action. This is done before each iteration.
1 for (int i = 0; i <= 1000000; i++)
2                     {
3                     }
  • Do while is the same as a while, however it doesn't check until it has done one iteration
1  do {
2                                           x ++
3                                          }
4                                         while (true)