C If…Else Statement https://www.skillvertex.com/blog Fri, 10 May 2024 07:17:27 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png C If…Else Statement https://www.skillvertex.com/blog 32 32 C If…Else Statement https://www.skillvertex.com/blog/c-ifelse-statement/ https://www.skillvertex.com/blog/c-ifelse-statement/#respond Fri, 10 May 2024 07:17:27 +0000 https://www.skillvertex.com/blog/?p=2270 Read more]]>

Table of Contents

C If…Else Statement

The “if…else” statement in the C programming language is a fundamental control structure that allows you to make decisions and control the flow of your program based on certain conditions. With “if…else,” you can specify two different blocks of code to be executed, one when a given condition is true, and another when the condition is false. This conditional statement is pivotal in enabling your program to make intelligent choices and respond differently to various situations, enhancing its versatility and functionality.

C if Statement

The if statement in C functions to execute a block of code based on a specified condition.

Syntax of C if statement in C

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

C if-else Statement

The if-else statement is a way to make decisions in programming. It checks a condition, like a true or false question. If the condition is true, it does one thing (inside the “if” part). If the condition is false, it does another thing (inside the “else” part). This helps programs react differently to different situations.

Syntax of if-else

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

How to use if-else in C?

// C Program to demonstrate the use of if-else statement
#include <stdio.h>
 
int main()
{
 
    // if block with condition at the start
    if (5 < 10) {
 
        // will be executed if the condition is true
        printf("5 is less than 10.");
    }
 
    // else block after the if block
    else {
 
        // will be executed if the condition is false
        printf("5 is greater that 10.");
    }
 
    return 0;
}

Output

5 is less than 10.

How if-else Statement work?

  1. It checks the test condition.
  2. If the condition is true, it executes the code inside the if block.
  3. If the condition is false, it executes the code inside the else block (if there is one).
  4. Afterward, the program continues to execute statements below the if-else statement, regardless of which block was executed.

Examples of if-else Statement in C

Example 1: C Program to check whether a given number is even or odd

A Number should be divided by 2, in order to become even. Hence, we will use the if-else statement to check the condition and execute different statements for when it is true and when it is false.

// C Program to Demonstrate the working of if-else statement
#include <stdio.h>
 
int main()
{
 
    // Some random number
    int num = 9911234;
 
    // checking the condition at the start of if block
    if (num % 2 == 0) {
        // executed when the number is even
        printf("Number is even");
    }
    // else block
    else {
        // executed when the number is odd
        printf("Number is Odd");
    }
 
    return 0;
}

Output

Number is even

Example 2. C Program to check whether a person is eligible to vote or not.

A person is eligible to vote after he/she is at least 18 years old. Therefore, we use this condition in the if-else statement to check the eligibility of the person.

// C Program to check whether the person is eligible to vote
// or not
#include <stdio.h>
 
int main()
{
 
    // declaring age of two person
    int p1_age = 15;
    int p2_age = 25;
 
    // checking eligibility of person 1
    if (p1_age < 18)
        printf("Person 1 is not eligible to vote.\n");
    else
        printf("Person 1 is eligible to vote.\n");
 
    // checking eligiblity of person 2
    if (p2_age < 18)
        printf("Person 2 is not eligible to vote.\n");
    else
        printf("Person 2 is eligible to vote.");
 
    return 0;
}

Output

Person 1 is not eligible to vote.
Person 2 is eligible to vote.

In the second example, we can notice that they didn’t put braces for the if and else statements. Even though, the code is running without error. This occurs due to the special property of the C language, that is, it allows the skipping of the braces around the body of the if-else statement when there is only one statement in the body.

Advantages of if-else Statement

The if-else statement allows users to execute various statements based on different conditions. It can evaluate test expressions of various types, including int, char, and boolean. This statement is valuable for altering the program’s flow, making it more versatile. It’s simple, efficient, and easier to read when dealing with a smaller number of conditions.

Disadvantages of if-else Statement

When there are numerous if statements in the code, it can become challenging to read and maintain, leading to complexity and reduced readability. Additionally, performance may be impacted, and the code can become slower when compared to using a switch statement, especially if there are many conditions to evaluate.

FAQ- C If…Else Statement

Q1. What are the 4 types of if statements in C?

Ans. There are four types of if Statement in c: simple if, if-else, nested if-else, and else-if ladder. In C, if statement supports two-way branching statement and multi-way branching statement.

Q2. What is if statement syntax?

Ans. The syntax of an ‘if’ statement in C programming language is − if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } If the Boolean expression evaluates to true, then the block of code inside the ‘if’ statement will be executed.

Q3. Is an if-else a loop?

Ans. In programming, for loops are used for repetitive tasks, executing code a set number of times. if/else statements, on the other hand, are conditional, making decisions based on true or false conditions. They serve different purposes and aren’t in a precedence order; their use depends on the specific needs of your code.


Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/c-ifelse-statement/feed/ 0