Fundamentals of Programming: Control Statements

CONTROL STATEMENTS IN PROGRAMMING

  • Control statements guide the flow of a software program.

Types of Control Structures

  • Sequence Structures

  • Selection Structures

    • Decision-making flow based on evaluated conditions

    • Examples:

    • if

    • if-else

    • nested if

    • else-if

    • switch

  • Loop Structures

    • For repeating actions

    • Examples: while, for, do-while

Selection Structures Overview

  • Conditions evaluated:

    • Non-zero/non-null: true

    • Zero/null: false

  • Operators used:

    • Relational: <, >, <=, >=, ==, !=

    • Logical: &&, ||, !

If-Else Statements

  • Syntax:

  if (condition) {  
      // statements if true  
  } else {  
      // statements if false  
  }  

Nested If Statements

  • Structure allows if statements inside other if statements.

Switch Statements

  • Testing equality against multiple values using cases.

Conditional Operator

  • Syntax: condition ? expression1 : expression2

    • Evaluates condition and assigns value accordingly.

Loop Structures

  • While Loop: Condition checked before each iteration.

  • For Loop: For known number of iterations.

  • Do-While Loop: Condition checked after the block execution.

Loop Control Statements

  • Break: Exit the loop/switch.

  • Continue: Skip to the next iteration.

  • Goto: Unconditional jump (not recommended).

Summary

  • Decision Making: if, if-else, nested if, else-if, switch

  • Repetition: while, for, do-while