Computer Science

C Program — Control Flow

2023-08-13 2 min read

Control-flow statements decide which code runs and how often.

Control flow

  • Selection Statements
  • Iteration Statements
  • Jump Statements

Selection Statements

Evaluate a condition as TRUE or FALSE to choose which block of code to run.

  • If … else
  • Switch case

Iteration Statements

Repeatedly run one or more statements. The condition is checked before each pass, and the loop stops only when it becomes FALSE (or on break).

  • While
  • Do while
  • For loop

Jump Statements

  • Break — stop and exit the loop.
  • Continue — skip the rest of the current iteration and jump back to the start of the loop for the next one.

Selection Statements

If … else

If _ Flow chart

If _ Syntax

If … else _ Flow chart

If … else _ Syntax

Shorthand if … else _ Syntax

If … else if … else _ Flow chart

If … else if … else _ Syntax

Nested if _ Flow chart

Nested if _ Syntax

Switch

Switch _ Flow chart

Switch _ Syntax

Thinking

What does the program below output?

Termination conditions:

  • Keeps running until there are no more cases.
  • When it hits a break, it jumps out immediately.

Iteration Statements

While

While _ Flow chart

While _ Syntax

Do while

Do while _ Flow chart

Do while _ Syntax

For loop

For loop _ Flow chart

For loop _ Syntax

  • statement 1 : initialization
  • statement 2 : conditional
  • statement 3 : update

Nested loop

“Nested” means things wrapped layer by layer, like a bird’s nest. A nested loop is a while/for loop placed inside another while/for loop.

Midway Challenge

← Posts
meow~