# Convert int to a bool

• December 19th, 2013, 11:11 AM
ineedahero
Convert int to a bool
How do I make this work?

Code :

```int i = 0;   do { System.out.println("blah blah blah"); Scanner input = new Scanner(system.in); i = input.nextInt();   if (i != 0) i = 1   } while (i);```

I want the program to say "okay, this 'i' is a 1, so that means TRUE, so we'll keep going" or "nope, this 'i' is a 0, that's FALSE, so let's exit the loop".

It seems really hard?
• December 19th, 2013, 11:20 AM
KevinWorkman
Re: Convert int to a bool
A boolean expression can be expressed like this:

i==0
i<76
i!=17
etc.

These evaluate to a boolean value (true or false). You can use boolean values in things like if statements and while loops.

You've already used a boolean expression in an if statement. You say you want to use a boolean expression in a while loop. What part of that is confusing you?
• December 19th, 2013, 11:26 AM
ineedahero
Re: Convert int to a bool
I guess I'm just used to C++ where you could say 'while(integer)'

I feel like that's cleaner than always saying 'while (integer == 1)'.....
• December 19th, 2013, 11:31 AM
KevinWorkman
Re: Convert int to a bool
I guess it's a personal preference, but while(i == 1) seems fine to me. In fact I would say it's better than just while(i) because it makes it clear that 1 is the only value it should continue for.

For your code, I don't know why you're checking if(i != 0) and making it 1. That step becomes unnecessary if you just use while(i != 0) instead.