Hi guys , could somebody be kind enough to explain the output for this code. thank you very much
for($i=0; $i 3 ; $i=$i + 1) {
for ($j=0; $j 3; $j =$j + 1) {
for ($k=0;$k 3; $k =$k + 1){
print " I: $i; J: $j; K: $k\n" ;
}
}
}
- Follow via:
- RSS
- Email Alert
Question
1
Vote
What is the output for this PHP for loop?
3rd Aug 2011
Clarifications
does this code belong to PHP, because to print something 'echo' is used not 'print'
avinashreddy1090
3rd Aug 2011
Actually print and echo are near enough the same thing and both or either can be used.
My question would be why does anyone who needs to know the answer need to ask the question.....
My question would be why does anyone who needs to know the answer need to ask the question.....
Tony Hopkinson
3rd Aug 2011
Well I am trying to understand the output for the code
sanchezr1
3rd Aug 2011
Since I dont seem to understand the output very well
sanchezr1
3rd Aug 2011
Answers (5)
3
Votes
Well I'm left with the horrible feeling that this is homework
of some sort and if it is, you are unlikely to get an answer, because I don't think anyone who couldn't figure it out, should be allowed anywhere near any code and I know anyone who can't be bothered to, shouldn't.
Just give up programming altogether you are totally unsuited to it.
Just give up programming altogether you are totally unsuited to it.
3rd Aug 2011
0
Votes
Try it yourself??
why don't you just like try doing it yourself... and you know get the output .. ???
3rd Aug 2011
0
Votes
Slight explanation
Hi Sanchez. Although the various other posts are right, and you really should work this out yourself I'm going to offer a little help by explaining a bit. Please note that everything I'm going to say I have learnt myself from W3Schools. You really should check it out if you want to understand anything properly.
The for creates a loop until a specified goal is reached. In your example it's all the time the variable is less than 3.
The next bit declares the variable ($x=0). Hopefully you can work out what that means.
The next bit is the statement to check against before continuing the loop. As I said, in your example it checks to see if the variable is less than 3.
$x=$x+1 reads the variable X and adds one to it.
I'm going to leave you to work out what the output would be. Although just running it would be an option I suggest you try and work out the output before you run the code to ensure you have an understanding. If you get the number wrong then you know you'll have to go back and better your understanding.
If you're genuinely interested and want to learn then I'll be happy to help, and create a few exercises for you to work on. If, however, you have no interest in learning and just want us to do your work for you then please don't waste our time.
The for creates a loop until a specified goal is reached. In your example it's all the time the variable is less than 3.
The next bit declares the variable ($x=0). Hopefully you can work out what that means.
The next bit is the statement to check against before continuing the loop. As I said, in your example it checks to see if the variable is less than 3.
$x=$x+1 reads the variable X and adds one to it.
I'm going to leave you to work out what the output would be. Although just running it would be an option I suggest you try and work out the output before you run the code to ensure you have an understanding. If you get the number wrong then you know you'll have to go back and better your understanding.
If you're genuinely interested and want to learn then I'll be happy to help, and create a few exercises for you to work on. If, however, you have no interest in learning and just want us to do your work for you then please don't waste our time.
Updated - 5th Aug 2011
Replies
Thank you noshmon
sanchezr1
4th Aug 2011
1
Vote
Control structure nesting
http://writecodeonline.com/php/
http://www.php.net/manual/en/control-structures.for.php
You need to understand the nesting of the FOR structures. The code in the innermost braces runs first. This might be easier to follow if you indent the code.
http://www.php.net/manual/en/control-structures.for.php
You need to understand the nesting of the FOR structures. The code in the innermost braces runs first. This might be easier to follow if you indent the code.
4th Aug 2011
0
Votes
Some real help
I believe this is what you are looking for:
http://writecodeonline.com/php/
Just drop your code in here to test it and see the results live. Now you don't need your own webserver just to try your code.
http://writecodeonline.com/php/
Just drop your code in here to test it and see the results live. Now you don't need your own webserver just to try your code.
4th Aug 2011

































