Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Оператор Continue | Цикли
Основи C#

bookОператор Continue

The continue statement is used for skipping to the next iteration of the loop.

When we use the continue statement, any code which follows it inside the code block is ignored and the loop skips to the next iteration.

Let's look at an example:

You can see in the above code, the second and third Console.WriteLine methods are ignored in every iteration.

Similarly, we can also conditionally ignore code using the continue statement:

main.cs

main.cs

copy
1234567891011121314151617
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { for(int i = 0; i < 5; i++) { if(i == 3) { continue; } Console.WriteLine(i); } } } }

In the above example, the Console.WriteLine statement is ignored in the 4th iteration.

A more practical example of the continue statement is the following code:

main.cs

main.cs

copy
1234567891011121314151617
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { for(int i = 0; i < 10; i++) { if(i % 2 != 0) { continue; } Console.WriteLine(i); } } } }

The above code iterates from numbers 0 to 9. It checks if i is odd using the condition i % 2 != 0 since an odd number divided by 2 always gives a non-zero remainder. If a number is odd, it skips to the next iteration, and if a number is even it does not skip and hence outputs i. This way it outputs all the even numbers between 0 to 9.

question mark

What does the continue statement do in a loop?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 7

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain the difference between continue and break in more detail?

Can you show more practical use cases for the continue statement?

How does the continue statement behave in nested loops?

bookОператор Continue

Свайпніть щоб показати меню

The continue statement is used for skipping to the next iteration of the loop.

When we use the continue statement, any code which follows it inside the code block is ignored and the loop skips to the next iteration.

Let's look at an example:

You can see in the above code, the second and third Console.WriteLine methods are ignored in every iteration.

Similarly, we can also conditionally ignore code using the continue statement:

main.cs

main.cs

copy
1234567891011121314151617
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { for(int i = 0; i < 5; i++) { if(i == 3) { continue; } Console.WriteLine(i); } } } }

In the above example, the Console.WriteLine statement is ignored in the 4th iteration.

A more practical example of the continue statement is the following code:

main.cs

main.cs

copy
1234567891011121314151617
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { for(int i = 0; i < 10; i++) { if(i % 2 != 0) { continue; } Console.WriteLine(i); } } } }

The above code iterates from numbers 0 to 9. It checks if i is odd using the condition i % 2 != 0 since an odd number divided by 2 always gives a non-zero remainder. If a number is odd, it skips to the next iteration, and if a number is even it does not skip and hence outputs i. This way it outputs all the even numbers between 0 to 9.

question mark

What does the continue statement do in a loop?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 7
some-alt