6 Replies - 400 Views - Last Post: 09 February 2013 - 02:58 AM Rate Topic: -----

#1 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 127
  • Joined: 30-January 13

need help with simple for loop new to PHP and forum

Posted 09 February 2013 - 01:13 AM

I need to figure out how to make 5 rows of * and only use one for loop it has been driving me nuts all week any help is greatly appreciated because I have tried so many different ways. I am trying to create this but with only one for loop in php
*
**
***
****
*****
Is This A Good Question/Topic? 0
  • +

Replies To: need help with simple for loop new to PHP and forum

#2 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 922
  • View blog
  • Posts: 3,195
  • Joined: 19-January 10

Re: need help with simple for loop new to PHP and forum

Posted 09 February 2013 - 01:50 AM

sounds like homework, but I'll point you in the right way, with logic!

First, let's take a look at what a for loop does. A for loop is usually given three expressions, separated with a ';' symbol. The first expression is executed (basically, ran as if it were on it's own line). The second expression acts as an if statement. If it's true, the loop will continue. If it's false, the loop will not continue and the rest of the script will continue. The third expression is executed just like the first.

A simple example of this would be:
for($i=0; $i<5; $i++){
    echo "Hello!";
}



This will output "Hello!" five times.

We are also able to access the variables we modified in our for statement. An example of this would be:
for($i=10; $i<15; $i++){
    echo "I am " . $i . " years old.";
}



This will produce:

Quote

I am 10 years old.
I am 11 years old.
I am 12 years old.
I am 13 years old.
I am 14 years old.



Now you know how to use a for loop! You can do what you want by multiplying the number of stars you need to display by the current variable you are using (like in our examples, $i).

Hope this helps!

This post has been edited by creativecoding: 09 February 2013 - 01:52 AM

Was This Post Helpful? 1
  • +
  • -

#3 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 127
  • Joined: 30-January 13

Re: need help with simple for loop new to PHP and forum

Posted 09 February 2013 - 02:17 AM

It is home work I have just been pounding my head trying different codes lol this does help a lot I have not quite got it yet but I think I know more now then I did before, I have to learn a lot of how to do things from research online because I am a online student and sometimes it can get overwhelming and after almost a week I am just afraid of being late submitting so I am very thankful for the forum and your help.
Was This Post Helpful? 0
  • +
  • -

#4 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 922
  • View blog
  • Posts: 3,195
  • Joined: 19-January 10

Re: need help with simple for loop new to PHP and forum

Posted 09 February 2013 - 02:23 AM

No problem. Don't forget that PHP has it's own documentation here: http://www.php.net/manual/en/ (you can search for specific things in the upper-right search bar).

Don't forget, we're always open to helping you learn the material and such. It's the whole doing someone else's homework that's against the rules.

Here's the link to the "for loop" documentation: http://www.php.net/m...uctures.for.php

Good luck on your studies!

This post has been edited by creativecoding: 09 February 2013 - 02:23 AM

Was This Post Helpful? 1
  • +
  • -

#5 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 127
  • Joined: 30-January 13

Re: need help with simple for loop new to PHP and forum

Posted 09 February 2013 - 02:31 AM

well now I dont expect the exact answer of course but maybe I did not get it as well as I thought because here is what I am coming up with
<?php
for($i=0; $i<1; $i++){
	    echo "*";
	}


?>

which only displays one star on the first line which is good but how do I specify other lines too I did figure out if I use a <br> I can make more lines to make it look correct but the code will not be right because I am only suppose to use the 3 lines if there is anyway you can help me with out totally giving me the answer I would really appreciate it thanks.
Was This Post Helpful? 0
  • +
  • -

#6 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 922
  • View blog
  • Posts: 3,195
  • Joined: 19-January 10

Re: need help with simple for loop new to PHP and forum

Posted 09 February 2013 - 02:39 AM

alright... well you're on the right track. You want to do five iterations though (your code inside of the for loop to be ran 5 times).

Right now you're saying "$i is 0. If $i is less than 1, increase it by one and say '*'".

You want it to say "$i is 0. If $i is less than 5" - because remember we're starting at zero - ", increase it by one and say '*' times whatever $i is"

Your second expression is where this happens ($i<1 in your code). This is saying "$i is less than 1", which will only be true once when $i is 0 (as it started at). After that returns true the next expression will be ran and $i would equal 1 and would no longer be less than one, so the for loop would be over.

This post has been edited by creativecoding: 09 February 2013 - 02:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 127
  • Joined: 30-January 13

Re: need help with simple for loop new to PHP and forum

Posted 09 February 2013 - 02:58 AM

Thank you I am going to continue working on it and read the links you sent me because I am still just a little confused because I can not get it to loop I am not sure if I am not doing it right or not describing it right so I am also going to copy and paste the assignment just in case I am describing it wrong. It is hard to ask for help sometimes but not ask for the exact answer I dont expect anyone to give me the answer because then Ill never learn and what you have told me has been a big help but I am afraid I am doing something wrong still.
We talked about nested for loops. We saw an example of one:

for($lineNumber=1;$lineNumber<=5;$lineNumber++) {
        for($starCount=1;$starCount<=$lineNumber;$starCount++)         {
                echo("*");
        }
            echo("<br />");

}


OUTPUT:

*

**

***

****

*****

This can be completed without using a nested for loop, instead just using one for loop. No other loops are necessary in this structure either. (No whiles or anything other than the initial for loop). ALSO - NO IFS, OR SWITCH STATEMENTS! No functions! No arrays are necessary either. Inside of one for loop, you can complete this in a maximum of three lines of code (not counting the opening and closing PHP tags).

This post has been edited by JackOfAllTrades: 09 February 2013 - 05:18 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1