If -Else statements in C program
If-Else Statement
Content:
Expressions Condational Statements
👉An
expression is the combination of functions, operators, commas, variables,
identifiers etc.
CLICK HERE to Visit My Youtube Channel
An expression, which
returns only
two value either TRUE or FALSE is known as Boolean expression
& Its becomes a statement.CONDITIONAL STATEMENTS
👉Conditional statement is a feature
of programming language which allows it to perform actions depending upon some
conditions provided by the programmer.
ØIf
the given condition is true then the set of statements are executed otherwise
body is skipped.
ØTwo types of Conditional Statements in C are:
if … else statement
switch case statemens
The if Statement
Output Of The Program |
Example Code:
#include <stdio.h>
int
main()
{
int
password=1234;
printf("Enter
The password:");
scanf("%d",
&password);
if ( password == 1234)
{
printf("Welcome
To My World.");
}
else
{
printf("Go
To Hell");
}
return 0;
}
Nested If – else :
What is If – else nesting?
👉Writing an entire if -else construct within
either the body of the if statement or the body of an else statement is called
“Nesting of if – else”..
Why is it needed ?
👉The if...else statement executes
codes depending upon the condition is true or false. Sometimes, a choice has to
be made from more than 2 possibilities. So the it allows to execute codes for
more than two conditions.
Syntax of
if-else :
if(condition
1 is true)
{
if(condition
2 is true)
{
statement 1;
}
else
}
statement
2;
}
else
{
statement
3;
}
👉Example:
Program
to find the oldest among three:
#include <stdio.h>
int
main ()
{
int a,b,c;
printf("Enter
the ages for Imran, Mushfik,
Riduan
:");
scanf("%d%d%%d",
&a,&b,&c);
if(a>b)
{
if (a>c)
printf("%d
= Imran is the oldest",a);
else
printf("%d
= Riduan
is the oldest",c);
}
else
{
if (b>c)
printf("%d
= Mushfik
is oldest ",b);
else
printf("%d
= Riduan
is the oldest ",c);
}
return 0;
}
This Content is Created By:
Md.Imran Hossain , Md.Riduyanur Rahman , Md.Musfiqur Rahman |
No comments
Thanks For Commenting .
we'll approve your comment soon.
Thank you