GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C# - Continue Statement
Selected Reading
C# - Continue StatementC# continue StatementThe continue statement in C# works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, continue statement causes the program control passes to the conditional tests. SyntaxThe syntax for a continue statement in C# is as follows − continue; Flow Diagram
Using continue Statement with do while LoopIn this example, we use the continue statement to skip the iteration when using System;
namespace Loops {
class Program {
static void Main(string[] args) {
/* local variable definition */
int a = 10;
/* do loop execution */
do {
if (a == 15) {
/* skip the iteration */
a = a + 1;
continue;
}
Console.WriteLine("value of a: {0}", a);
a++;
}
while (a < 20);
Console.ReadLine();
}
}
}
OutputWhen the above code is compiled and executed, it produces the following result − value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19 Using continue Statement with for LoopIn this example, we use the continue statement to skip the current iteration and move to the next iteration of the loop − using System;
class ContinueExample {
static void Main() {
for (int i = 1; i <= 10; i++) {
// Skip even numbers
if (i % 2 == 0) {
// Jump to the next iteration
continue;
}
Console.WriteLine("Odd number: " + i);
}
Console.WriteLine("Loop completed.");
}
}
OutputThe following is the output of the above code − Odd number: 1 Odd number: 3 Odd number: 5 Odd number: 7 Odd number: 9 Loop completed. Advertisements
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |