DomenicF Posted September 15, 2012 Share Posted September 15, 2012 Hey guys, I really need help here. For the past hour I've been trying to figure out the for loop, but I honestly just can't. I was wondering if we could learn by example. Here's what I want to for loop to do, perse: $avg = 150; for every 5 added to $avg (150) multiply only what is added by 3 I was wondering what the code is for this, and if this specific problem is more complicated than others. I like learning by example, so I hope someone could help me out. Quote Link to comment https://forums.phpfreaks.com/topic/268402-for-loop-help/ Share on other sites More sharing options...
Christian F. Posted September 15, 2012 Share Posted September 15, 2012 This is not only about a for loop, but the modulus operator. It also sounds suspiciously like a homework/interview question. Quote Link to comment https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378127 Share on other sites More sharing options...
thara Posted September 15, 2012 Share Posted September 15, 2012 follow http://php.net/manual/en/language.operators.arithmetic.php Quote Link to comment https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378141 Share on other sites More sharing options...
hakimserwa Posted September 15, 2012 Share Posted September 15, 2012 show us some effort do something get stuck and lets start from where you are stuck. Quote Link to comment https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378149 Share on other sites More sharing options...
DomenicF Posted September 16, 2012 Author Share Posted September 16, 2012 This is not only about a for loop, but the modulus operator. It also sounds suspiciously like a homework/interview question. It's not for a homework/interview question, if it was I wouldn't be here, I'd either be talking to my professor or just not take the job. show us some effort do something get stuck and lets start from where you are stuck. I already did, but didn't want to post it as it just causes an infinite loop. I really have NO idea what I'm doing, which is why I posted here. for ($i = 5; $i <= 150; 5++) { echo $i; } As I said, I have NO IDEA what I'm doing. I'm a noob. Trying to learn. Quote Link to comment https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378304 Share on other sites More sharing options...
Christian F. Posted September 16, 2012 Share Posted September 16, 2012 In that case I recommend that you start with reading about operators in PHP, once you're done with that you can move on to Control Structures. Best is probably to just start at the beginning, with the PHP tutorial and go from there. PS: If you haven't already, turn on all error reporting in your php.ini. This will help you immensely in programming. Quote Link to comment https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378308 Share on other sites More sharing options...
hakimserwa Posted September 16, 2012 Share Posted September 16, 2012 that is going to increment $i with one every time $i is less or = to 150. is that what you want? Quote Link to comment https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378312 Share on other sites More sharing options...
Barand Posted September 16, 2012 Share Posted September 16, 2012 that is going to increment $i with one every time $i is less or = to 150. is that what you want? It won't increment $i at all, hence the infinite loop. @Domenic, change the "5++" (which does nothing) to "$i += 5". That will increment $i by 5 each time. Quote Link to comment https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378319 Share on other sites More sharing options...
DomenicF Posted September 16, 2012 Author Share Posted September 16, 2012 @Domenic, change the "5++" (which does nothing) to "$i += 5". That will increment $i by 5 each time. Thanks a lot for your help. So let me try and put this into English, and correct me if I am wrong: The first part of the for loop is the starting point, or integer. The second part of the for loop is the ending point, or integer. The third part tells what to do with the first part until it gets to the second part. Does this sound right? Quote Link to comment https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378396 Share on other sites More sharing options...
Barand Posted September 16, 2012 Share Posted September 16, 2012 To be more precise The first part is executed before the loop starts The loop continues while the second expression is "true" The third part is executed at the end of each loop. So it equivalent to $i = 5; while ($i <= 150) { echo $i; $i += 5; } Quote Link to comment https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378420 Share on other sites More sharing options...
DomenicF Posted September 16, 2012 Author Share Posted September 16, 2012 Thank you very much! I understand the basic concept now! EDIT: When I get a chance I'll try and make the code I originally wanted and post it here. Quote Link to comment https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378428 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.