-7

I write code in c and

I am beginner in the case I think if(a==1,b<=8) work like the control first check the value of a if and it is true then check bcondition if both conditions are true then come into body but I think I did not know the accurate syntax to write that can anyone give me correct syntax.

here is my code:

#include <stdio.h>
#include <cono.h>
#include <iostream.h>
#include <stdlib.h>
void main(void)
{
    int a, b;
    printf("enter a");
    scanf("%d",&a);
    printf("enter b");
    scanf("%d",&b);
    if(a==1, b<=8)
    {
        printf("you");
        exit(0);
    }
    if(a==2, 5<b<=10)
    {
        printf("you");
    else
        printf("me");
    }
    getch();
}

Expected result example:

If a is equal to 2 so control check if b is less than 10 and greater than 5 if it is so, control run if's true condition if not so control run else statement

shoaib sabir
  • 605
  • 2
  • 9
  • 23

4 Answers4

3

if(a==1,b<=8) should be if(a==1 && b<=8)

if(a==2,5<b<=10) should be if(a==2 && 5<b && b<=10)

Please read some beginner book or some tutorial before asking such questions please.

K. Kirsz
  • 1,384
  • 10
  • 11
1

Logical operators in C for or/and are || and && respectively, so you would for example rewrite

if(a==1,b<=8) as if(a==1 && b<=8) or if(a==1 || b<=8).

Adam
  • 2,422
  • 18
  • 29
0

hi try this hope its work

#include<stdio.h>
#include<cono.h>
#include<iostream.h>
#include<stdlib.h>
void main(void)
{
 int a,b;
 printf("enter a");
 scanf("%d",&a);
 printf("enter b");
 scanf("%d",&b);
 if(a==1 && b<=8)
 {
  printf("you");
  exit(0);
  } else if(a==2 && 5<b && b<=10)
 {
  printf("you");
 }
else{
  printf("me");
  }
  getch();
  }
0

What you need is &&

The comma operator compiles, but it does not do what you want.

if(a==1,b==2)
   printf("hello\");

will print "hello" if b is 2, regardless of the value of a.

The construct:

if(3<a<8)

does not work either. It compiles, but it does not do what you want. What it does it does is to compare 3 < a and calculate the result, which is true or false, that is, 1 or 0. After that, the result is compared to 8, and since both 0 and 1 is less than 8, this will always do what's inside the if statement.

klutt
  • 30,332
  • 17
  • 55
  • 95