Laço While
Um loop while é semelhante a um loop for, no entanto, é usado quando precisamos executar um bloco de código repetidamente com base em uma condição. A sintaxe de um loop while é mais simples do que a de um loop for:
while (condition) {
// code to execute
}
Exemplo:
main.cs
12345678910111213141516using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int i = 0; while(i < 5) { Console.WriteLine(i); i++; } } } }
Although the while
loop may seem very similar to the for
loop at first, In more advanced levels of programming the difference becomes apparent. We might explore the usage of different kinds of loops in the Arrays section.
Note
We can write
true
as the condition of a while-loop to create an infinite loop. But it's not recommended to use.
main.cs
123while(true) { Console.WriteLine("Hello World"); }
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 1.59
Laço While
Deslize para mostrar o menu
Um loop while é semelhante a um loop for, no entanto, é usado quando precisamos executar um bloco de código repetidamente com base em uma condição. A sintaxe de um loop while é mais simples do que a de um loop for:
while (condition) {
// code to execute
}
Exemplo:
main.cs
12345678910111213141516using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int i = 0; while(i < 5) { Console.WriteLine(i); i++; } } } }
Although the while
loop may seem very similar to the for
loop at first, In more advanced levels of programming the difference becomes apparent. We might explore the usage of different kinds of loops in the Arrays section.
Note
We can write
true
as the condition of a while-loop to create an infinite loop. But it's not recommended to use.
main.cs
123while(true) { Console.WriteLine("Hello World"); }
Obrigado pelo seu feedback!