• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

confusion in code

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what will be o/p of this prg
int i = 0;
2 for (; i = 0; ++i) {
3 System.out.println(i);
4 }
5 System.exit(0);
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi anita dhar

You can always try and compile the prog and see the output
how ever there won't be any output
in fact it won't compile
cause in for loop
for (; i = 0; ++i)

i=0 is not correct it should actually result in a boolean value
which this doesn't
so the prog won't compile

Charandeep
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is prog? :roll:
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anita,
For loop should contain:
1.Declaration and initialization of variables
2.The boolean expression (conditional test)
3.The iteration expression

That is, it should like:
for (/*Initialization*/ ; /*Condition*/ ; /* Iteration */) {
/* loop body */
}

In your code, initialization is already done, therefore, inside for loop parenthesis, first part will remain empty, second part contains only boolean expression and third part can contain increment/decrement expression.

Important to note is that
* There can be more than one initialization each separated by comma.
* There can be more than one iteration expression each separated by comma.
* But there cannot be more than one boolean expression.
Example:
for (int i = 0,j = 0; (i<10) && (j<10); i++, j++) {
System.out.println("i is " + i + "j is " +j);
}

Hope it clears your doubt.

Regards,
Seema
 
You didn't tell me he was so big. Unlike this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic