The if-else Statement in C Programming Language
Let us understand if-else statement in C programming language.
Here, if and else are the keywords in C.
Syntax:
1 2 3 4 5 6 |
if (condition is true){ /*1st block of statements*/ } else{ /*2nd block of statements*/ } |
The keyword if tells the compiler that what follows is decision control statement. The block of statements to be executed must be enclosed in opening and closing braces. If only one statement needs to be executed then braces need not be used. Let us understand if-else statement using an example.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
int a = 10; if (a == 10) { printf(“You are in 1st block\n”); printf(“The value of a is %d\n”, a); } else { printf(“You are in 2nd block\n”); printf(“The value of a is not equal to %d\n”, a); } |
In the above example we have assigned value 10 to a. In the next line, there is an if statement. It is read as “If a is equal to 10 then perform the block of statements enclosed within the braces of if part else perform the block of statements enclosed within the braces of else part”. After the execution of if-else statement normal sequence flow of the program is followed.
The output of the above example is:
You are in if block
You are in 1st block
The value of a is 10
The else-if statement
The else-if statement is nothing but the rearrangement of else with the if that follows.
Consider the below program.
1 2 3 4 5 6 |
if(condition) perform this else{ if(condition) perform this; } |
The above program can be re-written as,
1 2 3 4 |
if(condition) perform this else if(condition) perform this; |
Here, the else-if statement is used. Else if reduces the complexity of the program making it easier to understand.
if we get result using if-else,then why we use else if statement. can you tell me properly?
Hello,
The else-if statement is is used when ever we have multiple if conditions.
e.g.
if (condition 1)
{
do this;
}
else if (condition 2)
{
do this;
}
else if(condition 3)
{
do this;
}
else
{
do this;
}
Hope its clear now… Any queries do reply. I will get back to you.
Many Thanks.
thanx…..
@learnconline dude…you know what you are doing now ? you are changing my life…:) thanks for the site…i will share it in every social media i am in…
Thanks everyone for your support… We would need your continuous support to keep this going…
Thanks once again.
Regards,
LearnConline
super thanx buddy you are doing a great job here
Cool site to learn C online.
heres my problem.
create a program that will accept 3 numbers. if the numbers are unique, press 1 for ascending order and press 2 for descending order. if two numbers have the same value it will automatically print the 3 numbers.
great work man….it really people learn c easily….Thank you so much
you have to work with three temperature types: Celsius, Fahrenheit and Kelvin. What we are going to do here, people would come to you to ask to convert temperature. They come with three inputs, the temperature which they want to convert, the type of the temperature and the intended type to convert it. As we are dealing with three types of temperature, we represent them 0, 1 and 2 (Celsius = 0, Fahrenheit = 1 and Kelvin = 2). The temperature is a number (floating point). The output is the temperature in the intended type….
what the ans is ….
cool 🙂
#include
#include
#include
#include
int main()
{
char ch;
cout< <"Enter any character :";
ch=getchar();
if(isalpha(ch))
cout< <"Alphabet";
else if(isdigit(ch))
cout< <"Number";
else
cout< <"Special Character";
getch();
return 0;
}
Shouldn’t the output of the example program in IF-ELSE section be:
you are in 1st block
The value of a is 10
Correct me if I’m wrong.
Cheers 🙂
@Mustafa
i think its just a typo
@learnconline
kudos to you..dude..this stuff is really cool and easy to understand.
#include
#include
main()
{
char sex, Qualification;
int experiance;
printf(“Enter sex Qualification and Experiance: “);
scanf(“%c %c %d”, &sex, &Qualification, &experiance);
if(sex == ‘M’ && Qualification == ‘PG’ && experiance >= 10)
{
printf(“Salary = 15000”);
}
if((sex == ‘M’ && Qualification == ‘UG’ && experiance >= 10) ||
(sex == ‘M’ && Qualification == ‘PG’ && experiance < 10))
{
printf("Salary = 10000");
}
if(sex == 'M' && Qualification == 'UG' && experiance = 10)
{
printf(“Salary = 15000:”);
}
if((sex == ‘F’ && Qualification == ‘UG’ && experiance >= 10) ||
(sex == ‘F’ && Qualification == ‘PG’ && experiance < 10))
{
printf("Salary = 10000");
}
else
{
printf("Salary = 7000");
}
getch();
return 0;
}
it output is 7000 every time. what should be done??
YOU ROCK!!!!! I WAS THINKING HW WILL I LEARN C WITHOUT A TEACHER….HERE I GOT THE BEST SOuRCE U R BETTER THAN ANY TEACHER. I WILL LEARN C IN A WEEK LIKE THIS…..ONE Q. PLZ TELL ME ANY FREE C COMPILER FOR WINDOWS 7 (64 bit) THANKS A LOT MAN
2) #include
#include
int main(void) {
int n;
printf(“Please enter a number: “);
scanf(“%d”, &n);
if (n == 1) {
printf(“n is equal to 1!\n”);
}
else if (n == 2) {
printf(“n is equal to 2!\n”);
}
else if (n == 3) {
printf(“n is equal to 3!\n”);
}
else {
printf(“n isn’t equal to 1, 2, or 3.\n”);
}
system(“PAUSE”);
return 0;
Aoa…
i am doing program which a program to input percentage of the student print out its grade according to the percentage criteria.
when i hve compiled this program its give error of else is misplaced…… can u tell me please whts problem here???
#include
#include
void main()
{
clrscr();
float per;
printf(“please enter the percentage of the student”);
scanf(“%f”,&per);
if(per>=80 && per< =100)
printf(“the grade A+”);
else
if(per>=70 && per<80)
printf(“the grade is A”);
else
if(per>=60 && per<70)
printf(“the grade is B”);
else
if(per>=45 && per<60)
printf(“the grade is c”);
else
if(per>=33 && per<45)
printf(“the grade is D”);
else
printf(“fail”);
hetch();
}
Naveen,why did u use double &&, instead of one & in above program. why can’t we use one &? Explain
what will be the output for
{
int a=10;
if(a)
printf(“%d”,a);
else
printf(“\n nothing”);
}
#include
#include
main()
{
char password[8];
int a,b, sum, prod, diff,order,exit;
float quo,peso, usd,change;
printf(“PASSWORD:\t”);scanf(“%s”,&password);
if (password!=”dakota”)
{
goto reenter;
}
else
{
goto menu;
}
reenter:
clrscr();
printf(“Reenter Password:\t”);
scanf(“%s”,&password);
if(password==”dakota”)
{
menu:
printf(“MENU”);
printf(“CALCULATOR\n”);
printf(“CONVERSION\n”);
printf(“GRADE\n”);
printf(“EXIT\n”);
}
printf(“choose a number from the menu: “);
scanf(“%d”,&a);
else if (a==1)
{
printf(“\nYou chose calculator”);
printf(“\nenter a number: \t”);scanf(“%d”, &a);
printf(“\nenter a another number: \t”);scanf(“%d”, &b);
sum=a+b;
prod=a*b;
diff=a-b;
quo=a/b;
printf(” the sum is= %d \n”, sum);
printf(” the product is= %d \n”, prod);
printf(” the diffence is= %d \n”, diff);
printf(” the quotient is= %.2f \n”,quo);
}
else if (a==2)
{
printf(“welcome to conversion!”);
printf(“\nenter Philippine peso:\t”);scanf(“%f”, &peso);
usd=peso/43;
printf(“\n%.2f”,peso);printf(” is equivalent to:\t%.2f”,usd);
}
else if (a==3)
{
printf(“you chose ordering system”);
clrscr();
printf(“\nwhat’s your order?\t”);scanf(“%d”,&order);
printf(“\ncost:\t”);scanf(“%d”,&a);
printf(“\nmoney received:\t”);scanf(“%d”,&b);
change=b-a;
printf(“\nYour change is:\t%.2f”,&change);
}
else if (a==4)
{goto end;
}
end:getch();
}
-i don’t know anymore how to correct this. i am a beginner. can you help me?
hi , i’d like to ask
my program is running now . but he’s giving a wrong answer
main()
{
int grade;
clrscr();
gotoxy(33,10);printf(“Enter grade: “);
scanf (“%d”,&grade);
if ((grade >=91)&&(grade< =100));{
gotoxy(33,11);printf(“Excellent”);}
if ((grade >=75)&&(grade< =90));{
gotoxy(33,11);printf(“passed”);}
if (grade < =74);{
gotoxy(33,11);printf(“failed”);}
if (grade >=101);{
gotoxy(33,11);printf(“Invalid grade”);}
getch();
}
if i input a 91 grade he shoud be answering “excellent” but what ever the number is the answer is always invalid ..
i asked my prof what’s wrong he replied that I missed a curly braces.. before the statement ? where should I put it I tried so many times putting curly braces in different places but it’s error ..
mind helping me :((
I’m just a first year college so I’m sorry for if I’m so dumb askin this to you guys :S
this is my program but, as it runs, the output has a problem… can u fix it? #include
main()
{
float at, wc;
char nc[25],cn[25];
clrscr();
printf(“\n Enter name of Costumer:”);
scanf(“%s”, &nc);
printf(“\n Enter client number: “);
scanf(“%s”, &cn);
printf(“\n Enter Account type: “);
scanf(“%f”, &at);
printf(“\nEnter Water Consumption:”);
scanf(“%f”, &wc);
if (at=1)
{
printf(“\nRESIDENTIAL”);
printf(“\n1st 10 cu.m=P185.00”);
printf(“\nExeed 10 cu,m= additional P10.00”);
if(wc< =10)
{
printf(“\nTO PAY FOR RESIDENTIAL:P185.00”);
}
else if(wc>10)
{
printf(“\nTO PAY FOR RESIDENTIAL:%f”,(wc-10)*10+185);
}
else if(at=2)
{
printf(“\nCOMMERCIAL”);
printf(“\n1st 10 cu.m=P220.00”);
printf(“\nExeed 10 cu.m=additional P15.00”);
}
if(wc< =10)
{
printf(“\nTO PAY FOR COMMERCIAL:P220.00”);
}
else if(wc>10)
{
printf(“\nTO PAY FOR COMMERCIAL:%f”,(wc-10)*15+220);
}
getch();
return 0;
}
}
Can anyone help with this program?? I will be much appreciative. I am stuck I have never done programming before and I need to write C program that will do the following:
Write a C program which prompts the user to enter some numbers and prints
Helllo… i have made a program but its giving some errors can u make it corret for me plzzz??
#include
#include
#include
main()
{
char str=”a”;
printf(“Enter the name of actor or actoress\t”);
scanf(“%s”,&a);
if(a==”shahrukh khan”)
{
printf(“Latest movies of Shahrukh Khan in 2012 and 2013\n”);
getch();
printf(“1:Jab Tak Ha Jaan\n2:Chennai Express\n3:My Name Is Khan\n4:Rab Ne Bna Di Jodi\b”);
}
else if(a==”salman khan”)
{
printf(“Latest movies of Salman Khan in 2012 and 2013\n”);
getch();
printf(“1:Ek Tha Tiger\n2:Veer\n3:Body Guard\n4:Ready\n5:Dabbang\n6:Dabbang 2\b”);
}
else if(a==”amir khan”)
{
printf(“Latest movies of Amir Khan in 2012 and 2013\n”);
getch();
printf(“1:Taare Zameen Par\n2:3 Idiots\n3:Ghajni\b”);
}
else if(a==”saif ali khan”)
{
printf(“Latest movies of Saif Ali Khan in 2012 and 2013\n”);
getch();
printf(“1:Tashan\n2:Race\n3:Agent Vinod\n4:Qurbaan\n5:Race 2\b”);
}
else if(a==”ayushman khurrana”)
{
printf(“Latest movies of Ayushman Khurrana in 2012 and 2013\n”);
getch();
printf(“1:Vicky Donor\n2:Nautanki Saala\b”);
}
else if(a==”katrina kaif”)
{
printf(“Latest movies of Katrina Kaif in 2012 and 2013\n”);
getch();
printf(“1:Ek Tha Tiger\n2:Jab Tak Ha Jaan\n3:Rajneeti\n4:Race\b”);
}
else if(a==”anushka sharma”)
{
printf(“Latest moies of Anushka Sharma in 2012 and 2013\n”);
getch();
printf(“1:Matru Ki Bijli Ka Mandola\n2:Baand Baaja Baarat\n3:Jab Tak Ha Jaan\b”);
}
else if(a==”shruti hassan”)
{
printf(“Latest movies of Shruti Hassan in 2013\n”);
getch();
printf(“1:Ramaiya Vasta Maiya\b”);
}
else if(a==”madhuri dixi”)
printf(“\nNo movie of Madhuri Dixit in released 2012 and 2013\b”);
else if(a==”sanjay dutt”)
{
printf(“Latest movies of Sanjay Dutt in 2012 and 2013\n”);
getch();
printf(“1:Police Giri\n2:Agneepath\b”);
}
getch();
}
question..how can i use if else statement to make a password?
answer please..thnks..(asap..)
I need bike and spare sale program in c
I need
They given spare rate .we select only quantity of sapre
bike rate standard, it will calculate
output
base mirror scratch box total
2500 1 0 1 2800
char vowel;
printf(“enter vowel=”);
scanf(“%c”,&vowel);
if(vowel==’a’ ||vowel==’A’ ||vowel==’i’ ||vowel==’I’ ||vowel==’O’ ||vowel==’o’ ||vowel==’e’ ||vowel==’E’ ||vowel==’u’ ||vowel==’U’)
{
printf(“alphabet is vowel=%c”,vowel);
}
else
{
printf(“alphabet is constant=%c”,vowel);
really awesome dude
hai,,im confused whether to solve it using if statement or switch statement?any help?
1. The National Earthquake Information Center has asked you to write a program implementing the following decision table to charaterize an earthquake based on it
hi. can someone help me with this .
req:
if item code is A description is CHAIR witha a prize of 200 else if itemcode is B description is TABLE with a prize of 500 else item code is C description is PC with a prize of 4000.
output display should be like this.
a.
Itemcode:_______
description :________
prize:_________
output B.
Itemcode:_______
description :________
prize:_________
quantity:______
total amount:__________
totalamount=quantity x prize
thanks for the reply 🙂 godbless
i need to print the name as well as the age using if else but name is not displaying
my code is this
#include
#include
void main()
{
char a[20];
int i;
clrscr();
printf(“enter the name\n”);
scanf(“%c”,&a);
printf(“enter the age”);
scanf(“%d”,&i);
if(i>=18)
{
printf(“eligible for vote”);
}
else
{
printf(“not eligible for vote”);
}
getch();
Write a program to calculate bill of a job work done as follows.
Use if else statement.
a) Rate of typing 3 Rs/page
b) Printing of 1st copy 5Rs/pages & later every copy 3Rs/page.
The user should enter the number of pages and print out copies he/she wants.
Can You please Give me the answer of this question…..