Monday, July 28, 2025

Unit 2 : Decision Control and Looping Statements

Prepared By : Prof. Uday Shah (HOD - IT)


Unit 2 : Decision Control and Looping Statements 


If Statements

Simple If

  • A simple if statement evaluates a condition and executes a block of code only if the condition is true.
  • It is the most basic form of decision-making in C.
  • Syntax:

if (condition) { 

    // statements 

} 

  • If the condition is false, the block is skipped.
  • Useful when there's only one condition to check without an alternate path.
  • Helps avoid unnecessary blocks when no action is needed for false conditions.

Multiple If

  • Involves using several if statements to check different unrelated conditions.
  • Each condition is evaluated independently.
  • All blocks whose conditions are true will be executed.
  • Syntax:

if (condition1) { } 

if (condition2) { } 

  • Useful when multiple independent checks need to be made.
  • Does not offer mutual exclusivity like else-if.

If..Else..Else

  • Checks a condition and executes one block if true, and another if false.
  • It provides an alternative path of execution.
  • Syntax:

if (condition) { 

    // true block 

} else { 

    // false block 

} 

  • Ensures only one of the blocks is executed.
  • Good for binary decisions (yes/no, true/false).

Nested If

  • An if statement inside another if or else block is called a nested if.
  • Allows checking secondary conditions only if the first is true.
  • Syntax:

if (condition1) { 

    if (condition2) { } 

} 

  • Useful for complex decision-making processes.
  • Must maintain proper indentation and logic flow for readability.

 

Switch Statement

  • The switch statement selects one of many code blocks to be executed.
  • It matches the value of a variable against case labels.
  • Syntax:

switch (variable) { 

    case value1: break; 

    case value2: break; 

    default: break; 

} 

  • Useful as an alternative to lengthy if-else-if ladders.
  • Requires break statements to prevent fall-through.
  • Works only with integral types (e.g., int, char).

 

Conditional Ternary Operator

  • A compact form of if-else statement.
  • Syntax: condition ? expression_if_true : expression_if_false;
  • Returns one of two values depending on the truth of the condition.
  • Example:

int max = (a > b) ? a : b; 

  • Often used for assignment or output decisions.
  • Makes code concise but can reduce readability for complex conditions.

 

Looping Structures

While Loop

  • Executes a block of code repeatedly as long as the condition is true.
  • Entry-controlled loop: condition is checked before the loop body.
  • Syntax:

while (condition) { 

    // statements 

} 

  • Used when the number of iterations is not known in advance.
  • May never execute if the condition is false at the start.
  • Ensures loop runs only when condition is satisfied.

For Loop

  • Used when the number of iterations is known.
  • Syntax includes initialization, condition, and update in a single line.
  • Syntax:

for (init; condition; increment) { 

    // statements 

} 

  • Compact and readable for counting loops.
  • Supports decrementing, skipping, and multiple variables.
  • Commonly used in array and numeric operations.

Do..While Loop

  • Executes the loop body at least once before checking the condition.
  • Exit-controlled loop.
  • Syntax:

do { 

    // statements 

} while (condition); 

  • Ensures the loop runs at least once regardless of condition.
  • Useful in menu-driven or input-validation programs.
  • Can be less intuitive than while or for.

Nesting of Loops

  • One loop placed inside another loop is called nested loop.
  • Inner loop completes all iterations for each iteration of the outer loop.
  • Syntax:

for (...) { 

    for (...) { 

        // inner statements 

    } 

} 

  • Commonly used in matrix, pattern, and table generation.
  • Increases complexity; proper indentation is critical.
  • Excessive nesting may impact performance and readability.

 

Jumping Statements

Break

  • Used to exit a loop or switch statement immediately.
  • Control moves to the statement after the loop or switch.
  • Syntax:

if (condition) break; 

  • Often used in switch and loops to prevent unnecessary execution.
  • Can improve efficiency in certain conditions.
  • Should be used carefully to avoid abrupt termination.

Continue

  • Skips the remaining part of the loop for the current iteration.
  • Moves control to the next iteration of the loop.
  • Syntax:

if (condition) continue; 

  • Used to skip over specific cases within loops.
  • Helpful in filtering or skipping logic without exiting the loop.
  • Avoids using complex if-else logic.

Goto

  • Transfers control to a labeled statement within the same function.
  • Syntax:

goto label; 

...

label: statement; 

  • Can create spaghetti code; not recommended in structured programming.
  • Used in rare cases like breaking from deeply nested loops.
  • Reduces readability and maintainability if overused.
  • Better alternatives include break, continue, or functions.