If Statement
We
use if statement when we have to enter in a section of code on the
basis of some condition. For example, we use if statement to check if
the username & password entered by user is correct then we allow him
to do the particular task. Otherwise that area is restricted for
him/her.
Whatever we
write inside the if statement will execute only when the condition is
true. If the condition is false, it will not execute. True returns a
non-zero number(say 1) and false evaluates to zero.
If Syntax
The structure if if statement is as follows:-
if (some condition)
{
---------
some code
---------
}
Note:- dashed lines show that some code will be there.
Now, if the condition is true then only the code written inside of if will execute. Otherwise it will not execute.
Example:- if ( 20 > 6)
{
printf("Condition is true");
}
Now,
here we see that 20 > 6 is true. So, it will go inside of if and
will execute the printf statement and print the statement 'Condition is
true'.
Else
We
use else when the condition in if evaluates to zero. But, its not
necessary to put else everywhere you used if. It depends on your
requirement. Suppose there is a condition. On the basis of that
condition, you want to perform some operations. If the condition is true
then some operations and if the condition is false then some different
operations.
Syntax for if else
if (some condition)
{ // <- if condition is true, it will execute
---------
Some code
----------
}
else
{ // <- if condition is false, it will execute.
---------
some code
----------
}
Example:-
if (20 < 6)
{
printf("condition is true");
}
else
{
printf("condition is false");
}
Output:-
As here, the condition in if is not true, so else part will execute
and it will print "condition is false" on the screen.
Else If
We use elseif when we have to execute some different instructions
on the basis of some different conditions. Syntax for if, else, else if
is as follows:-
if ( condition 1) // It will check for this condition first
// If above
condition is true, compiler will
execute this block of code
{
-----------
statement 1
------------
}
elseif ( condition 2) // if above condition is false, compiler will
check this condition
{ // if this condition is true, compiler will
execute this block of code.
-------------
statement 2
------------
}
elseif ( condition 3 ) // if 2nd condition is false, compiler will
check this condition.
{ // if this condition is true, compiler will execute this block of code.
------------
statement 3
------------
}
else // if none of condition is true, compiler will
come here and execute it.
{
------------
statement 4
------------
}
Example with a program using if, else, else if
WAP to enter percentage of marks and assign grade accordingly.
#include<stdio.h>
void main()
{
int no;
printf(" Enter your marks in percentage");
scanf("%d",&no);
if (no >=90)
{
printf("Your grade is A");
}
elseif (no >=75 && <90)
{
printf("Your grade is B");
}
elseif ("no >= 60 && < 75)
{
printf("Your grade is C");
}
else
{
printf("You didn't qualify");
}
}
Another Example for if else - WAP to check a number is odd or even
#include<stdio.h>
void main()
{
int no;
printf ("Enter a number");
scanf ("%d",&no);
if (no % 2 == 0)
{
printf("Number entered is even");
}
else
{
printf("Number entered is odd");
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment