Please help with the question, many thanks!
Author |
Message |
snowball
|
Posted: Sat Jun 14, 2008 8:10 pm Post subject: Please help with the question, many thanks! |
|
|
What is the output of the following Java code fragment?
PrintStream output = System.out;
for(int i = 13; i > 0; i=i%5 ){
if(i++ == 4){
output.printf("first=%d, second=%d\n", i-5, i*i );
} else {
output.printf("first=%d, second=%d\n", i-2, 7*i );
}
} |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Sat Jun 14, 2008 8:15 pm Post subject: RE:Please help with the question, many thanks! |
|
|
Have you considered simply compiling and executing the program?
Though it looks like an exercise in traversing the code on your own... |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
snowball
|
Posted: Sat Jun 14, 2008 8:37 pm Post subject: RE:Please help with the question, many thanks! |
|
|
It's a question in the exam. I know the answer is
first=12, second=98
first=0, second=25.
But I don't understand why. |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Sat Jun 14, 2008 9:08 pm Post subject: RE:Please help with the question, many thanks! |
|
|
Because when you run the code on a computer, that's what it would output. You just have to do the same in your head (or on paper). There's only one variable to keep track of -- i, so it should be simple.
You need to be able to read code to write code.
for(int i = 13; i > 0; i=i%5 ){
i = 13
if(i++ == 4){
false. i is now 14
output.printf("first=%d, second=%d\n", i-2, 7*i );
prints the first line. 14-2 = 12. 7*14=98
for(int i = 13; i > 0; i=i%5 ){
i=i%5; 14%5 = 4
if(i++ == 4){
true. i is now 4+1 = 5
... |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
snowball
|
Posted: Sat Jun 14, 2008 9:27 pm Post subject: RE:Please help with the question, many thanks! |
|
|
Thans a lot. |
|
|
|
|
![](images/spacer.gif) |
snowball
|
Posted: Sat Jun 14, 2008 9:28 pm Post subject: RE:Please help with the question, many thanks! |
|
|
another question. Thanks!
What is the output of the following Java code fragment?
int k, y = 123;
for(k = 0; k > 0; k++){
y = y++ / 10;
}
System.out.println(k + " " + y); |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
|
|
|
![](images/spacer.gif) |
snowball
|
Posted: Sat Jun 14, 2008 9:37 pm Post subject: RE:Please help with the question, many thanks! |
|
|
The answer is:
0 123
Why? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Reality Check
|
Posted: Sat Jun 14, 2008 9:42 pm Post subject: Re: Please help with the question, many thanks! |
|
|
It is not an infinite loop tony. It says k and y = 123. It reaches the for statement and makes k = 0 and the loop condition is IF k > 0 and not k >= 0. Therefore it never goes into the for loop, equates k to 0 while y retains it's 123 value. This is pretty simply stuff though, what grade is this? |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Sat Jun 14, 2008 9:46 pm Post subject: RE:Please help with the question, many thanks! |
|
|
Oh yeah. Ops. I skipped over k = 0 part. (busy with calculus... midterms).
0 123 is correct then. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
Zeroth
|
Posted: Sun Jun 15, 2008 5:00 pm Post subject: Re: Please help with the question, many thanks! |
|
|
How about we stop doing people's homework for them? I dunno, personally, it doesn't help anyone to say what a piece of code, does, unless you take them step by step with it. Just my view. |
|
|
|
|
![](images/spacer.gif) |
syntax_error
![](http://compsci.ca/v3/uploads/user_avatars/196798963948cf16794b6e5.jpg)
|
Posted: Sun Jun 15, 2008 5:06 pm Post subject: Re: Please help with the question, many thanks! |
|
|
Zeroth @ Sun Jun 15, 2008 5:00 pm wrote: How about we stop doing people's homework for them? I dunno, personally, it doesn't help anyone to say what a piece of code, does, unless you take them step by step with it. Just my view.
look
\
|
\/
Tony wrote:
Because when you run the code on a computer, that's what it would output. You just have to do the same in your head (or on paper). There's only one variable to keep track of -- i, so it should be simple.
You need to be able to read code to write code.
for(int i = 13; i > 0; i=i%5 ){
i = 13
if(i++ == 4){
false. i is now 14
output.printf("first=%d, second=%d\n", i-2, 7*i );
prints the first line. 14-2 = 12. 7*14=98
for(int i = 13; i > 0; i=i%5 ){
i=i%5; 14%5 = 4
if(i++ == 4){
true. i is now 4+1 = 5
...
|
|
|
|
|
![](images/spacer.gif) |
Dragan
|
Posted: Sun Jun 15, 2008 5:59 pm Post subject: RE:Please help with the question, many thanks! |
|
|
@snowball
You need firist to learn if and for than you can start with reading and writing code. If you don't know how to use if and for we can't help you. |
|
|
|
|
![](images/spacer.gif) |
|
|