English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have no idea why the "if" statement is valud, thus allowing for the infinite loop to execute. Someone please help. I am very frustrated.

public class test
{

public static void main (String args[]) {

int a,b,c,d;
a = 10;
b = 20;

c = 10;
d = 15;

if(((a & b) < (c & d)) | ((a & b) > (c & d)) )
{
for(;;)
System.out.println("asdf");
}
}
}

2006-06-09 23:04:14 · 4 answers · asked by needhelp 1 in Computers & Internet Programming & Design

4 answers

IMHO it may be that you are using the boolean logic in the wrong way.

My way of thinking is that you want a test to see if 'a' and 'b' are less than 'c' and 'd' OR 'a' and 'b' are greater than 'c' and 'd'.

If this is the way you want it, you need to write the if statement like this
if (((a < c) && (a < d) && (b < c) && (b < d)) || ((a > c) && (a > d) && (b > c) && (b > d))) { ... }

Whatever the way you want to do it, the for loop will cause the infinite loop to happen.

for(;;)
System.out.println("asdf")

has no way of stopping, once the condition in the if statement is true.

2006-06-14 15:44:52 · answer #1 · answered by Mark aka jack573 7 · 0 0

simply convert the values into binary and do as needed

a=10=001 000
b=20=010 000

c=10=001 000
d=15=001 101

for a & b = 000 000 (Logical AND operation)
c & d = 001 000

therefore 000 000 < 001 000
(a&b) < (c&d)

first condition satisfies and it is ORed with next instruction so it doesn't matter condition satisfies or not

therefore if condition satisfies and goes to the loop

2006-06-09 23:14:23 · answer #2 · answered by $$-SilentSakky-$$ 4 · 0 0

Terminate for loop you means instead of for(;;) write for(i=0;i Good luck

2006-06-09 23:14:51 · answer #3 · answered by gaurav 2 · 0 0

im having problems with Java too.. i can help you but im kinda lazy to think right now... sowee...

... and GoodLuck...

2006-06-09 23:16:33 · answer #4 · answered by baby_taddy 1 · 0 0

fedest.com, questions and answers