If Else and Switch in C

Subject: Data Structures and Algorithms · Language: C · Level: beginner · 4 min read

Write decision-making programs using if, else if, else, nested conditions, and switch case.

Control flow means controlling the path of execution in a program. Normally, C runs statements from top to bottom. But real programs need decisions: pass or fail, eligible or not eligible, positive or negative, grade A or grade B.

In C, decision-making is mainly done using `if`, `else if`, `else`, and `switch`.

Conditions in C

A condition is an expression that is treated as true or false. In C, `0` means false, and any non-zero value is treated as true.

```c

if (5) {

printf("This runs");

}

```

Building Good Conditions

A condition should be clear and direct. For example, `marks >= 40` is easier to read than writing a complicated expression with the same meaning.

When multiple rules are involved, break them into smaller conditions or use meaningful variable names.

What Is if?

`if` is used when a block of code should run only when a condition is true.

```c

if (marks >= 40) {

printf("Pass");

}

```

If the condition is true, the code inside `{ }` runs. If the condition is false, that block is skipped.

What Is if else?

`if else` is used when there are two possible paths.

```c

if (marks >= 40) {

printf("Pass");

} else {

printf("Fail");

}

```

Here, one block will definitely run. If the condition is true, `if` runs. Otherwise, `else` runs.

What Is else if?

`else if` is used when there are multiple conditions to check one by one.

For example, a grading system may check marks for Grade A, Grade B, Grade C, and Fail.

```c

if (marks >= 90) {

printf("Grade A");

} else if (marks >= 75) {

printf("Grade B");

} else if (marks >= 60) {

printf("Grade C");

} else {

printf("Needs improvement");

}

```

In this pattern, C checks from top to bottom. Once one condition is true, its block runs and the remaining conditions are skipped.

```c

#include <stdio.h>

int main(void) {

int marks;

printf("Enter marks: ");

scanf("%d", &marks);

if (marks >= 90) {

printf("Grade A\n");

} else if (marks >= 75) {

printf("Grade B\n");

} else if (marks >= 60) {

printf("Grade C\n");

} else if (marks >= 40) {

printf("Pass\n");

} else {

printf("Fail\n");

}

return 0;

}

```

Order of Conditions Matters

Range-Based Decisions

Use `if else` when checking ranges. Examples include marks, age groups, salary slabs, bill amounts, and temperature levels.

For marks, check higher ranges first. If you check `marks >= 40` before `marks >= 90`, the Grade A condition may never run.

Nested if

A nested if means writing one `if` inside another `if`. It is useful when a second condition should be checked only after the first condition is true.

```c

if (marks >= 40) {

if (marks >= 75) {

printf("Good score");

}

}

```

Switch Example

`switch` is useful when one variable is compared with fixed constant values. It is commonly used for menu-driven programs.

```c

#include <stdio.h>

int main(void) {

int day = 2;

switch (day) {

case 1:

printf("Monday\n");

break;

case 2:

printf("Tuesday\n");

break;

default:

printf("Invalid day\n");

}

return 0;

}

```

Role of break in switch

`break` stops execution from falling into the next case. Without `break`, C may continue running the statements of the next case also.

The `default` block is optional, but it is useful for handling invalid choices.

if else vs switch

Decision-Making in Real Programs

Decision-making is used in login systems, eligibility checks, grading systems, calculators, ATM menus, game logic, and validation rules.

Whenever a program must choose one path from many possible paths, control flow is involved.

Quick Practice

Write a program that takes a number as input and checks whether it is positive, negative, or zero. Then try a second program that takes a day number and prints the day name using `switch`.

More C Lessons

Browse all PrepCampus study materials