Jump to content

For Loop Without Increment Expression


Selacius

Recommended Posts

I am looking to find a way to create a for loop which automatically executes the proper increment expression. By this I mean if my x values are from -5 to 9, that it knows to go $i++, but if the values are 9 to -5, then it does $i--. I don't want ot have numerous amounts of if statements.

Link to comment
Share on other sites

I can't test this right now, but I think it should work:

for ($i = 0; $i < something something; ($x >= -5 && $x <= 9 ? $i++ : $i--))

I don't know what you mean by this though, so I just made it if/else:

By this I mean if my x values are from -5 to 9, that it knows to go $i++, but if the values are 9 to -5, then it does $i--
You mean if $x is between negative five and nine, you want to increment $i, but if $x is between nine and negative five, you want to.. aren't those the same thing? Isn't being between -5 and 9 the same as being between 9 and -5?Anyway, hopefully you can look at how that works and figure out your solution.http://www.php.net/manual/en/language.oper...parison.ternary
Link to comment
Share on other sites

I am looking to find a way to create a for loop which automatically executes the proper increment expression. By this I mean if my x values are from -5 to 9, that it knows to go $i++, but if the values are 9 to -5, then it does $i--. I don't want ot have numerous amounts of if statements.
Does $x always begin at -5 or 9, or can it begin somewhere in between?If it begins somewhere in between, what is your criteria for determining its direction of movement?Roddy
Link to comment
Share on other sites

Going from -5 to 9 would be causing the $i to increase, whereas 9 to -5 would cause it to decrease. If you just want a way to loop from -5 to 9, you could simply start the loop at -5:

for ($i = -5; $i <= 9; $i++) {//code to execute during the loop}

Or if you want to go from 9 to -5...

for ($i = 9; $i >= -5; $i--) {//code to execute during the loop}

It would help us is if you clarify and/or expound upon your question.

Link to comment
Share on other sites

Is this what you want? If you switch the values of $x and $y it will count backwards.
Although I like your iteration clause, you will find with some experimentation that your for-statement is not robust for ranges that do not traverse 0.If the asker can bear relinquishing the for-statement in favor of a foreach-statement, I would like to propose the following function. It is robust for all ranges of $x and $y no matter the direction.
<?php	function reverse_iterative_order($x, $y) {		foreach (range($x,$y) as $int) {			echo $int;		}	}?>

Roddy

Link to comment
Share on other sites

What is $x really? I can't figure it out from your posts... is it a single scalar value (a single integer or something)? That's what the solutions so far seem to assume... or is it an array of values, to which you want to be agile?If it's an array, you could do something like:

for ($i = min($x[0], $x[count($x) - 1]); $i < max($x[0], $x[count($x) - 1]); $i++) {/*$i is going to traverse the array from the left to right or right to left, depending on whether the first or last element is the greather... and it will contain the actual value in each iteration.*/}

And if you insist on keeping it from left to right... you might as well use foreach instead of for. If you have to do it without foreach, there's always functions like next() that serve a similar purpose. Basically the idea is that instead of trying to adjust the $i, you're eliminating it in favor of something where it doesn't matter what the array contains.

Link to comment
Share on other sites

I think he's jsut trying to figure out a way to get a loop to increment or decrement based on the bounds. So if value of the numbers starts negative and end up positive, it should increment. If it's the opposite, then it should decrement. I think he just wants to know how to set that up dynamically.

Link to comment
Share on other sites

I think he's jsut trying to figure out a way to get a loop to increment or decrement based on the bounds. So if value of the numbers starts negative and end up positive, it should increment. If it's the opposite, then it should decrement. I think he just wants to know how to set that up dynamically.
That is exactly what I am trying to get at. I have a way to do it now using a few if statements, but if there is a way to dynamically that would be great. the value of x is negligible. Just if I am going from small to large then it increments, and large to small it decreases.
Link to comment
Share on other sites

That is exactly what I am trying to get at. I have a way to do it now using a few if statements, but if there is a way to dynamically that would be great. the value of x is negligible. Just if I am going from small to large then it increments, and large to small it decreases.
function reverse_iterative_order($x, $y) {	  foreach (range($x,$y) as $int) {			echo $int;	  }}

Copy and paste. I made it for your pleasure and that of others. It works like a charm.Roddy

Link to comment
Share on other sites

Although I like your iteration clause, you will find with some experimentation that your for-statement is not robust for ranges that do not traverse 0.
That's not true, this works just fine, no recursion necessary.
$x = 9;$y = 5;for ($i=$x; $i != $y; ($x < $y ? $i++ : $i--)) {  echo "$i ";}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...