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:
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
Forum:
Beginning Java
What is the output?
Varuna Seneviratna
Ranch Hand
Posts: 213
posted 15 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 15 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 15 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 15 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: 79177
377
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Work out what happens when x == 5.
Campbell Ritchie
Marshal
Posts: 79177
377
posted 15 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!
Don't get me started about those stupid
light bulbs
.
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...