Tamil Nadu HSE Second Year Computer Applications study materials
Tamil Nadu HSE Second Year Computer Applications study resources

Tamil Nadu HSE Second Year Computer Applications: Control Structures

Tamil Nadu HSE Second Year Computer Applications · Work through your exam, one topic at a time.

All guide sections

Control Structures

What this domain tests

This domain tests selection and iteration: choosing between if, if-else, else-if, and switch, and writing correct for, while, and do-while loops. Questions often ask for output tracing or loop counts.

Core rules

  • Three control patterns: sequence (top to bottom), selection (branch), and iteration (repeat).
  • if executes a block when a condition is true. if-else chooses between two blocks. An else-if ladder tests conditions in order and stops at the first true condition.
  • switch compares one expression with constant case labels. break exits the switch; without it, execution falls through to later cases. default handles unmatched values.
  • for(initialisation; condition; update) is best when the number of repetitions is known. while is a pre-test loop. do-while is a post-test loop and runs at least once.
  • break exits the nearest loop or switch; continue skips the rest of the current iteration and proceeds to the next.
  • Nested loops: the inner loop completes fully for each outer-loop iteration.

Worked example

Find the sum 1 + 2 + ... + 10. Initialise sum = 0. For i = 1 to 10: sum = sum + i. The trace is 0+1=1, 1+2=3, 3+3=6, 6+4=10, 10+5=15, 15+6=21, 21+7=28, 28+8=36, 36+9=45, 45+10=55. The loop runs 10 times because the condition includes 10. If the condition were i < 10, the result would be 45 and the loop would run 9 times.

Traps

  • Placing a semicolon after for(...) or if(...), creating an empty body.
  • Off-by-one errors: <= versus <.
  • Forgetting break in switch, causing fall-through.
  • Expecting do-while to skip execution when the condition is false.
  • A dangling else binds to the nearest unmatched if.