aspose file tools*
The moose likes Beginning Java and the fly likes while loop question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "while loop question" Watch "while loop question" New topic
Author

while loop question

ahmad tamimi
Greenhorn

Joined: Dec 14, 2009
Posts: 5
Hello All
I'm new in to java world, i have a question about while loop statement
why we can initialize and declare a variable inside for loop statement, but we cant make the same thing inside while loop statement
for example why we can do the following:
for(int x.....)
but we cant make:
(int x...)
so we should always define var outside the while loop?
Thanks
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688

I think that is a part of Syntax... may be


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Jhakda Velu
Ranch Hand

Joined: Feb 26, 2008
Posts: 166
Hi
The syntax of while loop is

The expression should evaluate to a true or false.

Why don't we declare the variable inside?
I quote from the Kathy Sierra book as to why we can't declare the variable used in the condition expression where you pointed
Instead of testing the variable, you'd be declaring
and initializing it, so it would always have the exact same value


Hope that helps, and if it does, mentally thank Kathy(or you can do better, buy the book )

Jhakda


If I become filthy rich, I'll sponsor research for painless dental treatment at Harvard Medical School. Thats why,I'm learning Java.I have 32 teeth, 22 are man made.
marten kay
Ranch Hand

Joined: Feb 03, 2007
Posts: 178

The regular for loop is actually similar to a while loop with a number of built in expressions,
that is

actually has three expressions separated by semicolons, and it does the 'while' checking in the background.

the while loop only has a conditional statement, it's not doing anything else like in a for loop


when in doubt put it in parenthesis and stick a dollar sign in front of it, only good can come from this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Welcome to JavaRanch

You may find some more useful information in the Java™ Tutorials.
Amarnath Pandi
Greenhorn

Joined: Dec 04, 2009
Posts: 9
If you want to initialize and declare a variable inside while loop statement itself why don't you use the for loop itself to serve the purpose?

for(int i=0;i<MAX;){
//Code
i++; //Change the value of i
}



ahmad tamimi
Greenhorn

Joined: Dec 14, 2009
Posts: 5
Thanks alot for help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Yes, you can do that, but for loops are much easier to understand if you keep to a conventional structure
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

ahmad tamimi wrote:
why we can initialize and declare a variable inside for loop statement, but we cant make the same thing inside while loop statement
for example why we can do the following:
for(int x.....)
but we cant make:
(int x...)


That is why many programmer prefer for loop over while loop to avoid cut and paste error since for loop variable's scope is finished within a loop.
s.palanivel rajan
Ranch Hand

Joined: Sep 22, 2009
Posts: 40
Hey first understand the syn taxes clearly...

syntax of FOR loop is

for(initialization;condition;iteration)

ex:for(i=0;i<=10;i++)

syntax for WHILE loop is

while(condition)

even if you use for loop as

for(i<=10;i=0;i++)

we get error message only.. because we don't have any rights to change the syn taxes..
we have to give conditions and initializations in the right places otherwise error will be thrown...

With Regards,
S.Palanivel Rajan B.E.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11411
    
  16

That is why many programmer prefer for loop over while loop

I'm not sure I agree with this. What I've generally seen is that if you know EXACTLY how many times you need to loop, you would use a for-loop structure. If the number of loops may change from run to run, a while/do-while make more sense.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
 
subject: while loop question