0
0
C Sharp (C#)programming~5 mins

Nested conditional execution in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is nested conditional execution in C#?
It means putting one if or else if statement inside another if or else if statement to check multiple conditions step-by-step.
Click to reveal answer
beginner
How do you write a nested if statement in C#?
You write an if statement inside the block of another if or else if statement using curly braces { } to group the inner condition.
Click to reveal answer
beginner
Why use nested conditional execution?
To check more detailed conditions only if the first condition is true, like checking if a person is an adult, then checking if they have a driver’s license.
Click to reveal answer
beginner
What happens if the outer if condition is false in nested conditionals?
The inner if statements are skipped completely because the outer condition controls whether the inner code runs.
Click to reveal answer
beginner
Example: What will this code print? if (age >= 18) { if (hasLicense) { Console.WriteLine("Can drive"); } else { Console.WriteLine("No license"); } } else { Console.WriteLine("Too young"); }
If age is 20 and hasLicense is true, it prints "Can drive". If age is 20 and hasLicense is false, it prints "No license". If age is 16, it prints "Too young".
Click to reveal answer
What does nested conditional execution allow you to do?
ASkip all conditions
BWrite only one condition
CCheck multiple conditions inside each other
DRun code without any condition
In C#, which symbol is used to group the inner if statement?
A[]
B{}
C()
D<>
If the outer if condition is false, what happens to the inner if?
AIt is skipped
BIt runs anyway
CIt causes an error
DIt runs twice
Which is a correct nested if structure in C#?
Aif (x > 0) if (y > 0) { Console.WriteLine("Both positive"); }
Bif (x > 0) else if (y > 0) { Console.WriteLine("Both positive"); }
Cif (x > 0) { else if (y > 0) { Console.WriteLine("Both positive"); } }
Delse if (x > 0) { if (y > 0) Console.WriteLine("Both positive"); }
What will this code print if age = 17? if (age >= 18) { Console.WriteLine("Adult"); } else { Console.WriteLine("Minor"); }
AAdult
BNothing
CError
DMinor
Explain nested conditional execution and give a simple example in C#.
Think about checking one condition only if another is true.
You got /3 concepts.
    Describe what happens when the outer condition in nested if statements is false.
    Consider how the program decides which code to run.
    You got /3 concepts.