0% found this document useful (0 votes)
6 views6 pages

Module 3 Control Structures

Module 3 focuses on control structures in programming, specifically decision-making with if and switch statements, and repetition through loops such as for, while, and do-while. It includes practical exercises like FizzBuzz, a login system, and a calculator to reinforce these concepts. Key control flow keywords like break and continue are also discussed to enhance understanding of loop behavior.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Module 3 Control Structures

Module 3 focuses on control structures in programming, specifically decision-making with if and switch statements, and repetition through loops such as for, while, and do-while. It includes practical exercises like FizzBuzz, a login system, and a calculator to reinforce these concepts. Key control flow keywords like break and continue are also discussed to enhance understanding of loop behavior.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 3: Control Structures (Logic &

Loops)
Student: Techritoma Interns

Time Allocation:

Goal: Master decision-making (if, switch) and repetition (loops) to build complex
algorithms.

Part 1: Decision Making (The "Brain")


1. The if Statement
The bread and butter of logic.

int score = 85;

if (score >= 90)


{
[Link]("Grade: A");
}
else if (score >= 80)
{
[Link]("Grade: B");
}
else
{
[Link]("Grade: C or lower");
}
CS Concept: Short-Circuit Evaluation

● && (AND): Comparisons stop if the first condition is false.


● || (OR): Comparisons stop if the first condition is true.
● Why it matters: If you have if (CheckDatabase() && CheckFile()), and
CheckDatabase() fails, C# won't waste time (or crash) trying to CheckFile().

2. The switch Statement


Use this when comparing a single variable against many specific values (constants).

string day = "Monday";

switch (day)
{
case "Monday":
[Link]("Start of the week.");
break; // Vital! Prevents falling into the next case.
case "Friday":
[Link]("Weekend is near.");
break;
default:
[Link]("Just another day.");
break;
}

Modern C# 8.0+ Switch Expressions (The "Pro" Way):

string message = day switch


{
"Monday" => "Start of the week.",
"Friday" => "Weekend is near.",
_ => "Just another day." // '_' is the default case
};

Part 2: Loops (Repetition)


1. The for Loop
Best when you know exactly how many times you want to loop.

// Logic: Init; Condition; Increment


for (int i = 0; i < 5; i++)
{
[Link]($"Iteration: {i}");
}

● CS Warning (Big O): Be careful with nested loops (a loop inside a loop). That is

complexity, which kills performance on large datasets.

2. The while Loop


Best when you want to loop until a condition changes (unknown duration).

int health = 100;


while (health > 0)
{
[Link]("Fighting...");
health -= 10; // Vital! Without this, you get an Infinite Loop.
}
3. The do-while Loop
Guarantees the code runs at least once, even if the condition is false initially. Great for
"Press any key to quit" menus.

string input;
do
{
[Link]("Type 'quit' to exit:");
input = [Link]();
} while (input != "quit");

Part 3: Control Flow Keywords


● break: Immediately exits the loop or switch.
● continue: Skips the rest of the current iteration and jumps to the next one.

Example: Printing only odd numbers

for (int i = 0; i < 10; i++)


{
if (i % 2 == 0) // If even
{
continue; // Skip printing, go to next 'i'
}
[Link](i);
}
Part 4: Practical Exercises
Exercise 1: The "FizzBuzz" Challenge
This is the #1 interview question for junior devs.

Write a loop that counts from 1 to 100.

1. If the number is divisible by 3, print "Fizz".

2. If divisible by 5, print "Buzz".

3. If divisible by both 3 and 5, print "FizzBuzz".

4. Otherwise, print the number.

Exercise 2: The Login System


1. Create a stored password string (e.g., "secret123").

2. Use a while or do-while loop to ask the user for the password.

3. If they get it wrong, tell them "Access Denied" and ask again.

4. If they get it right, print "Access Granted" and end the loop.

5. Bonus: Limit them to 3 attempts. If they fail 3 times, print "Account Locked" and

break.

Exercise 3: The Calculator (Switch)


1. Ask the user for Number 1.

2. Ask for Number 2.

3. Ask for an operation (+, -, *, /).

4. Use a switch statement to perform the math and print the result.

5. Handle the edge case: Division by Zero!

Checkpoint: Debugging Logic


What is wrong with this code? (Find the logical error)

int x = 10;
if (x > 5)
{
[Link]("X is big");
}
else if (x > 8) // Will this ever run?
{
[Link]("X is very big");
}

You might also like