Using a for loop, print the numbers from 0 to 5

I’ve been training for a ZCE certification, and found myself getting quite annoyed with some of the more obvious questions. One of the questions in the beginning of the test was which of the pieces of code would output the numbers 0 to 5 using a for loop. I mean, come on! I present some of my more creative and twisted ways of completing said task.

By Clinton Montague

I’ve been training for a ZCE certification, and found myself getting quite annoyed with some of the more obvious questions. One of the questions in the beginning of the test was which of the pieces of code would output the numbers 0 to 5 using a for loop. I mean, come on! I present some of my more creative and twisted ways of completing said task.

The obvious solution

So if you’re in an exam, the answer would be

1
2
3
4
for ($i=0; $i<=5; $i++)
{
  echo $i;
}

but where’s the fun in that?! Below are various different ways of doing the same thing. They all work.
They’re all correct. They’re just not what you’d do.

Using an array

1
2
3
4
for ($i=range(0,5),$k=$j=count($i);$j>=0;$j--)
{
	echo $i[$k-$j];
}

Outputting chars

1
2
3
4
5
6
7
for ($i=48; $i < 54; $i++)
{
  echo chr($i);
}
 
// or even better!
for ($i=48; $i<=53; print_r(chr($i++)));

Using a string to count

1
2
3
4
for ($i=0, $s=''; $i<6; $s.=' ', $i++)
{
	echo strlen($s);
}

Using bitwise operators

1
2
3
4
for ($i=1;$i<64; $i=$i<<1)
{
	echo log($i,2);
}

The know it all method

1
2
3
4
for (;;)
{
	echo 'the numbers 0 to 5'; break;
}

more?

What other ways can you think of to output the numbers 0 to 5 using a for loop?

Tags: , , ,

One Comment

  1. poison added these wise words on March 23, 2009 | Permalink

    dude … loops are so yesterday …
    <?php
    function counter($to=0, $cur=0) {
    if ($cur

Me on the intertubes

It wouldn't be right to have a web site, be a nerd, and not take this opportunity for a bit of shameless pluggery.

If you like all these weird and magic internet page things, you should try Coaster Nerd, Oxford Bloggers or, if you'd really like some unexplainable sourcery, is it the weekend yet.

Also you should follow me on Twitter ;)

Where to find me

I can be found fairly regularly at Oxford Geek Nights, OxTuttle and other general nerding out activities in and around Oxford.

I am also often found in my natural habitat which is anywhere that you can find an adreneline machine. I frequent English theme parks more often than is healthy, and take trips abroad to sample foreign rides.

From the aviary

Was having a decidedly crap day of development. Just got much better. It's nice when things go your way. #win 1 day ago

Subscribe

Slightlymore Posts RSS feed

Comedy disclaimer

These posts were written at the time of writing and may not still be accurate or reflect my opinions any more. In fact, they may not even be correct or representative at the time of writing. You see, when I write, I just write. I don't do that thing which they always told you to do at school and plan what you want to write first. Because of this, there will almost certainly be a load of typos, grammar errors and incorrect facts and references. If this makes you feel queasy, I apologise that you had to get to the bottom of the page to read this, and hope that some day, you'll learn to forgive me.

What's this site?

Slightlymore started off all too formally for my liking. In the depths of the archives, you'll find tutorials. However if you read the more recent posts, you'll find them a lot more playful and about fun and interesting stuff.

Annoyingly difficult to use tag cloud