Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program. Branching allows the program to skip or execute certain blocks of code based on the result of these tests.
The if statement is the most powerful tool for controlling the flow of execution.
if-else statement inside another if or else block, used for complex multi-level decisions.if statements where multiple conditions are checked sequentially.
The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of an expression.
Utility: It is often used as a cleaner alternative to a long else if ladder when comparing a single variable against multiple constant values.
Looping involves repeating a set of instructions until a specific condition is met. This is essential for tasks like processing array elements or generating series.
There are three primary looping constructs in C:
if(condition) or for(...) header, as it terminates the statement immediately without executing the block.break keyword in switch cases; otherwise, execution will "fall through" to subsequent cases.for loop when you know the number of iterations in advance and a while loop when the termination depends on a condition.Q1: What is the main difference between while and do-while?
The while loop may never execute if the condition is false initially, whereas do-while guarantees at least one execution.
Q2: When should I use an ELSE IF ladder over a Switch?
Use an else if ladder for range-based conditions (e.g., marks > 90) and a switch for specific discrete values (e.g., choice == 1).