top

Search

C Tutorial

.

UpGrad

C Tutorial

Conditional Statements in C

In computer programming, statements are executed based on predefined rules, and logical conditions determine the control flow. However, there are situations where it becomes necessary to execute custom logic. This is where control statements come in. Control statements allow us to define and execute customised logic based on specific conditions.

With a control statement, the program enters a block of statements and executes them only if the specified logic is met. This ability to control the order of statement execution is why these constructs are called control statements. They are commonly used to determine the sequence in which statements should be executed. 

Let’s delve into a deep understanding of conditional statements in C with examples and why we need them.

What Are Control Statements in C?

Control Statements are crucial in guiding the computer to execute specific logical statements. They allow us to determine whether a particular set of statements should be executed or not based on certain conditions. Control statements are used to direct the flow of execution, ensuring that statements are executed only when specific conditions are met. 

In simpler terms, control statements in C help us control the program’s flow and decide which statements need to be executed under specific conditions. 

Why Do We Use Control Statements in C?

In the C programming language, the control passes from one instruction to another. This flow of control is known as subsequent control flow. In C programming, it is common for programmers to need to repeat sets of instructions or skip certain instructions based on logical conditions. In such cases, they utilise control structures in C, also called decision-making or conditional statements.

Conditional statements in C allow programmers to make decisions based on specific conditions. If no condition exists around the statements, these conditional statements are executed sequentially. However, when a condition is applied to a block of statements, the flow of execution can be altered based on the evaluated result of the condition in the program. This process is commonly referred to as decision-making in the C language.

Types of Conditional Control Statements in C

There are 4 types of control statements in C - 

  • Conditional Statements

  • Decision-making Control Statements 

  • Loop Control Statements in C

  • Goto Statements in C

Conditional Statements

The switch statement in C enables multi-way branching based on the value of the switch expression.

When the switch statement is encountered, the control is transferred to the specific case label that matches the value of the switch expression. The statements associated with that case label are then executed. If none of the cases match the switch expression, the default statement is executed instead.

The syntax for conditional control statements in C is - 

if (condition) {
    // Statements to be executed if the condition is true
} else if (condition2) {
    // Statements to be executed if condition2 is true
} else {
    // Statements to be executed if none of the conditions are true
}

Here is control statements in c example programs - 

#include <stdio.h>

int main() {
    int num = 10;

    if (num > 0) {
        printf("The given number is positive.\n");
    } else if (num < 0) {
        printf("The given number is negative.\n");
    } else {
        printf("Thegiven  number is zero.\n");
    }

    return 0;
}

Output:

The given number is positive.

Decision-Making Control Statements Are:

Simple if statement

In C, simple if statements are used to perform specific operations when a condition is true. If the condition in the if statement evaluates to true, the statements within the if block are executed. However, if the condition is false, the control is transferred to the statements outside the if block.

The syntax for a simple if statement is - 

if (condition) {
    // Statements to be executed if the condition is true
}

An example to help you understand better - 

#include <stdio.h>

int main() {
    int value = 10;

    if (value > 0) {
        printf("The value is positive.\n");
    }

    printf("This statement is always executed.\n");

    return 0;
}

Output:

The value is positive.
This statement is always executed.

If-else statements 

In certain scenarios, statements must be executed based on true or false conditions. To handle such situations, if-else statements are utilised. If the condition specified in the if statement is true, the statements within the if block are executed. Conversely, if the condition evaluates to false, the statements within the else block are executed.

The syntax for if-else statements is - 

if (condition) {
    // Statements to be executed if the given condition is true
} else {
    // Statements to be executed if the given condition is false
}

Let’s understand this better with an example - 

#include <stdio.h>

int main() {
    int value = 10;

    if (value > 0) {
        printf("The value is positive.\n");
    } else {
        printf("The value is non-positive.\n");
    }

    return 0;
}

Output:

The value is positive.

Nested if-else statements

Nested if-else statements include additional if or else statements within an existing if or else block. In this structure, if the condition of the outer if statement is true, the statements within its corresponding if block are executed. This block may also contain an inner if statement. If the condition of the inner if statement is true, the statements within its if block is executed. However, if the condition of the inner if statement is false, the statements within its else block are executed instead.

On the other hand, if the condition of the outer if statement is false, the statements within its else block are executed. This else block may also contain another if statement. If the condition of this inner if statement is true, the statement within its if block is executed. Otherwise, if the condition of the inner if statement is false, the statements within its else block are executed.

The syntax for nested if-else statements is - 

if (condition1) {
    // Statements to be executed if condition1 is true

    if (condition2) {
        // Statements to be executed if both condition1 and condition2 are true
    } else {
        // Statements to be executed if condition1 is true but condition2 is false
    }

} else {
    // Statements to be executed if condition1 is false
}

Example - 

#include <stdio.h>

int main() {
    int num = 10;

    if (num > 0) {
        printf("The given number is positive.\n");

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

    } else {
        printf("The given number is non-positive.\n");
    }

    return 0;
}

Output:

The given number is positive.
The given number is even.

else-if ladder

The else-if ladder statements in C allow the testing of multiple conditions. Each else-if condition is evaluated sequentially, and if any condition is true, the statements within that if block are executed. If none of the conditions are true, the statements within the else block are executed.

Using the else-if ladder structure, programmers can handle multiple conditions and execute specific sets of statements based on the first true condition encountered.

The syntax for else-if statements is - 

if (condition1) {
    // Execute these statements if condition1 is true
} else if (condition2) {
    // Execute these statements if condition1 is false and condition2 is true
} else {
    // Execute these statements if both condition1 and condition2 are false
}

Example - 

#include <stdio.h>

int main() {
    int num = 10;

    if (num > 0) {
        printf("The given number is positive.\n");
    } else if (num < 0) {
        printf("The given number is negative.\n");
    } else {
        printf("The given number is zero.\n");
    }

    return 0;
}

Output - 

The given number is positive.

Loop Control Statements in C

While Loop

The while loop in C is called an entry loop because it evaluates the condition before executing the statements within its body. If the while loop condition is initially false, the statements within the loop will not be executed.

The syntax for the while loop is - 

while (condition) {
    // Execute these statements as long as the condition is true
}

Example - 

#include <stdio.h>

int main() {
    int count = 0;

    while (count < 5) {
        printf("Count: %d\n", count);
        count++;
    }

    return 0;
}

Output:

Count: 0
Count: 1
Count: 2
Count: 3
Count: 4

do-while Loop

A do-while loop in C is often called an exit loop because the statements within its body are executed first, and then the condition is checked. If the condition of the while loop is true, the body of the loop will be executed repeatedly until the condition becomes false. Once the condition is false, the control will transfer outside the do-while loop, and the statements following the do-while loop will be executed.

The syntax for the do-while loop is - 

do {
    // Statements to be executed at least once
} while (condition);

Example - 

#include <stdio.h>

int main() {
    int count = 0;

    do {
        printf("Count: %d\n", count);
        count++;
    } while (count < 5);

    printf("Loop finished!");

    return 0;
}

Output - 

Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Loop finished!

For Loop

A for loop in C is often called a pretest loop because the condition is checked before executing the statements within its body. The for loop syntax consists of three expressions: initialization (expression1), conditional expression (expression2), and updation (expression3). Variables can be initialised within the for statement itself.

The syntax for For Loop is - 

for (expression1; expression2; expression3) {
    // Statements to be executed as long as the condition is true
}

Example - 

#include <stdio.h>

int main() {
    int i;

    for (i = 0; i < 5; i++) {
        printf("Count: %d\n", i);
    }

    printf("Loop finished!");

    return 0;
}

Output:

Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Loop finished!

Goto Statements in C

The goto statement in C transfers the flow of control within a program. It allows jumping to a specific part of the program. The label used in the goto statement serves as a marker to direct the program's branch to a specified point.

The syntax for Goto Statement in C is - 

goto label;
// ...
label:
// Statements to be executed

Example:

#include <stdio.h>

int main() {
    int number;

    printf("Enter a positive number: ");
    scanf("%d", &number);

    if (number <= 0) {
        printf("Invalid input! Please enter a positive number.\n");
        goto end;
    }

    printf("The number you entered is: %d\n", number);

    end:
    printf("Program execution complete.\n");

    return 0;
}

Conclusion

Conditional control statements in C provide the means to make decisions and control the program's flow based on specific conditions. They enable executing different code blocks depending on the evaluation of conditions, offering flexibility and adaptability in programming. Using these statements judiciously can help programmers create efficient and dynamic programs that respond intelligently to varying scenarios.

Students are also encouraged to study the Graduate Certificate Programme in Data Science upGrad from upGrad, which will help them gain an insight into the vast world of data science. Studying and upgrading skills like programming, predictive analytics, advanced tech, and more will help them become experts. 

So don’t let this opportunity pass on, enroll now

FAQs

1. In what situations do we use the nested if statement?

The nested if statement is used when checking additional conditions within an if or else block. It allows for more complex decision-making by nesting multiple if-else statements.

2. What are the control statements available in C?

The control statements in C include if, if-else, nested if-else, switch, while, do-while, for, and goto. These statements control the flow of execution based on conditions or looping requirements.

3. How is the conditional operator used in C?

The conditional operator in C, denoted as "? :", is a ternary operator that allows for inline conditional expressions. It evaluates a condition and returns one of two values based on whether true or false.

Leave a Reply

Your email address will not be published. Required fields are marked *