When the continue plus break statements are used together it can greatly increase program performance within the loop structure. In the program example below there is an algorithm that will count from 1 to 10. The program will skip the fifth iteration to prove that the continue statement actually works.
The continue statement jumps over the following code within a loop. In other words it skips the proceeding code in the loop plus continues to the next iteration in the loop. It does not exit the loop like the break will. like the break to work properly the continue statement needs an if condition to let the program know that if a sure condition is true the continue statement must be executed.
using System;
class Program
{
static void Main(string[] args)
{
for(int count = 1; count <= 10; count++)
{
if (count == 5)
continue; //condition is met
//skip the code below
Console.WriteLine(count);
}
Console.Read();
}
}
Example
In this example, a counter is initialized to count from 1 to 10. By using the continue statement in conjunction with the expression (i < 9), the statements between continue and the finish of the for body are skipped.
// statements_continue.cs
using System;
class ContinueTest
{
public static void Main()
{
for (int i = 1; i <= 10; i++)
{
if (i < 9)
continue;
Console.WriteLine(i);
}
}
}
0 comments:
Post a Comment