1

Can't we use Boolean logical operators (such as &,|,!,^ etc) in java flow controls ( for loop,while loop etc) ???

I want to print all even numbers between 1 and 100.So I used below two source codes.

import java.util.*;
class Example{
    public static void main(String args[]){
        int i=1;
        while(i<100){
            if(i%2==0)
                System.out.print(i+" ");
                i++;
            }
    }
}

This code is compiled and prints all even numbers between 1 and 100.

import java.util.*;
class Example{
    public static void main(String args[]){
        int i=1;
        while(i<100 & i%2==0){
                System.out.print(i+" ");
                i++;
            }
    }
}

This code is compiled without any error.but doesn't give any print.

Why is that ? Can't we use Boolean logical operators within a while loop ?

Virangaa
  • 119
  • 7
  • The second code says "stop as soon as `i` is 100 _or_ `i` is odd". `i` is initially odd, so it stops immediately, without looping even once. – Sweeper Feb 01 '20 at 14:25
  • & , && operator return true only when both expressions are true , in your case i initialised to 1 so i<100 is true but i%2 ==0 is false , so while loop will not run. so the second approach logic is incorrect. – saravana Feb 01 '20 at 14:28
  • So can't I get the same output by using "&" sir ?? – Virangaa Feb 01 '20 at 14:33

3 Answers3

1

Remember that the while loop condition is the condition to continue the loop, which means that when the condition is false, the loop stops.

If you negate the condition in the second code:

!(i<100 & i%2==0)

Using De Morgan's Law, This is equivalent to:

i>=100 | i%2!=0

Or in words:

i is greater than or equal to 100 OR i is odd.

This is the stopping condition of the while loop. Well, i is initially 1, which is odd, so the loop stops without even executing one iteration.

In other words, you can't rewrite the first code as the second code. They are not equivalent. What goes in the if condition goes in the if condition. You can't "merge" it into the while condition, because they are for different purposes.

I also recommend && for the logical AND, as it only evaluates the right operand when necessary. For more info, see What is the difference between & and && in Java?

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

The error is in this line:

while(i<100 & i%2==0){

& should be replaced with &&:

while(i<100 && i%2==0){
Luca Murra
  • 1,848
  • 14
  • 24
0

U have tearn the logical operators... and it is a logical operator it can be true only if 2 operators are true( x>5 and y<7) will be true if only x>5 is true and y<7 is true that is give us the condition is true but if one of them is not verified the condition is false.... but if we have or operator the condition will be true only if one of them is true or both........ so let's explain ur code. You have condition (i<100 and i%2==0). 0%2 ==0 so it is true and of course i<100 so while returns true(both true at the same time) but when i==1 here we have a problem 1%2!=0 so i<100 is true but i%2==0 is false so we get **true && false ** the result is false . (true && true==true) (false && true==false) (false and false==false) (true ||true==true) (true || false==true) (false||false==false)