Why do programmers use conditional flow control statements like if and switch in their code?
Think about how a program can choose different paths depending on information it has.
Conditional flow control lets a program choose what to do next based on conditions. This helps the program make decisions, like a fork in a road.
What will be the output of this C# code?
int temperature = 30; if (temperature > 25) { Console.WriteLine("It's hot outside."); } else { Console.WriteLine("It's cool outside."); }
Check the condition temperature > 25 and see if it is true or false.
The variable temperature is 30, which is greater than 25, so the if block runs and prints "It's hot outside.".
What will this C# program print?
int day = 3; switch (day) { case 1: Console.WriteLine("Monday"); break; case 2: Console.WriteLine("Tuesday"); break; case 3: Console.WriteLine("Wednesday"); break; default: Console.WriteLine("Another day"); break; }
Look at the value of day and match it with the correct case.
The variable day is 3, so the case 3 block runs and prints "Wednesday".
What error will this C# code cause?
int number = 10; if (number > 5) { Console.WriteLine("Number is greater than 5"); }
Check the end of the first line for syntax mistakes.
The first line is missing a semicolon at the end, causing a syntax error.
Which statement best explains why conditional flow control is essential in programming?
Think about how programs can behave differently in different situations.
Conditional flow control lets programs choose different actions depending on conditions, making them flexible and responsive.