This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes flow _Control _Please rectify me if wrong.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "flow _Control _Please rectify me if wrong.." Watch "flow _Control _Please rectify me if wrong.." New topic
Author

flow _Control _Please rectify me if wrong..

deepu Bhalotia
Ranch Hand

Joined: Apr 19, 2005
Posts: 39
class JMM118
{
public static void main (String[] args)
{
int i = 0, j = 9;
while (i++ <= j--)
{
i++; //i=5, j=4
if (j < 5)
break;
}
System.out.print(i + "," + j);
}
}

I think the Answer for this Flow is 8,5. But in http://www.danchisholm.net/july21/mybook/chapter8/exam1ans.html
They have given the Answer as 9,4.

Can anybody please Verify me...

Thanks in Advance

Deepak
Stefan Willi
Ranch Hand

Joined: Mar 11, 2005
Posts: 47
The state of the variables after the last loop in the while are:
i = 8
j = 5
Now the condition i <= j = false.
But the post ++ and -- operator increment and decrement the variables after evaluating the condition (i++ <= j--). The new values are i = 9 and j = 4

Stefan
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226


The comp and val represent values used for comparison and the value of the variable at the end of the line. Flow is as follows:




Line 3 never evaluates as true, because in the iteration where j becomes less than 5, it is also < i.

Does this make sense?
deepu Bhalotia
Ranch Hand

Joined: Apr 19, 2005
Posts: 39
Thanks Timmy...

in last condition i didn't increment the i and decrement J. Thats why i was getting 8,5.
deepak khiyani
Greenhorn

Joined: May 05, 2005
Posts: 7
hi deepu,
i have modified your code a little bit.....please check this whether it helps.....

class Test
{
public static void main (String[] args)
{
int i = 0, j = 9;
while (i++ <= j--)
{
i++; //i=5, j=4
if (j < 5)
break;
System.out.println(i + "," + j);
}
//System.out.print(i + "," + j);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: flow _Control _Please rectify me if wrong..
 
Similar Threads
bitwise operator
SCJP 1.4 Chapterwise online mock exams
Cloneable Object
Is Float more Specific than Double
Dan Mock exam questions