C Control Structures (CT 101): IOE Notes on if, switch, for, while
An overview of C Control Structures like if-else and for loops
Control Structures
5 hours | 5 marks

COMPUTER PROGRAMMING (CT 101) – Chapter 5

By default, program statements execute sequentially. However, to write meaningful programs, we need to alter this flow. C Control Structures are statements that allow a program to make decisions and repeat actions. They are the core of programming logic, enabling us to choose which code to execute (branching) and how many times to execute it (looping).

Chapter Information

Chapter 5: Control Structures (5 hours) – 5 marks

Course: Computer Programming (CT 101), I Year I Part

Description: This guide provides a complete overview of C Control Structures, covering branching (if-else, switch) and looping (for, while, do-while) with examples and flowcharts, as per the IOE syllabus.

Credit: Sujan Karki

Detailed Chapter Notes

5.1 Sequential

Sequential control is the default flow in C. Statements are executed one after another in the order they appear in the source code, from top to bottom. There is no branching or repetition.

5.2 Branching (Decision Making)

Branching statements allow a program to choose a specific path of execution based on a condition.

5.2.1 simple if

The if statement executes a block of code only if a specified condition is true.

Syntax: if (condition) { /* statements */ }

5.2.2 if-else

The if-else statement executes one block of code if the condition is true and another block if it is false.

Syntax: if (condition) { /* true block */ } else { /* false block */ }

5.2.3 nested if-else

Placing an if-else statement inside another if or else block. This allows for multi-level decision making.

5.2.4 else-if ladder

Used to choose one option from multiple alternatives. It tests conditions sequentially until one is found to be true.

Syntax: if (cond1) { ... } else if (cond2) { ... } else { ... }

5.2.5 switch

A multi-way branching statement that compares the value of an expression against a list of constant integer or character values (cases). It’s often a cleaner alternative to a long else-if ladder.

Syntax: switch (expression) { case val1: ... break; case val2: ... break; default: ... }

5.2.6 goto

The goto statement provides an unconditional jump from the goto to a labeled statement in the same function. Its use is highly discouraged in modern programming as it makes code hard to read, debug, and maintain.

5.3 Looping (Iteration)

Looping statements execute a block of code repeatedly as long as a specified condition is true.

5.3.1 while, do…while & for

  • while loop: An entry-controlled loop. The condition is checked *before* each iteration. The loop body may not execute at all if the condition is initially false.
  • do…while loop: An exit-controlled loop. The condition is checked *after* each iteration. The loop body is guaranteed to execute at least once.
  • for loop: A concise loop structure that combines initialization, condition checking, and update (increment/decrement) into one line. It’s ideal when the number of iterations is known.

5.3.2 Loop Categories

  • Entry-Controlled (Pre-test) Loop: The test condition is checked at the beginning of the loop. If the condition is false, the loop body is never executed. (e.g., for, while).
  • Exit-Controlled (Post-test) Loop: The test condition is checked at the end of the loop. This guarantees the loop body will execute at least once. (e.g., do...while).
  • Counter-Controlled Loop: A loop that repeats a specific number of times, controlled by a counter variable. (Typically implemented with a for loop).
  • Sentinel-Controlled Loop: A loop that continues until a special value (a “sentinel”) is entered by the user to signal termination. (Often implemented with a while loop).

5.3.3 Nesting of loops

Placing one loop inside the body of another loop. The inner loop executes completely for each single iteration of the outer loop. This is common for working with 2D arrays or creating patterns.

5.3.4 Loop interruption (break, continue)

  • break statement: Immediately terminates the innermost loop or switch statement in which it appears. Execution resumes at the statement following the terminated loop.
  • continue statement: Skips the remainder of the current iteration of a loop and proceeds to the next iteration.

PDF Notes

×

Disclaimer

The educational materials provided on this website are intended as supplementary resources to support your learning journey. These study materials are sample documents designed to help students understand complex concepts in C Programming.

We have made every effort to ensure the accuracy of the content. However, we recommend students to refer to standard textbooks and consult with professors for authoritative explanations. These materials should be used as references only.

Scroll to Top