Control Flow Statements to Know for Intro to C Programming

Control flow statements are key in C programming, allowing your code to make decisions and repeat actions. They help manage how your program runs based on conditions, making it flexible and efficient for various tasks.

  1. if statement

    • Evaluates a condition and executes a block of code if the condition is true.
    • Syntax:
      if (condition) { /* code to execute */ }
    • Can be used for simple decision-making in programs.
    • Supports logical expressions for complex conditions.
    • Essential for controlling the flow of execution based on dynamic conditions.
  2. if-else statement

    • Extends the if statement by providing an alternative block of code if the condition is false.
    • Syntax:
      if (condition) { /* code if true */ } else { /* code if false */ }
    • Useful for binary decision-making scenarios.
    • Can be nested to handle multiple conditions.
    • Enhances program flexibility by allowing different outcomes based on conditions.
  3. switch statement

    • Allows multi-way branching based on the value of a variable or expression.
    • Syntax:
      switch (variable) {
          case value1: /* code */ break;
          case value2: /* code */ break;
          default: /* code */
      }
      
    • More efficient than multiple if-else statements for handling numerous conditions.
    • Each case can execute a block of code until a break statement is encountered.
    • The default case handles any values not explicitly matched.
  4. for loop

    • Used for iterating a block of code a specific number of times.
    • Syntax:
      for (initialization; condition; increment) { /* code to execute */ }
    • Ideal for scenarios where the number of iterations is known beforehand.
    • Combines initialization, condition checking, and incrementing in one line.
    • Supports complex iteration patterns through custom increment expressions.
  5. while loop

    • Repeats a block of code as long as a specified condition is true.
    • Syntax:
      while (condition) { /* code to execute */ }
    • Useful for situations where the number of iterations is not predetermined.
    • The condition is evaluated before each iteration, potentially skipping the loop entirely.
    • Can lead to infinite loops if the condition never becomes false.
  6. do-while loop

    • Similar to the while loop, but guarantees at least one execution of the code block.
    • Syntax:
      do { /* code to execute */ } while (condition);
    • The condition is evaluated after the code block executes.
    • Useful for scenarios where the code must run at least once, regardless of the condition.
    • Helps in user input scenarios where initial processing is required.
  7. break statement

    • Immediately exits the nearest enclosing loop or switch statement.
    • Syntax:
      break;
    • Can be used to terminate loops based on a specific condition.
    • Helps in controlling the flow of execution and avoiding unnecessary iterations.
    • Often used in conjunction with if statements to create exit conditions.
  8. continue statement

    • Skips the current iteration of a loop and proceeds to the next iteration.
    • Syntax:
      continue;
    • Useful for bypassing specific conditions within loops without terminating the entire loop.
    • Can be applied in for, while, and do-while loops.
    • Helps in filtering out unwanted iterations based on conditions.
  9. goto statement

    • Provides an unconditional jump to a specified label in the code.
    • Syntax:
      goto label;
      label: /* code to execute */
      
    • Generally discouraged due to potential for creating complex and hard-to-follow code.
    • Can lead to "spaghetti code" if overused, making debugging difficult.
    • May be useful in specific scenarios for breaking out of deeply nested structures.
  10. return statement

    • Exits a function and optionally returns a value to the calling function.
    • Syntax:
      return value;
    • Essential for passing results back from functions to the caller.
    • Can be used to terminate a function early based on conditions.
    • Helps in managing program flow and function outputs effectively.


© 2025 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2025 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.