Why is the last condition not executing?

69 views Asked by At

The first two conditions for 100<a<200 and 100<b<200 are executing perfectly but the condition for both to be between 100 and 200 is not working any idea why? My code:

#include<stdio.h>
int main()
{
    int a,b;
    printf("Enter the first integer:");
    scanf("%d",&a);
    printf("Enter the second integer:");
    scanf("%d",&b);
    if (100<=a&&a<=200)
    {printf("The integer %d lies between 100 and 200",a);}
    else if (100<=b&&b<=200)
    {printf("The integer %d lies between 100 and 200",b);}
    else if((100<=a&&a<=200)&&(100<=b&&b<=200))
    {printf("Both of the integers lie between 100 and 200");}
    else
    {printf("The integers does not lie between 100 and 200");}
    return 0;
}

Output: enter image description here

4

There are 4 answers

3
Vlad from Moscow On

If for example the condition in the if statement

if (100<=a&&a<=200) 

evaluates to logical true then other if-else statements will not be executed including this if statement

else if((100<=a&&a<=200)&&(100<=b&&b<=200))

You need to start your if-else statement from this statement

if((100<=a&&a<=200)&&(100<=b&&b<=200))
{printf("Both of the integers lie between 100 and 200");}
else if (100<=a&&a<=200)
{printf("The integer %d lies between 100 and 200",a);}
else if (100<=b&&b<=200)
{printf("The integer %d lies between 100 and 200",b);}
else
{printf("The integers does not lie between 100 and 200");}
2
T.Aoukar On

I suggest you read the basics of if-else all over again. When using if-else, the conditions are checked sequentially, once one of them is matched, that block is executed and the rest is ignored.

In your case, when 100<a<200 and 100<b<200, it matches the first block therefore it doesn't check other conditions.

Easiest fix is to start with the if((100<=a&&a<=200)&&(100<=b&&b<=200)) then put the other else if conditions.

0
mcilloni On

An else block is only considered if its associated if condition evaluates to 0.

In your case, if both a and b are between 100 and 200, the first if (100<=a&&a<=200) will be taken, and its else block is then skipped. It might help you see why this is the case if you write the if-else blocks without omitting the brackets:

if (100<=a && a<=200){
    printf("The integer %d lies between 100 and 200",a);
} else {
    if (100<=b && b<=200) {
        printf("The integer %d lies between 100 and 200",b);
    } else {
        if((100<=a && a<=200) && (100<=b && b<=200)) {
            printf("Both of the integers lie between 100 and 200");
        } else {
            printf("The integers does not lie between 100 and 200");
        }
    }
}
1
Saifeddine Ben Salem On

Because you need to learn more about if and else statement in c What you have wrote meant if a is between [100...200] ,if not check if b is between [100..200] if not check the both conditions that you just neglected?

i advice you to use boolean like this so it be obvious enough if you don't see it

#include<stdio.h>
int main()
{
int a,b;
printf("Enter the first integer:");
scanf("%d",&a);
printf("Enter the second integer:");
scanf("%d",&b);
int x,y;
x=100<=a&&a<=200; // returns 1 if it's between 100 and 200
y=100<=b&&b<=200; // returns 1 if it's between 100 and 200
if ((x==1) && (y==1))
printf("Both of the integers lie between 100 and 200");
else {
  if(x==1)
     printf("The integer %d lies between 100 and 200",a);
  else if (y==1) 
     printf("The integer %d lies between 100 and 200",b);
  else 
    printf("The integers does not lie between 100 and 200");}
}

Note : if you have one statement in each condition or a loop it's usless to open brackets