If and If Else in C
If and If Else in C
In the world of programming, like our real life, there also arises situations where we
need to make some decisions and based on these decisions we execute the tasks.
For example, if marks of a student are more than 49 then decide the grade “Pass”
otherwise decide the grade “Fail”. There are many more other situations where we
need to make such kind of decisions. Decision-making statements in programming
languages are used to decide the flow of program control. Decision-making statements
available in C are:
1. if statement
2. if-else statements
3. nested if statements
4. if-else-if ladder
5. switch statements
6. Jump Statements:
a) break
b) continue
c) goto
d) return
1. if Statement: This is the simplest decision-making statement used to decide whether
a certain statement or block of statements based on some specified condition will
be executed or not. If the specified condition is “true” then the statement or block of
statement is executed otherwise ignored.
Syntax:
if(condition)
{
// Statements to be executed if the specified condition is true
}
Here, the statements within the curly braces “{ }” are executed if the given condition
is true otherwise the statements are not executed.
#include <stdio.h>
void main ()
{
int n;
printf (“Enter a number: - “);
scanf (“%d”, &n);
if (n > 100)
{
printf ("Hello”);
}
printf ("Bye Everyone");
}
Here, if the user enters number more than 100 then the condition of “if” becomes true
therefore, “Hello” is printed, and after it “Bye Everyone” is printed on the monitor
otherwise only “Bye Everyone” is printed on the monitor.
Note: By default, all control statements execute single statement
immediately after it regardless the curly brace is used or not. “if” is one
of the control statements in C, therefore, this rule is also applicable with
it.
if(condition)
statement1;
statement2;
Here if the condition is true, then only statement1 will be executed not both of the
statements. So, the above program can be written as:
#include <stdio.h>
void main()
{
int n;
printf(“Enter a number:- “);
scanf(“%d”,&n);
if (n > 100)
printf("Hello”);
printf("Bye Everyone");
}
2. if – else Statement: This statement is used to execute only one block of code
based on the result of the specified condition in the “if”. If the given condition is
evaluated to true, then the statements within “if” block are executed otherwise the
statements within the “else” black are executed. Using the “else” block is
completely optional and is used according to the requirements of the program.
Syntax:
if (condition)
{
Executes this block if condition is evaluated to true
}
else
{
Executes this block if condition is evaluated to false
}
Example:
#include <stdio.h>
void main()
{
int n;
printf(“Enter a number:- “);
scanf(“%d”,&n);
if (n % 2 == 0)
printf("%d is even!!”, n);
else
printf("%d is odd!!”, n);
}
Here, if the given number is divisible by 2, that is n % 2 gives 0, then the “if” block is
executed otherwise the “else” block is executed.