• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

flow control

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,
please take a look at the following code with these 3 situation?
1. if the code is the way it is, it prints 1, 2, 3, oops.
2.If i changed y to b in line 6, it prints 1,2, oops.
3. If i changed y to b in line 5 and take out the return keyword in line 14, it prints 1,2, oops, oops
Can you explain to me how to approach these 3 situations?
thanks in advance, yuki
---------------
public class Ace{
public static void main(String argv[]){
int b =1;
for(int y =0; y<4; y++){<br /> try {<br /> if(y > 2) { // line 6
throw new Exception();
}
System.out.println(b);
b++;
}
catch (Exception c){
System.out.println("oops");
return; //line 14
}
}
}
}

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Yuki Cho:
hi guys,
please take a look at the following code with these 3 situation?
1. if the code is the way it is, it prints 1, 2, 3, oops.
2.If i changed y to b in line 6, it prints 1,2, oops.
3. If i changed y to b in line 6 and take out the return keyword in line 14, it prints 1,2, oops, oops
Can you explain to me how to approach these 3 situations?
thanks in advance, yuki
---------------
public class Ace{
public static void main(String argv[]){
int b =1;
for(int y =0; y<4; y++){<br /> try {<br /> if(y > 2) { // line 6
throw new Exception();
}
System.out.println(b);
b++;
}
catch (Exception c){
System.out.println("oops");
return; //line 14
}
}
}
}


Hello Yuki,
I am not very good w/ words. So let's walk together through the code execution for all 3 cases.
Case #1
1 - b = 1
2 - y = 0
3 - in 'try' y is not greater than 2 -> no throw
4 - System.out.println (b) ----> 1
5 - b++ -------> b = 2
6 - no throw in 3 therefore catch loop is not executed, continue w/ for-loop
7 - y = 1
8 - in 'try' y is not greater than 2 -> no throw
9 - System.out.println (b) ----> 2
10 - b++ -------> b = 3
11 - no throw in 8 therefore catch loop is not executed, continue w/ for-loop
12 - y = 2
13 - in 'try' y is not greater than 2 -> no throw
14 -System.out.println (b) ----> 3
15 - b++ -------> b = 4
16 - no throw in 13 therefore catch loop is not executed, continue w/ for-loop
17 - y = 3
18 - in 'try' y is now greater than 2 ----> throw (i.e. catch block will execute next)
19 - in catch block System.out.println ("oops")
20 - return and get out of main(). That is how you get your answer of 1, 2, 3, oops
Case #2
1 - b = 1
2 - y = 0
3 - in 'try' b is not greater than 2 -> no throw
4 - System.out.println (b) ----> 1
5 - b++ -------> b = 2
6 - no throw in 3 therefore catch loop is not executed, continue w/ for-loop
7 - y = 1
8 - in 'try' b is not greater than 2 -> no throw
9 - System.out.println (b) ----> 2
10 - b++ -------> b = 3
11 - no throw in 8 therefore catch loop is not executed, continue w/ for-loop
12 - y = 2
13 - in 'try' b is now greater than 2 ----> throw (i.e. catch block will execute next)
14 - in catch block System.out.println ("oops")
15 - return and get out of main(). That is how you get your answer of 1, 2, oops
Case #3
1-13 Refer to the first 13 steps in case #2
14 - in catch block System.out.println ("oops"), continue w/ for-loop (return was removed)
15 - y = 3
16 - in 'try' b is now greater than 2 ----> throw (i.e. catch block will execute next)
17 - in catch block System.out.println ("oops"), continue w/ for-loop (return was removed)
18 - y = 4 - for-loop terminates. And that is how you get your answer if 1, 2, oops, oops
Hope that helps answer your question
- Lam -

[This message has been edited by Lam Thai (edited May 13, 2001).]
 
Yuki Cho
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Lam,
you did an excellent job of walking me through this. your explanation is very clear and thorough. thanks soooooo much!!!
-yuki
 
reply
    Bookmark Topic Watch Topic
  • New Topic