“if statement”
if is a keyword in c [Link] statement is a decision making
statement. It is a simplest form of selection [Link] is used
to execute or skip statement or set of statements by checking
condition.
The condition is given as relational [Link] the condition is
true,then the statement or set of staements after if statement
is [Link] the condition is false,then the statement or set of
statement after if statement is not executed.
Syntax/general form:
The syntax of if statement is as follow:
if(condition)
Statement;
The above syntax is used for single statement. A set of
statements can also be make [Link] this case, these
satements are written in curly brackets{ }. The set of statement
is also called conditional statement.
Flowchart:
The flowchart of if statement is as follows:
conditi
on statement
The syntax for compound statement in if statements is as
follows:
If (condition)
{
Statement1;
Statement2;
.
.
StatementN;
Program:
Write a program that input marks and display
“congratulation! You have passed”.If marks are 40 or
more.
#include<stdio.h>
#include<conoi.h>
Void main( )
{
int marks;
clrscr( );
printf(“enter your marks:”);
scanf(“%d”,&marks);
if(marks>=40)
printf(“congratulation! You have passed”);
getch( );
}
Output:
Enter your marks:60
congratulation!you have passed.
“if else statement”
if else statement is another form of if [Link] execute
one block of statement(s) when the condition is true and other
when it is [Link] any situation one block is executed and other
is [Link] if else statement :
Both blocks of statements can never be executed.
Both block of statements can never be skipped.
Syntax:
if(condition)
statement;
else
statement;
Two or more statements are written in curly brackets{ }.The
syntax for compound statement in if else statement is as
follows:
If(condition)
{
Statement1;
Statement2;
.
.
StatementN;
}
else
{
Statement1;
Statement2;
.
.
StatementN;
}
Flowchart:
conditi Statement
on (s)
Statement(s)
Program:
Write a program that input a number and find whether it is
even or odd using if else statement.
#include<stdio.h>
#include<conoi.h>
void main( )
{
int n;
clrscr( );
printf(“enter a number:”);
scanf(“%d”,&n”);
if(n%2==0)
printf(“number is even”);
else
printf(“number is odd”);
getch( );
}
Output:
enter a number:10
Number is even.