Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Input/Output statements
  5. Decision making statement...
  6. if-else statement

if-else statement

if-else statement

Most of the problems require one set of actions to be performed if a particular condition is true, and another set of actions to be performed if the particular condition is false.

Syntax

if(expression)
{
     Statement 1
}
else
{
     Statement 2
}

Flowchart

An if-else statement is executed as follows

  • The if-else controlling expression is evaluated
  • If the if else controlling expression evaluates to true, the statement constituting the body is executed and else body is skipped
  • If the if else controlling expression evaluates to false, the statement constituting the body is skipped and else body is executed

Example

#include<stdio.h>
int main()
{
	int n;
	printf("Enter a number: ");
	scanf("%d",&n);
	if(n%2==0)
	{
		printf("%d is even number",n);
	}
	else
	{
		printf("%d is odd number",n);
	}
	return 0;
}

Output 1

Enter a number: 4
4 is even number

Output 2

Enter a number: 5
5 is odd number

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments