Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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
 
robl
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Praveen Zala
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • 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
 
Vijay T
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic