Changes

Jump to: navigation, search

C Sharp Looping with do and while Statements

1,092 bytes added, 19:20, 17 January 2008
C# do ... while loops
i--;
} while (i > 0)
</pre>
 
== Breaking from Loops ==
 
As with the for loop, it is also possible to exit from a ''while'' or ''do while'' loop at any time through the use of the ''break'' statement. When the execution path encounters a ''break'' statement the looping will stop and execution will proceed to the code immediately following the loop. In the following example, the loop is coded to exit when the value of ''i'' matches the value of ''j'':
 
<pre>
int i = 0;
 
int j = 5;
 
while (i < 100)
{
i++;
 
if (i == j)
break;
}
</pre>
 
It is important to note that in the case of nested loops the ''break'' statement only exists the current loop leaving the outer loop to continue executing (and most likely once again executing the inner loop).
 
== The continue Statement ==
 
The ''continue'' statement causes all remaining code statements in a loop to skipped, and execution to be returned to the top of the loop. In the following example, the System.Console.WriteLine method is only called when the value of variable 'i'' is an even number (i.e divisible by 2 with no remainder):
 
<pre>
</pre>

Navigation menu