• 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

for loop

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cann't we declare & initialize two variables in a for loop.
for(int i=3,int j=5;i+j<20;i++,j++)
//do something
this code is giving me error while compiling. Can somebody explains this???
Thanx in advanced.
--Shallender
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can initialize multiple variables in the init part of a for loop, but the corrct syntax is like this
for( int i=0, j=0; i<10; i++, j++) {}
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means that we have to declare the varibale j before the starting of for loop otherwise it will give a complie error.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there
the code
for(int i=3,int j=5;i+j<20;i++,j++)
//do something
will not compile alright !!!
for(int i=3,j=5;i+j<20;i++,j++)
//do something
will compile !!
But wonder why
for(int i=3,char j=5;i<20;i++)
//do something
results in a compilation error ! ?
Praveen Zala
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need not declare 'j' before the beginning of for loop. The syntax

int i=0, j=0

will take care the declaration part for both i and j.
 
reply
    Bookmark Topic Watch Topic
  • New Topic