Difference between revisions of "Repetition - Iteration"
m |
|||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | =Repetition= | |
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. | ||
<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 lang="csharp" line>for (int i = 0; i <= 1000000; i++) | ||
{ | { | ||
console.WriteLine(i); | console.WriteLine(i); | ||
}</syntaxhighlight> | }</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 | <syntaxhighlight lang="csharp" line>do | ||
{ | { | ||
Line 18: | Line 21: | ||
} | } | ||
while (true)</syntaxhighlight> | while (true)</syntaxhighlight> | ||
− | + | ||
+ | '''Foreach''' loops can also be used to access array variables [http://compsci.duckdns.org/mediawiki/index.php/Arrays] | ||
<syntaxhighlight lang="csharp" line>foreach (int x in Arr) | <syntaxhighlight lang="csharp" line>foreach (int x in Arr) | ||
{ | { | ||
Line 24: | Line 28: | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
− | + | =Iteration= | |
− | An iteration is | + | An iteration is the piece of code performed each time a loop runs. |
+ | |||
+ | Iterations can be skipped by calling the '''continue''' function. | ||
+ | |||
+ | <syntaxhighlight lang="csharp" line> | ||
+ | for (int i = 0; i <= 10; i++) | ||
+ | { | ||
+ | if(i==5) | ||
+ | { | ||
+ | continue; | ||
+ | } | ||
+ | console.WriteLine(i); | ||
+ | }</syntaxhighlight> | ||
+ | |||
+ | Iteration can be terminated with the '''break''' function. | ||
+ | |||
+ | <syntaxhighlight lang="csharp" line> | ||
+ | for (int i = 0; i <= 10; i++) | ||
+ | { | ||
+ | if(i==5) | ||
+ | { | ||
+ | break; | ||
+ | } | ||
+ | console.WriteLine(i); | ||
+ | }</syntaxhighlight> |
Latest revision as of 13:20, 29 June 2018
Repetition
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 console.WriteLine(i);
4 }
Do while is the same as a while, however it doesn't check until it has done one iteration.
1 do
2 {
3 x ++;
4 }
5 while (true)
Foreach loops can also be used to access array variables [1]
1 foreach (int x in Arr)
2 {
3 Console.WriteLine(x);
4 }
Iteration
An iteration is the piece of code performed each time a loop runs.
Iterations can be skipped by calling the continue function.
1 for (int i = 0; i <= 10; i++)
2 {
3 if(i==5)
4 {
5 continue;
6 }
7 console.WriteLine(i);
8 }
Iteration can be terminated with the break function.
1 for (int i = 0; i <= 10; i++)
2 {
3 if(i==5)
4 {
5 break;
6 }
7 console.WriteLine(i);
8 }