Back to C Programming
2026-07-125 min read

C Flow Control

Learn C Flow Control step by step with clear examples and exercises.

Why This Matters

C is a powerful programming language that offers various control structures to manipulate the flow of your program. In this lesson, we will delve into C's flow control mechanisms, focusing on decision-making (if...else) statements and loop constructs (for, while, do...while). By mastering these concepts, you can write more efficient and effective C programs.

Why This Matters

Understanding C flow control is crucial for writing well-structured programs that solve complex problems. The ability to make decisions based on conditions and iterate through loops enables you to create dynamic solutions that adapt to different scenarios. In interviews, demonstrating proficiency in flow control can impress potential employers and help you stand out from other candidates.

Prerequisites

Before diving into C flow control, it's essential to have a solid understanding of the following topics:

  • Basic C syntax (variables, operators, expressions)
  • Data types (int, char, float, etc.)
  • Functions and function prototypes
  • Input/Output operations (scanf, printf)

Core Concept

If...Else Statements

C's if...else statements allow you to make decisions in your code based on a condition. The syntax is as follows:

if (condition) {
// Code executed if the condition is true
} else {
// Code executed if the condition is false
}

For example, consider the following code snippet that checks whether a number is even or odd:

#include <stdio.h>

int main() {
int num;

printf("Enter an integer: ");
scanf("%d", &num);

if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}

return 0;
}

Loop Constructs

C offers three types of loops: for, while, and do...while. These loops allow you to repeatedly execute a block of code until a certain condition is met.

For Loop

The for loop is a compact way to iterate through a range of values or perform an action a specific number of times. Its syntax is as follows:

for (initialization; condition; increment/decrement) {
// Code executed during each iteration
}

For example, consider the following code snippet that prints the numbers from 1 to 10 using a for loop:

#include <stdio.h>

int main() {
int i;

for (i = 1; i <= 10; ++i) {
printf("%d ", i);
}

return 0;
}

While Loop

The while loop continually executes a block of code as long as the specified condition remains true. Its syntax is as follows:

while (condition) {
// Code executed during each iteration
}

For example, consider the following code snippet that repeatedly asks for user input until they enter "quit":

#include <stdio.h>

int main() {
char input[10];

while (1) {
printf("Enter a command: ");
scanf("%s", &input);

if (strcmp(input, "quit") == 0) {
break;
} else {
// Process the user's input here
printf("Processing...\n");
}
}

return 0;
}

Do...While Loop

The do...while loop executes a block of code at least once before checking the condition. Its syntax is as follows:

do {
// Code executed during each iteration
} while (condition);

For example, consider the following code snippet that repeatedly asks for user input until they enter a valid number between 1 and 10:

#include <stdio.h>

int main() {
int num;

do {
printf("Enter a number between 1 and 10: ");
scanf("%d", &num);

if (num >= 1 && num <= 10) {
break;
} else {
printf("Invalid input. Please try again.\n");
}
} while (1);

return 0;
}

Worked Example

Let's write a program that calculates the factorial of a number using a for loop.

#include <stdio.h>

int main() {
int num, fact = 1;

printf("Enter a non-negative integer: ");
scanf("%d", &num);

if (num < 0) {
printf("Invalid input. Please enter a non-negative integer.\n");
return 1;
}

for (int i = 1; i <= num; ++i) {
fact *= i;
}

printf("The factorial of %d is %d.\n", num, fact);

return 0;
}

Common Mistakes

1. Forgetting to initialize the loop variable

In a for loop, it's essential to initialize the loop variable before the loop starts:

// Incorrect
for (int i = num; i > 0; --i) {
// ...
}

// Correct
for (int i = num; i > 0; ) {
int temp = i;
--i;
// ...
if (temp == i) break;
}

2. Using == instead of = in the assignment statement

In C, == is used for comparison, while = is used for assignment:

// Incorrect
if (num = 5) {
// ...
}

// Correct
if (num == 5) {
// ...
}

3. Not handling the edge cases in your logic

Ensure that your code handles all possible edge cases, such as input validation and boundary conditions:

#include <stdio.h>

int main() {
int num;

printf("Enter a number between 1 and 10: ");
scanf("%d", &num);

if (num >= 1 && num <= 10) {
// ...
} else {
printf("Invalid input. Please enter a number between 1 and 10.\n");
return 1;
}

// ...
}

Practice Questions

  1. Write a program that checks whether a given year is a leap year (a year divisible by 4, except for century years, which must be divisible by 400).
  2. Write a program that calculates the sum of all even numbers between 1 and 100.
  3. Write a program that finds the largest prime number among the numbers from 2 to 50.
  4. Write a program that prints the Fibonacci sequence up to the nth term, where n is input by the user.

FAQ

Q: What's the difference between == and = in C?

A: In C, == is used for comparison (checking if two values are equal), while = is used for assignment (assigning a value to a variable).

Q: Why do I need to initialize the loop variable in a for loop?

A: Initializing the loop variable ensures that it has a defined value before the loop starts, which can help avoid unexpected behavior and make your code more predictable.

Q: How can I find all the prime numbers between 2 and 100 using C flow control structures?

A: You can use a for loop to iterate through the numbers from 2 to 100, checking each number to see if it's prime by trying to divide it by all smaller numbers. If no smaller number divides the current number evenly, then it's a prime number. Here's an example of how you can implement this:

#include <stdio.h>

void printPrimes(int start, int end) {
for (int i = start; i <= end; ++i) {
if (isPrime(i)) {
printf("%d ", i);
}
}
}

int isPrime(int num) {
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
return 0;
}
}
return 1;
}

int main() {
printPrimes(2, 100);
return 0;
}