Table of Contents
ToggleDefinition of a Control Statement in C#:
A control statement in C# is a programming instruction that determines the order in which other statements are executed. It controls the flow of a program by allowing the execution to branch (make decisions), loop (repeat actions), or jump (transfer control) based on specific conditions or requirements. Control statements make it possible to handle complex logic and dynamic behavior in a program.
Types of control statements
1.If statement:
The if statement checks whether a condition is true or false. If the condition is true, it runs a block of code; if not, it skips it. It helps make decisions in a program based on certain conditions.Syntax of if statement is
if (condition) { // Code if the condition is true }
2 if-else statements:
The if-else statement does the same as if, but also provides an alternative. If the if condition is true, it runs the first block; if false, it runs the second else block. It helps choose between two actions. The syntax of the if-else statement is
if (condition) { // Code if true } else { // Code if false }
3. if-else if-else statement:
This statement checks several conditions one by one. If the first condition is false, it checks the next. If none of the if or else if conditions are true, it runs the final else block. It is used for multiple-choice questions.
if (condition1) { // Code } else if (condition2) { // Code } else { // Code if none are true }
4. Switch statement
The switch Statement tests the value of a variable and matches it with case labels. It runs the code block that matches the value. If none match, the default block runs. It is an easier way to handle many fixed options. The syntax of the switch statement is
switch (variable) { case value1: // Code break; case value2: // Code break; default: // Code if no case matches break; }
5 Break statement:
The break statement stops the nearest loop switch immediately and moves control to the next statement after it. It is often used to exit a loop early when a condition is met. The syntax of the break statement is
for (int i = 0; i < 10; i++) { if (i == 5) break; }
6 Continue statement:
The continue statement skips the rest of the current loop step and jumps to the next iteration of the loop. It is used when you want to skip certain steps but keep the loop running. The syntax of the break statement is
for (int i = 0; i < 5; i++) { if (i == 2) continue; Console.WriteLine(i); }
7 Return statement:
The return statement ends a method and optionally sends a value back to the place where the method was called. It is used to finish a function’s work and pass results back. The syntax example of return statements is
int Add(int a, int b) { return a + b;
8 Goto statement:
The goto statement jumps to a labeled point in the code. It is rarely used because it can make code confusing, but sometimes it helps to jump out of deeply nested structures quickly. Syntax example.
goto Label; Console.WriteLine("This will be skipped."); Label: Console.WriteLine("Jumped here.");
Question: WAP How can you check if a person can get a driving license based on their age?
using System; class Program { static void Main() { int age = 20; if (age >= 18) { Console.WriteLine("You can apply for a driving license."); } } }
Question: WAP. How can you check if a bank account balance is enough for a withdrawal?
using System; class Program { static void Main() { int balance = 500; int withdrawAmount = 600; if (balance >= withdrawAmount) { Console.WriteLine("Withdrawal successful."); } else { Console.WriteLine("Insufficient balance."); } } }
WAP. How can you categorize a person’s age group?
using System; class Program { static void Main() { int age = 25; if (age < 13) { Console.WriteLine("Child"); } else if (age >= 13 && age < 20) { Console.WriteLine("Teenager"); } else { Console.WriteLine("Adult"); } } }
Question: How can you create a simple menu that lets the user choose an option (like 1 for “Add”, 2 for “Subtract”, 3 for “Multiply”, 4 for “Exit”) and display a message based on the choice?
using System; class Program { static void Main() { Console.WriteLine("Simple Calculator Menu:"); Console.WriteLine("1. Add"); Console.WriteLine("2. Subtract"); Console.WriteLine("3. Multiply"); Console.WriteLine("4. Exit"); Console.Write("Enter your choice (1-4): "); int choice = int.Parse(Console.ReadLine()); switch (choice) { case 1: Console.WriteLine("You chose Addition."); break; case 2: Console.WriteLine("You chose Subtraction."); break; case 3: Console.WriteLine("You chose Multiplication."); break; case 4: Console.WriteLine("Exiting the program. Goodbye!"); break; default: Console.WriteLine("Invalid choice. Please select 1-4."); break; } } }
How can you search for a specific number and stop when found? Use a break statement
using System; class Program { static void Main() { int[] numbers = { 1, 3, 5, 7, 9 }; foreach (int num in numbers) { if (num == 5) { Console.WriteLine("Number found!"); break; } Console.WriteLine("Checking: " + num); } } }
How can you skip printing even numbers in a list? Use the continue Statement.
using System; class Program { static void Main() { for (int i = 1; i <= 5; i++) { if (i % 2 == 0) { continue; } Console.WriteLine(i); } } }
How can you find and return the square of a number? Use the return statement.
using System; class Program { static void Main() { int result = Square(4); Console.WriteLine("Square is: " + result); } static int Square(int num) { return num * num; } }
Looping and Iteration in C#
Looping (or Iteration) in C# means executing a block of code repeatedly until a certain condition is met. It helps us avoid writing the same code again and again. Instead, we write the code once and control how many times it should run.
Types of Loops in C#
C# provides four main types of loops:
1. for loop
The for loop is used when you know exactly how many times you want to repeat a block of code. It has three parts inside the parentheses: initialization, condition, and increment/decrement. The loop starts by initializing a variable, checks the condition before each iteration, and updates the variable each time.

syntax:
for (initialization; condition; increment) { // Code to repeat } Example:Print numbers from 1 to 5. for (int i = 1; i <= 5; i++) { Console.WriteLine(i); }
2. While Loop
The while loop repeats a block of code as long as a given condition is true. If the condition is false at the start, the loop won’t run at all. It’s useful when you don’t know in advance how many times you’ll need to repeat the code.
Syntax:
while (condition) { // Code to repeat } Example: Print numbers from 1 to 5. int i = 1; while (i <= 5) { Console.WriteLine(i); i++; }
3. Do While Loop
The do-while Loop is similar to while, but it always runs at least once because it checks the condition after executing the code block. This is helpful for menus or input validation, where the code must run first.
Syntax:
do { // Code to repeat } while (condition); Example: Print numbers from 1 to 5. int i = 1; do { Console.WriteLine(i); i++; } while (i <= 5);
4.Foreach Loop
The Foreach Loop is used to iterate through all elements in a collection (like an array or list) easily. You don’t need to worry about indexes — it automatically goes through each item in order.
Syntax:
foreach (datatype item in collection) { // Code to repeat for each item } Example: Print each name in an array. string[] names = { "Alice", "Bob", "Charlie" }; foreach (string name in names) { Console.WriteLine(name); }
Comparison of Loops in C#
| Feature | for Loop | while Loop | do-while Loop | foreach Loop |
|---|---|---|---|---|
| Purpose | Best when the number of iterations is known. | Best when the number of iterations is unknown and depends on a condition. | Best when you need the code to run at least once, then repeat if the condition is true. | Best for looping through collections like arrays, lists, etc. |
| Condition Check | Checks condition before each iteration. | Checks condition before each iteration. | Checks condition after each iteration. | Automatically loops through each item — no condition. |
| Syntax | for (init; condition; increment) { } | while (condition) { } | do { } while (condition); | foreach (type item in collection) { } |
| When to Use | Fixed tasks (like printing 1 to 10). | Unknown tasks (like reading input until valid). | Input validation or menus that must run at least once. | Reading all items in a list/array. |
| Control Variable | Defined in the loop header (e.g., int i). | Defined outside the loop. | Defined outside the loop. | Loop variable automatically declared. |
| Example Use | Counting, fixed repetitions. | Waiting for user input, conditions. | Asking for input at least once. | Printing all names in an array. |
Write a program that prints the squares of numbers from 1 to 5 using a for loop.
using System; class Program { static void Main() { Console.WriteLine("Squares of numbers from 1 to 5:"); for (int i = 1; i <= 5; i++) { Console.WriteLine($"{i} squared is {i * i}"); } } }
Write a program that keeps asking the user to enter a number greater than 10. If the user enters a smaller number, ask again using a while loop.
using System; class Program { static void Main() { int number; Console.WriteLine("Enter a number greater than 10:"); number = Convert.ToInt32(Console.ReadLine()); while (number <= 10) { Console.WriteLine("Number must be greater than 10. Try again:"); number = Convert.ToInt32(Console.ReadLine()); } Console.WriteLine($"You entered: {number}"); } }
Write a program that displays a simple menu with two options — “1. Say Hello” and “2. Exit”. Keep showing the menu until the user chooses Exit.
using System; class Program { static void Main() { int choice; do { Console.WriteLine("Menu:"); Console.WriteLine("1. Say Hello"); Console.WriteLine("2. Exit"); Console.Write("Enter your choice: "); choice = Convert.ToInt32(Console.ReadLine()); if (choice == 1) { Console.WriteLine("Hello!"); } } while (choice != 2); Console.WriteLine("Program ended."); } }
Write a program that stores 5 fruits in an array and uses a foreach loop to print each fruit’s name.
using System;
class Program
{
static void Main()
{
string[] fruits = { "Apple", "Banana", "Mango", "Orange", "Grapes" };
Console.WriteLine("Fruits in the basket:");
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
}
}
