pls, does anyone the output to this questonCode:what is the output of the following fragment int i-4; do{ printf("i intermediate value is %d\n", i); i--; }while (i>0); printf("final value of i is %d", i);
pls, does anyone the output to this questonCode:what is the output of the following fragment int i-4; do{ printf("i intermediate value is %d\n", i); i--; }while (i>0); printf("final value of i is %d", i);
Last edited by mikel360; 05-23-2011 at 10:05 AM.
my answer is 1,2,3,4
pls, am i write
First you need to figure out what the line that you have written as "int i-4" is supposed to be, because that's not a valid line. Then you need to step through each line, one by one, with a table of variables (or in this case, variable) and evaluate each line as it happens, one at a time.
> my answer is 1,2,3,4
Mmm, you get that by counting backwards?
It would take all of 30 seconds just to paste it into your code editor, compile and run it to get the answer.
Or as tabstop says, run it "in your head" to see what it would do.
This too is an important skill, as being able to envisage what code would do is a good step to being able to write it.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
The only explanation I can think of for questions like these is that it's hard to compile code when you're sitting in an exam.
that was how the question is in the exam paper.
i dont undestand what you mean by "that's not a valid line"
In that case the correct answer is: the following code fragment produces no output as it results in a compile error. But it is more likely that you simply forgot the exact text, or you have a typographical error.Originally Posted by mikel360
It results in a compile error because the operator - at that point is invalid. It probably should be:Originally Posted by mikel360
The point is this: in the exam, and for practice, you should trace over the code yourself, mentally and/or on paper. But to check your answers, instead of asking us, write a program with this code fragment, compile and run it and observe the output for yourself.Code:int i = 4;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
when i compiled and run it, it was an error message showing though am suppose to use my head
thanks for your feedback
what about if the code starts with
[code]
int i=4;
[\code]
what will be the output
Why don't you actually use your brain and think about it!? If you can't figure this out, programming probably isn't for you.
It would be:Originally Posted by mikel360
Of course, I might not be telling the truthCode:i intermediate value is 1 i intermediate value is 2 i intermediate value is 3 i intermediate value is 4 final value of i is 5
To find out the truth, compile and run the code yourself.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)