• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

while loop question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is a part of Syntax... may be
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 235
2
jQuery Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79654
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

You may find some more useful information in the Java™ Tutorials.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot for help
 
Campbell Ritchie
Marshal
Posts: 79654
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
please buy my thing and then I'll have more money:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic