• 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

Dans mock test on control flow

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers

I thought the o/p is 9,3. But the answer is 8,4.Can anyone explain it clearly and i am totally confused about the increment operators .
Thanks in advance
kalpana
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kalpana,
Let us go throught the values of i and j as we understand the code.
class JMM119 {
public static void main (String[] args) {
int i = 0, j = 9;
do {
if (j < 4) {break;} else if (j-- < 7) {continue;}
i++;
} while (i++ < 7);
System.out.print(i + "," + j);
}}
now as we enter do while for the firdt time
i =0 and j =9
so j = 9 < 4 is true go on this is the tricky part j-- < 7 is like check if j=9 < 7 and and then decrement j so 9 < 7 is true and then the value of j becomes 8. The continue statement takes the flow to while clause
now here again first i = 0 < 7 is checked it is true and then i is incremented to i = 1;
As j -- and i ++ are postincrement operators.
Similarly the rest of the code is
2 time
8<4
8<7
j = 7
1 < 7
i = 2

3 time
7<4
7< 7 - false
still j = 6
i = 3
3 < 7
i = 4
6<4
6< 7 - false
still j = 5
i = 5
5 < 7
i = 6
5<4
5< 7 - false
still j = 4
i = 7
7 < 7 [B]fasle[B] and so ends the loop but ofcourse incrementing i so
i = 8
and then the print statement is executed printing 8 and 4

4<4
4< 7 - false
still j = 3
i = 7
7 < 7 - [B]fasle[B]
i = 8
now the while loop ends
 
Sricharan Modali
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kalpana
Ignore the last block in the previous code it creeped in while i was copy pasting
 
kalpana appala
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sricharan

Thanks for the reply. What I understood from the code is i is incremented to 2 in the first loop , to 4 in the second loop etc. May be i am seeing the code in a different way . i is incrementing twice when j--< 7 is false and while checking the condition i++ < 7. so the value of i is 6 till the third loop. Am i missing something here.
please explain it clearly.
Thanks kalpana
 
Sricharan Modali
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kalpana
I am sorry to confuse u. I did it wrong last time.
This time i will be correct

first time inside the code
i =0 and j = 9
so
9<4 = false
(j-- <7 ) is 9<7 = false
now j becomes 8
and i becomes 1
comes to the while statement and 1 < 7 true and then increments i to 2
(as u said yes)
now similarly
8<4 = false
8<7 = fasle
j = 7
i =3
3<7 true
i =4
3 time
7 < 4 = false
7 < 7 = false
j = 6
i = 5
5 < 7 true
i = 6
4 time
6 < 4 = false
6 < 7 = true now j becomes 5 and enter the else if condition and continue with while
so now (i++ < 7) is 6 < 7? yes now i becomes 7
and the loop continues
5 time
5 < 4 = false
5 < 7 = true now j becomes 4 [B]and again it enters elseif condition and continues with while
so now (i++ < 7) is 7 < 7? [B]false

However as i ++ is postincrement i will become 8
before the print statement is encountered.
printing 8 and 4
Hope i didnt make any mistakes this time
 
kalpana appala
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thanks for the reply. I got it now. I forgot to check the condition i++<7 when i reaches 7.

kalpana
 
kalpana appala
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thanks for the reply. I got it now. I forgot to check the condition i++<7 when i reaches 7.

kalpana
 
reply
    Bookmark Topic Watch Topic
  • New Topic