C# While Loop Examples

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is the condition for the while loop to continue executing in the basic example?

  • i is not equal to 5
  • i is greater than 5
  • i is less than 5 (correct)
  • i is equal to 5

What is the purpose of the while loop in the reading user input example?

  • To display a message to the user
  • To prompt the user to enter their name once
  • To prompt the user to enter their name until they enter a valid name (correct)
  • To exit the program

What happens to the variable seconds in each iteration of the while loop in the simulating a timer example?

  • It is assigned a value of 10
  • It is incremented by 1
  • It is decremented by 1 (correct)
  • It is reset to 0

What is the purpose of the System.Threading.Thread.Sleep(1000) line in the simulating a timer example?

<p>To wait for 1 second (B)</p> Signup and view all the answers

What is the output of the basic example when i is equal to 4?

<p>The value 4 will be displayed (C)</p> Signup and view all the answers

Flashcards are hidden until you start studying

Study Notes

While Loop Examples

The while loop is a fundamental control flow statement in C# that allows code to be executed repeatedly while a certain condition is true. Here are some examples of using while loops:

Basic Example

  • Initialize a variable i with a value of 0.
  • The loop continues to execute as long as i is less than 5.
  • In each iteration, i is incremented by 1.
int i = 0;
while (i &lt; 5)
{
    Console.WriteLine(i);
    i++;
}

Reading User Input

  • Prompt the user to enter their name.
  • Continue to prompt the user until they enter a non-empty string.
string name;
while (true)
{
    Console.Write("Enter your name: ");
    name = Console.ReadLine();
    if (!string.IsNullOrWhiteSpace(name))
    {
        break;
    }
    Console.WriteLine("Please enter a valid name.");
}

Simulating a Timer

  • Initialize a variable seconds with a value of 10.
  • The loop continues to execute as long as seconds is greater than 0.
  • In each iteration, seconds is decremented by 1.
int seconds = 10;
while (seconds &gt; 0)
{
    Console.WriteLine($"Time remaining: {seconds} seconds");
    seconds--;
    System.Threading.Thread.Sleep(1000); // wait for 1 second
}
Console.WriteLine("Time's up!");

While Loop Examples

Basic Example

  • Initialize a variable i with a value of 0, and the loop continues to execute as long as i is less than 5.
  • In each iteration, i is incremented by 1, and the current value of i is printed to the console.

Reading User Input

  • Continuously prompt the user to enter their name until a non-empty string is entered.
  • If an empty string is entered, the user is prompted again, and an error message is displayed.

Simulating a Timer

  • Initialize a variable seconds with a value of 10, and the loop continues to execute as long as seconds is greater than 0.
  • In each iteration, seconds is decremented by 1, and the remaining time is printed to the console.
  • The program waits for 1 second between each iteration using System.Threading.Thread.Sleep(1000).
  • When seconds reaches 0, a "Time's up!" message is displayed.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

C# Properties
10 questions

C# Properties

DiversifiedConstructivism avatar
DiversifiedConstructivism
Programación en C# con Arrays
8 questions
C# Files and Data Hierarchy
20 questions
.NET and C# Interview Questions
40 questions
Use Quizgecko on...
Browser
Browser