Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/268402-for-loop-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378304
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378308
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378319
Share on other sites

@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?

Link to comment
https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378396
Share on other sites

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;
}

Link to comment
https://forums.phpfreaks.com/topic/268402-for-loop-help/#findComment-1378420
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.