Eppiey
Eppiey

Reputation: 95

Operator '&&' cannot be applied to operand of type 'bool' and 'int'

I have an if elseif statement to check marks and grade the marks according to the condition.

int marks;
string grade;

if (marks>=80 && marks!>100)
{
  grade = "A1";
}
else if (marks>=70 && marks!>79)
{
  grade = "A2";
}

and so on.....

however, when i compile it i got

Operator '&&' cannot be applied to operand of type 'bool' and 'int'

help me fix it.thanks in advance.

Upvotes: 7

Views: 12172

Answers (4)

Sender
Sender

Reputation: 6858

int marks;
string grade;

if ((marks>=80) && !(marks > 100))
{
grade = "A1";
}
else if ((marks>=70) && !(marks > 79))
{
grade = "A2";
}

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340208

Other answers have made it known that the main problem is that !> isn't an operator.

I'd like to suggest that since you're testing whether marks lies within particular ranges that you take an additional further step to format your conditional expressions to use the following pattern:

if (80 <= marks && marks <= 100)
{
  grade = "A1";
}
else if (70 <= marks && marks <= 79)
{
  grade = "A2";
}

It's a simple and maybe subtle change, but I think it makes the range check intent much more clear.

Upvotes: 4

Bhavik Goyal
Bhavik Goyal

Reputation: 2796

you have used the wrong operator,

it must be.

int marks;
string grade;

if (marks>=80 && marks<=100)
{
  grade = "A1";
}
elseif (marks>=70 && marks<=79)
{
  grade = "A2";
}

Also you can do is

int marks;
string grade;

if (marks>=80 && !(marks>100))
{
  grade = "A1";
}
elseif (marks>=70 && !(marks>79))
{
  grade = "A2";
}

Upvotes: 4

CodeRedick
CodeRedick

Reputation: 7415

That is not a real operator:

!>

Not Greater Than would be <= (Less Than or Equal To)

EDIT: What you are trying to say could actually also be expressed using the ! operator. But it would be

!(marks > 100 )

Upvotes: 13

Related Questions