File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

while loop question

 
ahmad tamimi
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that is a part of Syntax... may be
 
Jhakda Velu
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marten kay
Ranch Hand
Posts: 178
Java jQuery Postgres Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

You may find some more useful information in the Java™ Tutorials.
 
Amarnath Pandi
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for help
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can do that, but for loops are much easier to understand if you keep to a conventional structure
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic