C Programming

Chapter 4: Decision-Making Statements in C

Chapter 4: Decision-Making Statements in C

In programming, a program often needs to make decisions based on certain conditions.

For example:

Is a student eligible to pass?

Is a person eligible to vote?

Which number is greater?

What grade should be assigned based on marks?

In C language, decision-making statements allow a program to choose different actions depending on whether a condition is true or false.

4.1 What is a Condition?

A condition is an expression that produces either:

True → represented by a non-zero value

False → represented by 0

Example:

age >= 18

If age is 20, the condition is true.

4.2 Types of Decision-Making Statements

C provides several decision-making statements:

if statement

if-else statement

Nested if statement

else-if ladder

switch statement

Conditional operator ?:

4.3 The if Statement

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

Syntax

if (condition) {    // Code to execute }

Example: Check Voting Eligibility

#include <stdio.h> int main() {    int age;    printf("Enter your age: ");    scanf("%d", &age);    if (age >= 18)    {        printf("You are eligible to vote.");    }    return 0; }

If the age is 18 or more, the message will be displayed.

4.4 The if-else Statement

The if-else statement is used when there are two possible choices.

Syntax

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

Example: Check Even or Odd Number

#include <stdio.h> int main() {    int number;    printf("Enter a number: ");    scanf("%d", &number);    if (number % 2 == 0)    {        printf("Even Number");    }    else    {        printf("Odd Number");    }    return 0; }

4.5 Checking Positive or Negative Numbers

#include <stdio.h> int main() {    int number;    printf("Enter a number: ");    scanf("%d", &number);    if (number >= 0)    {        printf("Positive Number");    }    else    {        printf("Negative Number");    }    return 0; }

Note: Zero is neither positive nor negative. To handle zero separately, use an else-if ladder.

4.6 The else-if Ladder

When a program needs to check multiple conditions, we can use an else-if ladder.

Syntax

if (condition1) {    // Code } else if (condition2) {    // Code } else if (condition3) {    // Code } else {    // Default code }

Example: Check Positive, Negative, or Zero

#include <stdio.h> int main() {    int number;    printf("Enter a number: ");    scanf("%d", &number);    if (number > 0)    {        printf("Positive Number");    }    else if (number < 0)    {        printf("Negative Number");    }    else    {        printf("Number is Zero");    }    return 0; }

4.7 Program to Display Grade

The following program displays a grade based on marks.

#include <stdio.h> int main() {    int marks;    printf("Enter marks: ");    scanf("%d", &marks);    if (marks >= 90)    {        printf("Grade A");    }    else if (marks >= 75)    {        printf("Grade B");    }    else if (marks >= 60)    {        printf("Grade C");    }    else if (marks >= 40)    {        printf("Grade D");    }    else    {        printf("Fail");    }    return 0; }

Example

If marks are 82, the output will be:

Grade B

4.8 Nested if Statement

An if statement inside another if statement is called a nested if statement.

Syntax

if (condition1) {    if (condition2)    {        // Code    } }

Example: Check Eligibility

#include <stdio.h> int main() {    int age;    int citizen;    printf("Enter age: ");    scanf("%d", &age);    printf("Enter 1 if you are a citizen, otherwise enter 0: ");    scanf("%d", &citizen);    if (age >= 18)    {        if (citizen == 1)        {            printf("Eligible");        }        else        {            printf("Not Eligible");        }    }    else    {        printf("Not Eligible");    }    return 0; }

4.9 The switch Statement

The switch statement is useful when one expression needs to be compared with several fixed values.

Syntax

switch (expression) {    case value1:        // Code        break;    case value2:        // Code        break;    default:        // Default code }

Example: Display Day Name

#include <stdio.h> int main() {    int day;    printf("Enter day number (1-7): ");    scanf("%d", &day);    switch (day)    {        case 1:            printf("Monday");            break;        case 2:            printf("Tuesday");            break;        case 3:            printf("Wednesday");            break;        case 4:            printf("Thursday");            break;        case 5:            printf("Friday");            break;        case 6:            printf("Saturday");            break;        case 7:            printf("Sunday");            break;        default:            printf("Invalid Day Number");    }    return 0; }

4.10 Importance of break

The break statement terminates the current case in a switch statement.

Without break, execution may continue into the following case. This behavior is known as fall-through.

Example:

switch (number) {    case 1:        printf("One");        break;    case 2:        printf("Two");        break; }

4.11 The default Statement

The default statement is executed when none of the case values match.

default:    printf("Invalid Choice");

The default section is optional, but it is useful for handling unexpected input.

4.12 Menu-Driven Program Using switch

#include <stdio.h> int main() {    int choice;    printf("1. Addition\n");    printf("2. Subtraction\n");    printf("3. Multiplication\n");    printf("Enter your choice: ");    scanf("%d", &choice);    switch (choice)    {        case 1:            printf("You selected Addition");            break;        case 2:            printf("You selected Subtraction");            break;        case 3:            printf("You selected Multiplication");            break;        default:            printf("Invalid Choice");    }    return 0; }

4.13 Conditional Operator ?:

The conditional operator can be used as a short form of a simple if-else statement.

Syntax

condition ? expression1 : expression2;

Example: Find Greater Number

#include <stdio.h> int main() {    int a, b, greater;    printf("Enter two numbers: ");    scanf("%d %d", &a, &b);    greater = (a > b) ? a : b;    printf("Greater Number = %d", greater);    return 0; }

4.14 Comparison of if-else and switch

Featureif-elseswitch
ConditionCan use complex conditionsUsually compares one expression with fixed case values
OperatorsCan use relational and logical operatorsUses case labels
Best forRanges and complex decisionsFixed choices or menu options
ExampleMarks, age, eligibilityDay number, menu selection

4.15 Common Mistakes

1. Using = Instead of ==

Incorrect:

if (a = 10)

Correct comparison:

if (a == 10)

2. Missing Braces

Braces improve readability and help avoid mistakes.

Recommended:

if (marks >= 40) {    printf("Pass"); }

3. Forgetting break in switch

case 1:    printf("One");    break;

4.16 Important Points to Remember

if executes code when a condition is true.

if-else provides two alternative paths.

An else-if ladder is useful for multiple conditions.

Nested if means an if statement inside another if.

switch is useful for selecting from fixed options.

break usually stops execution of the current case.

default handles unmatched cases.

?: is called the conditional or ternary operator.

Chapter Summary

In this chapter, you learned how C programs make decisions using:

if

if-else

else-if ladder

Nested if

switch

break

default

Conditional operator

Decision-making statements are important because they allow a program to perform different actions based on different conditions.

The next chapter will introduce Loops in C Language, including:

for loop

while loop

do-while loop

Nested loops

break and continue

Practice Questions

Very Short Answer Questions

What is a decision-making statement?

When is an if statement used?

What is the purpose of else?

What is an else-if ladder?

What is a nested if statement?

What is the purpose of break in a switch statement?

When is the default statement executed?

What is the conditional operator?

Short Answer Questions

Explain the if statement with an example.

Differentiate between if and if-else.

Explain the else-if ladder.

What is a nested if statement?

Explain the syntax and working of a switch statement.

Differentiate between if-else and switch.

Programming Questions

1. Write a program to check whether a number is even or odd.

#include <stdio.h> int main() {    int number;    printf("Enter a number: ");    scanf("%d", &number);    if (number % 2 == 0)    {        printf("Even Number");    }    else    {        printf("Odd Number");    }    return 0; }

2. Write a program to find the greater of two numbers.

#include <stdio.h> int main() {    int a, b;    printf("Enter two numbers: ");    scanf("%d %d", &a, &b);    if (a > b)    {        printf("%d is greater", a);    }    else    {        printf("%d is greater", b);    }    return 0; }

3. Write a program to check whether a student has passed or failed.

#include <stdio.h> int main() {    int marks;    printf("Enter marks: ");    scanf("%d", &marks);    if (marks >= 40)    {        printf("Pass");    }    else    {        printf("Fail");    }    return 0; }

4. Write a program using switch to display the name of a month.

#include <stdio.h> int main() {    int month;    printf("Enter month number: ");    scanf("%d", &month);    switch (month)    {        case 1:            printf("January");            break;        case 2:            printf("February");            break;        case 3:            printf("March");            break;        default:            printf("Enter a valid month number");    }    return 0; }