Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
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:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Beginning Java
What is the output?
Varuna Seneviratna
Ranch Hand
Posts: 213
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
class OutPut { public static void main(String[] args) { OutPut o=new OutPut(); o.go(); } void go() { int y=7; for(int x=1; x<8; x++) { y++; if(x>4) { System.out.print(++y + " " ); } if(y>14) { System.out.println(" X = " + x); break; } } } }
How can the output of the above program be
13 15 X =6
From where is the
13
coming from
Varuna
Varuna Seneviratna
Steve Luke
Bartender
Posts: 4179
22
I like...
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
It comes from inside this code:
if(x>4) { System.out.print(++y + " " ); }
Why do you think it shouldn't be there?
Steve
Varuna Seneviratna
Ranch Hand
Posts: 213
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
System.out.print(++y + " " );
From the above statement the output is 15 and a space
System.out.println(" X = " + x);
From the right above it should be
X = 6;
In the final screen it should be
15 X = 6
I can't figure out from where the
13
is coming from
Varuna
Varuna Seneviratna
Varuna Seneviratna
Ranch Hand
Posts: 213
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Right I just got it the 13 is sent to the screen and X=6 is sent next in the next iteration, with 2 added to 13, that is 15.So finally 13, then in the next iteration 15 X=6 and the final screen looks 13 15 X=6
Am I right?
Varuna Seneviratna
Campbell Ritchie
Marshal
Posts: 80653
476
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Work out what happens when x == 5.
Campbell Ritchie
Marshal
Posts: 80653
476
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Originally posted by myself:
Work out what happens when x == 5.
I see you have worked that out; it might be easier to understand if you count forwards!
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
switch statement example
P315 K&B puzzle
Logical Operator
SCJP 5 Guide by K&B: Q5. Page 391 / 402
how to compare the values in arrays
More...