• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BOOLEAN CONDITION

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BOOLEAN CONDITION
I can not understand the behaviour of the code. I first set variable S1 to false. Then I put a boolean condition, which equates to false=true, and still the while loop is executed and I get an output false and Green. I think while loop should not have been executed
Can anyone kindly guide me where I am wrong.
public class Test{
public static void main(String IBM[]){
String P="GREENHORN";
boolean S1;
int q=0;
int w=0;
S1=q<w;>
System.out.println(S1);
while (S1=true){ System.out.println(P.substring(0,5));
break;
}}
}
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
First please explain what is this line about;
S1=qSystem.out.println(S1);
It won't compile even without the obvious typo q. println has void return type.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Zeeshan.In the code,you are NOT COMPARING the values,but assigning the value 'true' to boolean variable S1 as the condition for 'while'.This will always be true.The assignment operator returns a value,which is the value of the left hand operand.That comes out to be 'true' in this case.Hence the output u get.
If u want to compare,us the == operator.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use while(S1==true){
}
that will compare the value of S1
Harish
 
reply
    Bookmark Topic Watch Topic
  • New Topic