• Articles
  • Tutorials
  • Interview Questions

if else Statement in C - Syntax and Examples

Tutorial Playlist

Introduction to the if-else statement in C

Control flow statements are the heart of any programming language, allowing developers to dictate the execution path of their code. One of the most fundamental control structures in C programming is the “if-else” statement. This versatile construct empowers programmers to make decisions, perform actions based on conditions, and create dynamic and responsive programs. In this blog, we will delve into the depths of the “if-else” statement, uncover its syntax, explore various use cases, and unlock its potential to make your C programs smarter and more efficient.

What Does the “if” Statement in C Do?

At its core, the “if” statement embodies a conditional mechanism that permits the execution of a code block exclusively when a particular condition holds true. This foundational framework propels programs to make decisions grounded in distinct situations, culminating in software that is responsive and capable of dynamic behavior.

Working of if-else Statement in C

Let’s explore how the “if-else” statement works in C:

if-else Syntax in C: The basic syntax of the “if-else” statement is as follows:

if (condition) {

    // Code block executed if the condition is true

} else {

    // Code block executed if the condition is false

}

Mechanism of if-else statement in C

  • Initiated by the “if” keyword, the statement is enclosed in parentheses containing an evaluative condition, typically a Boolean expression capable of being true or false.
  • When the condition enclosed within the parentheses is assessed as true, the code snippet confined within the initial set of curly braces (the “if” block) is triggered for execution.
  • In instances where the condition yields a false outcome, the code segment enfolded within the secondary set of curly braces (the “else” block) becomes active and is executed.

Flowchart of if-else Statement in C: The flowchart of the “if-else” statement helps illustrate its working

   +-----------+

   | Condition |

   +-----|-----+

         |

   +-----v-----+

   |  If true  |

   | statement |

   +-----|-----+

         |

   +-----v-----+

   |   Else    |

   | statement |

   +-----------+

Example:

#include <stdio.h>

int main() {

    int num = 10;

    

    if (num > 5) {

        printf("The number is greater than 5.\n");

    } else {

        printf("The number is not greater than 5.\n");

    }

    return 0;

}

In this example, the condition num > 5 is evaluated. Since the value of num is 10, which is indeed greater than 5, the code block within the “if” statement is executed, resulting in the output: “The number is greater than 5.”

Exploring Real-World Scenarios in the if-else statement in C

  1. Grade Determination: Imagine you’re designing a student grading system. You can use the “if-else” statement to assign grades based on a student’s score. For instance:
int score = 87;

if (score >= 90) {

    printf("Grade: A");

} else if (score >= 80) {

    printf("Grade: B");

} else if (score >= 70) {

    printf("Grade: C");

} else {

    printf("Grade: F");

}
  1. User Authentication: When building a login system, “if-else” statements are invaluable for verifying user credentials. Here’s a simplified example:
char username[] = "user123";

char password[] = "pass456";

char input_username[20];

char input_password[20];

printf("Enter username: ");

scanf("%s", input_username);

printf("Enter password: ");

scanf("%s", input_password);

if (strcmp(input_username, username) == 0 && strcmp(input_password, password) == 0) {

    printf("Login successful");

} else {

    printf("Login failed");

}
  1. Menu Selection: Interactive menus benefit from “if-else” statements. In a basic calculator program, users can choose an operation by entering a number, and the program responds accordingly:
int choice;

printf("Select operation:\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");

scanf("%d", &choice);

switch (choice) {

    case 1:

        // Perform addition

        break;

    case 2:

        // Perform subtraction

        break;

    case 3:

        // Perform multiplication

        break;

    case 4:

        // Perform division

        break;

    default:

        printf("Invalid choice");

}

Conclusion

The “if-else” statement in C programming holds the power to guide your code’s execution path based on conditions. By using “if-else” statements, you can build adaptable applications that cater to a range of scenarios, from grading systems to authentication processes and interactive menus. Embrace the art of decision-making in programming, and watch your code come alive as it dynamically responds to various inputs and conditions. The “if-else” statement isn’t just a control structure; it’s a gateway to crafting intelligent and user-friendly programs.

Course Schedule

Name Date Details
Python Course 27 Apr 2024(Sat-Sun) Weekend Batch
View Details
Python Course 04 May 2024(Sat-Sun) Weekend Batch
View Details
Python Course 11 May 2024(Sat-Sun) Weekend Batch
View Details

Full-Stack-ad.jpg