0% found this document useful (0 votes)
5 views

If-else Statement in C-Programming

The document explains the if-else statement in C programming, which is a fundamental flow control statement used for decision-making. It details the syntax, working, advantages, and disadvantages of the if-else statement, along with examples demonstrating its use in checking conditions like even/odd numbers and voting eligibility. Additionally, it addresses common questions regarding the use of braces and types of if-else statements.

Uploaded by

kussupurne
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

If-else Statement in C-Programming

The document explains the if-else statement in C programming, which is a fundamental flow control statement used for decision-making. It details the syntax, working, advantages, and disadvantages of the if-else statement, along with examples demonstrating its use in checking conditions like even/odd numbers and voting eligibility. Additionally, it addresses common questions regarding the use of braces and types of if-else statements.

Uploaded by

kussupurne
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C if...

else Statement
Last Updated : 16 Jun, 2023

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 Statement
The -jfstatem-en-t 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 Statement
The 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-else
if (condition) {
// code executed whenthe condition is true

// code executed whenthe condition is false

How to use if-else in C?


The following program demonstrates how to use if-else in C:

c
Program to demonstrate the use of if-else statement
# include

int main ()

if block with condition at the start


if (5 < 10)
// willbe executed if the condition is true
print f ( '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.

returnO;

Output

5 is less than 10.

Note: Any non-zero and non-null values are assumed to be true, and zero or null values are
assumedto be false.
How if-else Statement works?
Working of the if-else statement in C is explained below:
1. When the program control first comes to the if-else block, the test condition is checked.
2. If the test condition is true:
• The if block is executed.

3. If the test condition is false:


• The else block is executed

4. After that, the program control continues to the statements below the if-else statement.

if (condition)
// execute code statements of if block when
// condition is true

else

// execute code statements of if block when


// condition is true

Structure Ofif-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

If Cm di tio n

ElseBody

Sta tementJus t Bel owIf

Flowchart Ofif-else in C

Examples of if-else Statement in C


The 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 odd

For 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
Program to Demonstrate the working of if-else statement
# include

int main ( )

// Some random number


int num = 9911234;

// checking the condition at the start of if block


if (num % 2
// executed when the number is even
print f ('Number is even");

// else block
else [
// executed when the number is odd
print f ('Number is Odd");

return 0;

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/sheis at least 18 years old. Now 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

int main ( )

// declaring age of two person


int pl_age
int p2_age 25,

// checking eligibilityof person 1


if (pl_age < 18)
printf ('Person 1 is not eligible to vote. \n•)•
else
printf ('Person 1 is eligible to vote. \n•);

// checking eligiblityof person 2


if (p2_age < 18)
printf ('Person 2 is not eligible to vote. \n')•
else
print f ('Person 2 is eligible to vote. '),

return 0;

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
• The if-else statement enables the user to execute different statements based on different
conditions.
• It can evaluate test expressions of type int, char, boolean, and more.
• It helps in modifying the flow of the program.
• It is simple, efficient, and easier to read when there is less number of conditions.

Disadvantages of if-else Statement


• If there are a lot of if statements present, the code becomes unreadable and complex.
• It also becomes slower compared to the switch statement.

Conclusion
In 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 C

1. Can we skip braces around the body of the if-else block in C?

Answer:

We can skip the braces of the body of the if or else block as long as there is only a single
statement inside their body. We will get an error if there is more than one statement in the
body without braces.

2. What is an if-else statement example?

Answer:

Followingis a simple example of the if-else statement in C:

// C program to illustrate the use of if-else

*include < stdio

int main ()

// if else statement far true condition

if (1)

printf( "The if block is executed.\n");

else {

printf( "The else block is executed\n"),

return O;

Output
Theif block is executed.
3. What are the types of if-else statements in C?

Answer:

Thereare 3 types of if-else statements in C whichare as follows:

1. if Statement

2. if-else Statement

3. if-else-if Ladder

4. What is the syntax of the if-else statement?

Answer:

The syntax of the if-else statement is:

if (test expression)
{
// if body
else {
// else body

You might also like