int x = 0; for (int i = 1; i <= 100; i++) { x += 1; // Statement 1 if (i > N) continue; x += 1; // Statement 2 if (i > M) break; } As a function of N and M, what is the value of x at the end of the execution of the for-loop? Identify the correct statement in the list below. a) If N = 60 and M = 45, then x = 160. b) If N = 75 and M = 25, then x = 52. c) If N = 30 and M = 50, then x = 131 d) If N = 20 and M = 30, then x = 62.
b) If N = 75 and M = 25, then x = 52.
I think x is increment twice when i is <= N. So, when N=75.X =75*2=150 + !@#%# I am confused. Could you tell me how do you think about? How you trace it?
this same,so even notice the log output, so it is easier to understand: Statement 1>>x=1 i=1 N=75 M=25 Statement 2>>x=2 i=1 N=75 M=25 Statement 1>>x=3 i=2 N=75 M=25 Statement 2>>x=4 i=2 N=75 M=25 Statement 1>>x=5 i=3 N=75 M=25 Statement 2>>x=6 i=3 N=75 M=25 Statement 1>>x=7 i=4 N=75 M=25 Statement 2>>x=8 i=4 N=75 M=25 Statement 1>>x=9 i=5 N=75 M=25 Statement 2>>x=10 i=5 N=75 M=25 Statement 1>>x=11 i=6 N=75 M=25 Statement 2>>x=12 i=6 N=75 M=25 Statement 1>>x=13 i=7 N=75 M=25 Statement 2>>x=14 i=7 N=75 M=25 Statement 1>>x=15 i=8 N=75 M=25 Statement 2>>x=16 i=8 N=75 M=25 Statement 1>>x=17 i=9 N=75 M=25 Statement 2>>x=18 i=9 N=75 M=25 Statement 1>>x=19 i=10 N=75 M=25 Statement 2>>x=20 i=10 N=75 M=25 Statement 1>>x=21 i=11 N=75 M=25 Statement 2>>x=22 i=11 N=75 M=25 Statement 1>>x=23 i=12 N=75 M=25 Statement 2>>x=24 i=12 N=75 M=25 Statement 1>>x=25 i=13 N=75 M=25 Statement 2>>x=26 i=13 N=75 M=25 Statement 1>>x=27 i=14 N=75 M=25 Statement 2>>x=28 i=14 N=75 M=25 Statement 1>>x=29 i=15 N=75 M=25 Statement 2>>x=30 i=15 N=75 M=25 Statement 1>>x=31 i=16 N=75 M=25 Statement 2>>x=32 i=16 N=75 M=25 Statement 1>>x=33 i=17 N=75 M=25 Statement 2>>x=34 i=17 N=75 M=25 Statement 1>>x=35 i=18 N=75 M=25 Statement 2>>x=36 i=18 N=75 M=25 Statement 1>>x=37 i=19 N=75 M=25 Statement 2>>x=38 i=19 N=75 M=25 Statement 1>>x=39 i=20 N=75 M=25 Statement 2>>x=40 i=20 N=75 M=25 Statement 1>>x=41 i=21 N=75 M=25 Statement 2>>x=42 i=21 N=75 M=25 Statement 1>>x=43 i=22 N=75 M=25 Statement 2>>x=44 i=22 N=75 M=25 Statement 1>>x=45 i=23 N=75 M=25 Statement 2>>x=46 i=23 N=75 M=25 Statement 1>>x=47 i=24 N=75 M=25 Statement 2>>x=48 i=24 N=75 M=25 Statement 1>>x=49 i=25 N=75 M=25 Statement 2>>x=50 i=25 N=75 M=25 Statement 1>>x=51 i=26 N=75 M=25 Statement 2>>x=52 i=26 N=75 M=25
Well x=52 is the right one, what I did, I just copy your code and create a new java class and put your code as follow : ******************************************************************* public class Test { public static void main(String[] args) { int x = 0; for (int i = 1; i <= 100; i++) { x += 1; // Statement 1 if (i > 20) continue; x += 1; // Statement 2 if (i > 30) break; } System.out.println(x); } } ******************************************************************* Then I start changing the value of N and M each time, and see the result.
@ktobah: Or you could use something like this to produce a log output same as sofiasores' System.out.println("Statement 1>>x=" + x.toString() + " i=" + i.toString() + " N=" + N.toString() + "M=" + M.toString()); System.out.println("Statement 2>>x=" + x.toString() + " i=" + i.toString() + " N=" + N.toString() + "M=" + M.toString()); System.out.println(); Hope I got it right
Why you print out M and N, they are constants and you specify their value, so it's useless to print them out. And well here is this code N it does anything (just tricky), but M yes it's the important one because it breaks the loop : so to calculate x we have : X = 2M + 2 I add 2 because we have the condition ">"
Interactive look at my code, so you will be able to observe better processing:: #include <stdio.h> int main (void){ int N,M,i; scanf ("%d %d",&N,&M); while (N !=0 && M !=0){ int x = 0; for (i = 1; i <= 100; i++) { x += 1; // Statement 1 printf ("Statement 1>>x=%d i=%d N=%d M=%d \n",x,i,N,M); if (i > N) continue; x += 1; // Statement 2 printf ("Statement 2>>x=%d i=%d N=%d M=%d \n\n",x,i,N,M); if (i > M) break; } printf ("%d\n",x); scanf ("%d %d",&N,&M); } }
order to better observe
@ktobah Oh, I didn't notice that you're using integer literals and not constant variables. @sofiasores Yeah that one is easier.
@KimoKiko, not understand what you mean
I mean the program you just wrote makes it easy to observe this thing
Hey my formula X = 2M + 2 is wrong, sorry!
I think its right for (a) and (b) when M is less than N, Because in that case M is the important one.
So for (a) X would be (2 * 45) + 2 = 92
Yes, I didn't consider N for that it was true, right now I don't know what happen with the instruction 'continue' for that I can't figure it out.
The 'continue' statement is never executed in (a) and (b) Because the loop breaks when i > M and consequently i never gets greater than N because N is greater than M in (a) and (b). Here are the Branching Statements in Java from the documentation: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
Well I figure that it doesn't do anything to "i", but N play a role here I'm sure!
Well, "i" never gets greater than N in (a) and (b) so it doesn't do anything, but in (c) and (d) it causes the loop to jump to the beginning of the next cycle of the loop without executed the statements after the 'continue' statement, that is, "x" won't be incremented a second time when "i" is greater than N.
Join our real-time social learning platform and learn together with your friends!