The operator '&&' cannot be applied to an operand of type 'bool' and 'int'

I have an if elseif statement for checking labels and evaluating labels according to a condition.

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

etc.

however, when I compile it, I got

The && operator cannot be applied to operands of type "bool" and "int"

Help me fix this. Thanks in advance.

+6
source share
4 answers

Other answers made it clear that the main problem is that !> Is not an operator.

I would suggest that since you are checking to see if marks within certain ranges, you take an extra extra step to format conditional expressions in order to use the following pattern:

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

This is a simple and maybe subtle change, but I think that makes the definition of range checking a lot clearer.

+3
source

This is not a real statement:

 !> 

No more than <= (less than or equal to)

EDIT: What you are trying to say can also be expressed with! operator. But that would be

 !(marks > 100 ) 
+12
source

you used the wrong operator

he must be.

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

Also you can do it

 int marks; string grade; if (marks>=80 && !(marks>100)) { grade = "A1"; } elseif (marks>=70 && !(marks>79)) { grade = "A2"; } 
+3
source
 int marks; string grade; if ((marks>=80) && !(marks > 100)) { grade = "A1"; } else if ((marks>=70) && !(marks > 79)) { grade = "A2"; } 
+1
source

Source: https://habr.com/ru/post/902898/


All Articles