Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara break Statement | Loops
C# Basics

bookbreak Statement

The break statement is used for breaking/stopping a loop mid-execution. It is useful in cases where we want to stop a loop in case some additional condition is met.

Following is an example of the break statement being used in the for loop:

main.cs

main.cs

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

The above code outputs i till 5 and then the loop stops. This is because of the additional conditional break we added.

We can also use the break statement in other loops:

main.cs

main.cs

copy
12345678910111213141516171819
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int i = 0; while(true) { Console.WriteLine(i); if(i == 5) { break; } i++; } } } }

The loop we constructed above works exactly the same as the for loop we looked at before. The break statement can be used in the do-while loops as well. The break statement is very useful in adding additional conditions to a loop.

question mark

How many iterations will the loop run for?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 6

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you show an example of using break in a while loop?

What happens if there is no break statement in the loop?

Can you explain the difference between break and continue?

Awesome!

Completion rate improved to 1.59

bookbreak Statement

Scorri per mostrare il menu

The break statement is used for breaking/stopping a loop mid-execution. It is useful in cases where we want to stop a loop in case some additional condition is met.

Following is an example of the break statement being used in the for loop:

main.cs

main.cs

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

The above code outputs i till 5 and then the loop stops. This is because of the additional conditional break we added.

We can also use the break statement in other loops:

main.cs

main.cs

copy
12345678910111213141516171819
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int i = 0; while(true) { Console.WriteLine(i); if(i == 5) { break; } i++; } } } }

The loop we constructed above works exactly the same as the for loop we looked at before. The break statement can be used in the do-while loops as well. The break statement is very useful in adding additional conditions to a loop.

question mark

How many iterations will the loop run for?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 6
some-alt