Ms. Terkper's Digital Classroom

Basics of C# 3: Conditionals and Loops

Basics of C# 3! This module covers conditional statements (if, else, switch)** and loops (for, while, do-while).

What are Conditionals?

Conditionals allow your program to make **decisions** based on certain conditions.

            
int age = 16;
if (age >= 18) {
    Console.WriteLine("You can vote.");
} else {
    Console.WriteLine("You are too young to vote.");
}
            
        

What are Loops?

Loops repeat a block of code multiple times based on a condition.

            
for (int i = 0; i < 5; i++) {
    Console.WriteLine("Iteration: " + i);
}
            
        

Questions

1. Which keyword is used for a conditional statement in C#?

2. How many times will this loop run?

                
for (int i = 0; i < 3; i++) {
    Console.WriteLine(i);
}
                
            

3. What will be the output of the following C# code?

        
int x = 10;
if (x > 5)
{
    Console.WriteLine("Greater");
}
else
{
    Console.WriteLine("Smaller");
}
        
    

4. What is the correct syntax for a `while` loop in C#?

5. How many times will the following `do-while` loop execute?

        
int i = 5;
do
{
    Console.WriteLine(i);
    i++;
} while (i < 5);