• 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 complexity

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The last line prints out 8, 6, which is ambiguous to me. Would you comment........ clarify.....?
<BLOCKQUOTE>
class ij
{
public static void main(String args[] )
{
int i =0;
int j =10;
tp: for (i= 0; i<j; i++)<br /> {<br /> <br /> System.out.println("i = " +i);<br /> <br /> i++;<br /> <br /> System.out.println("After ++i: i = " +i + ", j = " + j);<br /> <br /> if (i>j)
break tp;
j--;

System.out.println(i + " " + j);
System.out.println("--------------------------------" );
}

System.out.println("=============================");

System.out.println(i + " " + j);
}


}
</BLOCKQUOTE>
Albert
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you fix the code to look like it should? Right now this won't even go through the compiler. For instance, the for-declaration is missing parts, and there are some ++i written as +i.
This will make it easier to check the program.
/Mike
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain this?

Originally posted by Albert Gray:
The last line prints out 8, 6, which is ambiguous to me. Would you comment........ clarify.....?
<BLOCKQUOTE>
class ij
{
public static void main(String args[] )
{
int i =0;
int j =10;
tp: for (i= 0; i < j ; i++ )
//i = 0; i < j; i++
{

System.out.println("i = " +i);

i++;

System.out.println("After ++i: i = " +i + ", j = " + j);

if (i>j)
break tp;
j--;

System.out.println(i + " " + j);
System.out.println("



[This message has been edited by Vanitha Sugumaran (edited July 07, 2001).]
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
original code:
<small>
class ij
{
public static void main(String args[] )
{
int i =0;
int j =10;
tp: for (i= 0; i < j ; i++ )
//i = 0; i < j; i++
{
System.out.println("i = " +i);
i++;
System.out.println("After ++i: i = " +i + ", j = " + j);
if (i>j)
break tp;
j--;
System.out.println(i + " " + j);
System.out.println("
</small>
I'm a little new to java so please feel free to correct me.
For each turn the forloop goes it increment the i variable by 2
and decrement the j variable by 1.
for (i= 0; i < j ; i++ ) // here i increments by one. and on the line i++ i is incremented by 1. The if clause

if (i>j)
break tp;
j--;

doesn't do what it's suppose to do... i think... either way j get decremented by one each turn leaving the final block i = 8 and j = 6

------------------
Preparing for the Java 2 Certification exam
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I got the point.


tp: for (i= 0; i < j ; i++ ) <br /> {<br /> System.out.println("i = " +i);<br /> i++;<br /> System.out.println("After ++i: i = " +i + ", j = " + j);<br /> /when i = 7 and j = 7 it won't break the loop, so j is decremented to 6 and by i++ i -> 8, but the loop condition is i < j so the loop ends and the final value of i --> 8 and j --> 6
if (i>j)
break tp;
j--;
System.out.println(i + " " + j);
System.out.println("


Vanitha.

[This message has been edited by Vanitha Sugumaran (edited July 07, 2001).]
 
Snylt Master
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct!
// SnyltMaster
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic