The continue statement is used to skip the current iteration and move to the next iteration of the loop, It is used within loops, such as for, for each, while, and do while.
Syntax
continue;
Example 1. Continue in For Loop
Skip odd numbers and print only even numbers.
Process flow
- Initialization: The loop starts with int i = 1.
- Condition Check: The loop runs as long as i is less than or equal to 10.
- Increment: After each iteration, i is incremented by 1.
Inside the loop
- The condition if (i % 2 != 0) checks if i is an odd number.
- If i is odd (i % 2 != 0 evaluates to true), the continue statement is executed, skipping the rest of the loop body and proceeding to the next iteration.
- If I is even, the Console.WriteLine(i) statement is executed, printing the value of i.
Explanation
This pattern continues until I reach 10, ensuring that only even numbers are printed.
Result
Example 2. Continue in Foreach Loop
Skip odd numbers and print only even numbers.
Process flow
- Array Initialization: An array number is initialized with values from 0 to 10.
- Iteration: The foreach loop iterates over each element in the numbers array.
Inside the loop
- The condition if (number % 2 != 0) checks if the current number is odd.
- If the number is odd (number % 2 != 0 evaluates to true), the continue statement is executed, skipping the rest of the loop body and proceeding to the next iteration.
- If the number is even, the Console.WriteLine(number) statement is executed, printing the value of a number.
Explanation
This pattern continues for each element in the array, ensuring that only even numbers are printed.
Result
Example 3. Continue in While Loop
Skip odd numbers and print only even numbers.
Process flow
- Initialization: Start with an index variable i set to 0.
- Condition Check: The loop runs as long as i is less than or equal to 10.
- Inside the Loop: Check if i is odd using the condition if (i % 2 != 0).
- If the condition is true (i.e., i is odd), increment i and use the continue statement to skip the rest of the loop body, moving to the next iteration.
- If the condition is false (i.e., i is even), print the value of i and then increment i.
Explanation
Once I reach 11, the condition I <= 10 becomes false, and the loop exits. This ensures that only even numbers from 0 to 10 are printed.
Result
Example 4. Continue in do While Loop
Skip odd numbers and print only even numbers.
Process flow
- Initialization: Start with an index variable I set to 0.
- Condition Check: The loop runs as long as i is less than or equal to 10.
- Inside the Loop: Check if i is odd using the condition if (i % 2 != 0).
- If the condition is true (i.e., i is odd), increment i and use the continue statement to skip the rest of the loop body, moving to the next iteration.
- If the condition is false (i.e., i is even), print the value of i and then increment i.
Explanation
Once I reach 11, the condition I <= 10 becomes false, and the loop exits. This ensures that only even numbers from 0 to 10 are printed.
Result