![]() |
The if-else statement in C is a flow control statement used for decision-making in the C program. It is one of the core concepts of C programming. It is an extension of the if in C that includes an else block along with the already existing if block. C if StatementThe if statement in C is used to execute a block of code based on a specified condition. The syntax of the if statement in C is: if (condition) { // code to be executed if the condition is true } C if-else StatementThe if-else statement is a decision-making statement that is used to decide whether the part of the code will be executed or not based on the specified condition (test expression). If the given condition is true, then the code inside the if block is executed, otherwise the code inside the else block is executed. Syntax of if-elseif (condition) { // code executed when the condition is true } else { // code executed when the condition is false } How to use if-else in C?The following program demonstrates how to use if-else in C: C
Output
5 is less than 10.
How if-else Statement works?Working of the if-else statement in C is explained below:
![]() Structure of if-else Syntax in C We can understand the working of the if-else statement in C with the help of the flowchart. Flowchart of the if-else statement![]() Flowchart of if-else in C Examples of if-else Statement in CThe following are two basic examples of the if-else statement that shows the use of the if-else statement in a C program. Example 1: C Program to check whether a given number is even or oddFor a given number to be even, it should be perfectly divisible by 2. We will use the if-else statement to check for this condition and execute different statements for when it is true and when it is false. C
Output
Number is even Example 2. C Program to check whether a person is eligible to vote or not.We know that a person is eligible to vote after he/she is at least 18 years old. Now we use this condition in the if-else statement to check the eligibility of the person. C
Output
Person 1 is not eligible to vote. Person 2 is eligible to vote. You may notice that in the second example, we did not enclose the body of the if and else statement in the braces and still the code is running without error. This is because the C language 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
Disadvantages of if-else Statement
ConclusionIn this article, we discussed how to use the if-else statement in C for making decisions in our program based on the specified conditions. Being the core concept of C programming, it is frequently used in almost all C programs. FAQs on if-else Statement in C1. Can we skip braces around the body of the if-else block in C?Answer:
2. What is an if-else statement example?Answer:
3. What are the types of if-else statements in C?Answer:
4. What is the syntax of the if-else statement?Answer:
Related Articles: |
Reffered: https://www.geeksforgeeks.org
C Language |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |