Does (0 <0) return true?

ill has an if statement

 if (int1 < int2) {} else {} 

I want the else statement to be executed if both int1 and int2 are 0 ..

+4
source share
6 answers

Not. 0 not less than 0 .

How to use else if ?

 if (int1 < int2) { } else if (int1 == 0 && int2 == 0) { } 
+11
source

There are several answers to your question and many other answers on this page. But there is another question that has not yet been addressed: why are you asking this question in a web forum, no matter how many programmers fill it in?

You have a great tool at your disposal to automatically answer such questions, and you use it all the time: your C compiler! You just need to formulate the question correctly.

If you donโ€™t understand something, try building a small program to check some logic and see what happens. Just keep a simple pattern around (I like ~ / tmp / hello_world.c). When you have a question, just make a copy (say ~ / tmp / zerotest.c), add some function that you want to try (for example, printf("Answer: %d\n", 0 < 0); ), and run it until you understand what is happening.

I do this all the time. Even when I am working on another project, sometimes I turn out pieces of logic into a small file and play with it there until I understand. Here's what it means: find an effective way to teach yourself the language. Do not be afraid to experiment. It is very unlikely that you will hit anything together that will destroy your system. And even if this happens, Iโ€™m sure that you too can learn something from this experience.

Get in the habit of experimenting. This is a skill that you will use for the rest of your programming career.

+9
source

(0 < 0) should logically return false, as between two equal numbers, cannot be less than the other. (0 <= 0) will return true.

+6
source

Currently, your else clause will execute when both int values โ€‹โ€‹are 0.

if you want 0s to be treated the same as int1 <int2, then

 if( (int1 < int2) || (int1 == 0 && int2 ==0) ) 

but if you just want 0 <0 to go to else, it will ...

or maybe you have some code that you think should go into your else, but enter your if, but can't understand why it's so interesting, 0 or lt; 0? In this case, there will probably be something else in your code.

+3
source

No, (0 <0) returns false.

It is difficult to understand the logic that you propose in the context of the code presented.

+1
source

Of course, 0==0 and NOT 0<0 or 0>0

In compiled languages โ€‹โ€‹like C # , you need to use a comparison operator for each variable, as indicated in the answers above

 if (int1 < int2) { } else if (int1 == 0 && int2 == 0) { } 

But if your case has some interpreted language like python, you can use simple built-in comparison

 if int1<int2: print "Less" elif int1==int2==0: print "Equals to 0" 
+1
source

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


All Articles