Menu
CoddyTech

While Loop

Part of the Fundamentals section of Coddy's C# journey. Lesson 42 of 70.

A while loop is different from the for loop. A for loop is usually used to count through a known range of values, whereas a while loop has only a condition and keeps iterating as long as that condition is met.

To use a while loop write:

while (condition) {
	code
}

The code will execute only if the condition is true.

A for loop could do the same work, but a while loop is clearer when you do not know in advance how many times the code must repeat.

challenge icon

Challenge

Beginner

Write a program that gets one input, double number.

Use a while loop to repeatedly divide the number by 2, and keep dividing as long as the current value is bigger or equal to 3.5.

Print the first number that is smaller than 3.5.

Try it yourself

using System;

public class Program {
    public static void Main(string[] args) {
        // Write your code below
        
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online C# compiler