else termination
Title
Question
How else is working even if i terminate it by semicolon?
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter the value of a,b and c \\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
{
printf("a is greater than b and c \\n");
}
else if(b>a && b>c)
{
printf("b is greater than a and c\\n");
}
else;
{
printf("c is greater than a and b\\n");
}
return 0;
}
int main()
{
int a,b,c;
printf("Enter the value of a,b and c \\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
{
printf("a is greater than b and c \\n");
}
else if(b>a && b>c)
{
printf("b is greater than a and c\\n");
}
else;
{
printf("c is greater than a and b\\n");
}
return 0;
}
C-and-Cpp If-And-Else-If-statement 05-06 min 30-40 sec
Answers:
in the above code what we have code after else; statement it will execute always because after else we put ; so the if-else part is completed
so irrespective of a,b,c values we will get "c is greater than and b"
A semicolon after the else indicates an empty else clause.
if (something) {
} else;
is the same as:
if(something) {
} else {
}
for the above mentioned program the actual procedure is:
if(condition1)
statement1
else;
statement2
While executing this type of a question, if condition1 is false control automatically transfer to statement2....it will not touch else keyword.....
you can visualize this with turbo C Interpreter (Press f7 Key continuously)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter a value:");
sacnf("%d",&a);
printf("Enter b value:");
scanf("%d",&b);
printf("Enter c value:");
scanf("%d",&c);
if(a<b&&a<c)
printf("%d is greater",a);
if(b<c)
printf("%d is greater",b);
else
printf("%d is greater",c);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter a value:");
sacnf("%d",&a);
printf("Enter b value:");
scanf("%d",&b);
printf("Enter c value:");
scanf("%d",&c);
if(a<b&&a<c)
printf("%d is greater",a);
if(b<c)
printf("%d is greater",b);
else
printf("%d is greater",c);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter a value:");
sacnf("%d",&a);
printf("Enter b value:");
scanf("%d",&b);
printf("Enter c value:");
scanf("%d",&c);
if(a<b&&a<c)
printf("%d is greater",a);
if(b<c)
printf("%d is greater",b);
else
printf("%d is greater",c);
getch();
}
Login to add comment