9 Replies - 581 Views - Last Post: 21 February 2010 - 12:48 PM Rate Topic: -----

#1 pyro1772  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-February 10

while statement

Posted 19 February 2010 - 12:32 PM

I'm in an online course and the instructor isn't as helpful as this forum is.The book also is very confusing on certain things as well. I don't know if I'm looking to hard are what but I'm having trouble with these 4 problems and can't figure it out. Is there anyway someone can give me an example to go by on this? Here are the questions:

Write a while statement for each of the following conditions:
answer is equal to yes
done is set to false
moreRecords is set to true
sex is male and age is great than or equal to 21

I' don't know why but I'm really having trouble here and need to figure this out before I start trying the programming assignment. Can anyone help?

Is This A Good Question/Topic? 0
  • +

Replies To: while statement

#2 trosity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 19-February 10

Re: while statement

Posted 19 February 2010 - 12:49 PM

I think you need to supply some code as to what your asking to do the while loops on. but a general java while loop is

While ( conditon ) {
do this ;
}

so example ,

While ( answer == yes) {
System.out.println("Answer is yes");
}

but i think you'd be better off with an if statement so it won't constantly be looping and crash your programme.

if ( answer == yes) {
System.out.println (" Answer is yes " );
}

This post has been edited by trosity: 19 February 2010 - 12:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: while statement

Posted 19 February 2010 - 01:09 PM

when checking if a String is equal to another String you should use the String.equals(String) method.

if(string1.equals(string2)){
// do some cool crap
}

or something like this

if(string1.equals("yes")){
// do some cool crap
}

When you want to check if two things are true u use the AND operator '&&' like this.
if((string1.equals(string2) && (string1.equals("yes")){
// do some cool crap
}

This checks if string1 == string2 AND string1 == yes

The other operator you can use is OR which looks like this ||

For example..
if((string1.equals(string2) || (string1.equals("yes")){
// do some cool crap
}

This checks if string1 == string2 OR string1 == yes
so string1 could either equal string2 or it could equal yes.


These are called conditions and they can be used in many places in the java language such as if statements (as shown above) in while-loops and for-loops
Was This Post Helpful? 0
  • +
  • -

#4 pyro1772  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-February 10

Re: while statement

Posted 19 February 2010 - 01:19 PM

The book wants it in a while statement instead of the if.
So would that mean that the question, sex is male and age is greater than or equal to 21, would look like this?

While ( sex == male)&& (>=21) {
System.out.println("Answer is true");
}


Is this correct?
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: while statement

Posted 19 February 2010 - 01:27 PM

While ((sex == male)&& (age >=21)) {

Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: while statement

Posted 19 February 2010 - 01:28 PM

No...

That depends on what type of variable sex is...whether it's an integer or a String. Also, you cannot compare something to nothing as you have done.

If sex is a String variable, you need:

// Notice first the lowercase "w"

while ( sex.equals("male"))&& (needSomethingHereLikeAge>=21) {
System.out.println("Answer is true");
}



As you can see, after the && sign, you need something to compare like an integer such as age.
Was This Post Helpful? 0
  • +
  • -

#7 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: while statement

Posted 19 February 2010 - 01:29 PM

this is correct
while (( sex.equals("male"))&& (age>=21)) {
System.out.println("Answer is true");
}
This will print answer is true 'forever'

notice the added variable age

it all needs to be inside these ( condition ) or ((condition1) &&/|| (condition2))

or ((condition1) &&/|| (condition2) &&/|| (condition3) &&/|| (condition4)) ect.

Also u need to use .equals for string variables.
Read Dogstopper post :)

This post has been edited by gl3thr0: 19 February 2010 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 pyro1772  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-February 10

Re: while statement

Posted 20 February 2010 - 09:16 AM

Thanks guys. I think I understand this now. Don't know what I'd do without this site.
Was This Post Helpful? 0
  • +
  • -

#9 pyro1772  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 16-February 10

Re: while statement

Posted 21 February 2010 - 12:37 PM

Hey. BTW guys would this one be any different? Done is set to false:
while(done==false){
system.out.println("Answer is false.")
or would it be
while(!done){
system.out.println("Answer is false."

Which if either of those would be correct?
Was This Post Helpful? 0
  • +
  • -

#10 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: while statement

Posted 21 February 2010 - 12:48 PM

while(done == false)

and
while(!done)
Have the same effect. I would prefer
while(!done)
Just because it reads better in this case. "While not done"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1