IF Statements
c program-ல் உள்ள statements அனைத்தும் வரிசையாக (அ)ஒவ்வொரு statement-ஆக program execution செய்யப்பட்டது.ஆனால் இந்த if statement ஆனது.program execution-யை ஒரு இடத்தில இருந்து மற்றோர்ரு மற்றோரு இடத்திற்கு கொண்டு செல்கின்றது.
if statement
if statement ஆனது execution ஆகும் போது முதலில் condition ஆனது check செய்யப்படும்.condition true என்றால் அதன் பிறகு வருகின்ற single statement (அல்லது)group of statement ஆனது execution செய்யப்படும்.condition false என்றால் next statement ஆனது execute செய்யப்படும்.
Syntax
if(condition)
{
statements;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
printf("Enter your AGE");
scanf("%d",&age);
if(age>18)
{
printf("\n Eligible for the exam");
}
printf("Not Eligible for the exam");
getch();
}
OUTPUT 1:
Enter your AGE:19
Eligible for the exam
OUTPUT 2:
Enter the AGE:10
Not Eligible for the exam
if…else
இதில் condition-யை check செய்யப்படும் போது true-ஆக இருந்தால் ஒரு group of statement-ஆனது execute செய்யப்படுகிறது.false-ஆக இருந்தால் வேறொரு group of statement-ஆனது execute செய்யப்படுகிறது.
syntax:
if(condition)
{
statement 1;
statement 2;
.
.
.
true block;
.
.
.
statement n;
}
else
{
statement 1;
statement 2;
.
.
.
.
false block;
.
.
.
statement n;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int mark;
printf("Enter your MARK");
scanf("%d",&mark);
if(mark>35)
{
printf("\n PASS");
}
else
{
printf("\n FAIL");
}
getch();
}
OUTPUT 1:
Enter your MARK:30
FAIL
OUTPUT 2:
Enter the MARK:50
PASS
If..Elseif
முதலில் if-ல் உள்ள condition check செய்யப்படும்போது condition ஆனது true என்றால் if block உள்ள group of statement ஆனது execute செய்யப்படும்.இல்லையென்றால்,else if-க்கு சென்று அங்கிருக்கும் condition ஆனது check செய்யப்படும்.condition ஆனது true என்றால் else if-ல் இருக்கின்ற group of statement ஆனது exeute செய்யப்படும்.அனைத்தும் condition-களும் false என்றால் கடைசியாக else block-ல் உள்ள statement ஆனது execute செய்யப்படும்.
syntax:
if(condition)
{
statements;
}
elseif(condition)
{
statements;
}
else(condition)
{
statements;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int day;
printf("Enter a number between 1 and 7");
scanf("%d",&day);
if(day==1)
{
printf("\n MONDAY");
}
elseif(day==2)
{
printf("\n TUESDAY");
}
elseif(day==3)
{
printf("\n WEDNESDAY");
}
elseif(day==4)
{
printf("\n THRUSDAY");
}
elseif(day==5)
{
printf("\n FRIDAY");
}
elseif(day==6)
{
printf("\n SATURDAY");
}
elseif(day==7)
{
printf("\n SUNDAY");
}
else
{
printf("\nplease....give a number between 1 to 7");
}
getch();
}
OUTPUT 1:
Enter a number between 1 and 7:1
MONDAY
OUTPUT 2:
Enter a number between 1 and 7:8
please....give a number between 1 to 7
Nested if…else statement:
if (or) else block-ன் உள்ளே மறுபடியும் if (or) else statement வந்தால் இதனை nested if ...else statement என்று சொல்வோம்.
syntax:
if(condition 1)
{
if(condition 2)
{
statements;
}
else
{
statements;
}
}
else
{
statements;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter three numbers:");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("\n Big=%d",a);
}
else
{
printf("\n Big=%d",c);
}
}
else
{
if(b>c)
{
printf("\n Big=%d",b); }
else
{
printf("\n Big=%d",c);
}
}
}
OUTPUT:
Enter three numbers:7 6 8
Big=8
No comments:
Post a Comment