Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

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.

like image 718
Eppiey Avatar asked Dec 02 '11 04:12

Eppiey


People also ask

What is called a operator?

In mathematics and computer programming, an operator is a character that represents a specific mathematical or logical action or process. For instance, "x" is an arithmetic operator that indicates multiplication, while "&&" is a logical operator representing the logical AND function in programming.

Is an operator a person?

An operator is a person who connects telephone calls at a telephone exchange or in a place such as an office or hotel. He dialed the operator and put in a call to Rome. An operator is a person who is employed to operate or control a machine. ...

What is the work of or operator?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool . Logical OR has left-to-right associativity.

Who is an operator of a company?

Definition: A Business Operator is the primary force driving a team working towards fulfillment of a company vision. This may be a dedicated team of contractors, but usually includes a mixture of contractors and employees.


2 Answers

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 )
like image 199
CodeRedick Avatar answered Nov 15 '22 22:11

CodeRedick


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.

like image 36
Michael Burr Avatar answered Nov 15 '22 21:11

Michael Burr