[College -Computer Science 165] PHP Programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jintu
    FFR Player
    • Jul 2009
    • 15

    #1

    [College -Computer Science 165] PHP Programming

    Code:
    <html>
    <!-- test01.php -->
    <body>
    
       <?php 
          $x = 2;
          for ($i = 1; $i <= 6; $i++)
          {
              if ($i % 2 == 1)
              {
                 $x++;
              }
              print $i . 'x' . $i * $x . " ";
          }
    
       ?>
    
    </body>
    </html>
    Could someone pls explain why do you get the answer below as opposed to:
    X = 2 rite,
    Then howcome the answer to the code is:
    1 x 3, 2 x6, 3 x 12, 4 x16, 5 x 25, 6 x30

    x++(shouldn't it be 3, 5, 7, 9, 11, 13)

    I am confused...

    *******
    Answer:

    1x3 2x6 3x12 4x16 5x25 6x30
  • Izzy
    Snek
    FFR Simfile Author
    • Jan 2003
    • 9195

    #2
    Re: [College -Computer Science 165] PHP Programming

    What exactly are you wanting to be the outcome be?

    If you step through each loop it would be like this

    1)

    x=3
    1x3" "

    2)

    x=3
    2x6" "

    3)

    x=4
    3x12" "

    etc....

    It seems like it works just fine assuming your syntax is correct.

    Comment

    • Jintu
      FFR Player
      • Jul 2009
      • 15

      #3
      Re: [College -Computer Science 165] PHP Programming

      Originally posted by Izzy
      What exactly are you wanting to be the outcome be?

      If you step through each loop it would be like this

      1)

      x=3
      1x3" "

      2)

      x=3
      2x6" "

      3)

      x=4
      3x12" "

      etc....

      It seems like it works just fine assuming your syntax is correct.
      I dun understand how the program works and everything is correct, i just need some explanation as how the program is working because i dun get it...

      Comment

      • Izzy
        Snek
        FFR Simfile Author
        • Jan 2003
        • 9195

        #4
        Re: [College -Computer Science 165] PHP Programming

        Ok then...


        $x = 2;

        This line is declaring some kind of variable as the integer 2.


        for ($i = 1; $i <= 6; $i++)

        This is the start of a for loop. $i = 1 inside the loop is actually declaring another variable as 1. The for loop uses this number to keep track of the looping.

        $i <= 6, this is saying that the loop will go until the variable "i" is greater or equal to 6.
        $i++, this is saying that after every loop it will increase "i" by one. Which is why the loop will eventually end because it will hit 6 from this.

        {
        if ($i % 2 == 1)
        Simple if statment here. This line of code is done every time the loop does another pass. So this if statement will get the numbers 1 through 6 passed through it. The percentage sign is a modulus symbol. Doing 1 modulus 2 will result in the answer of 1. And since 1 modulus 2 = 1 it will do the code inside of the if statement and add 1 to the x variable.
        {
        $x++;
        }
        print $i . 'x' . $i * $x . " ";
        Now this just prints out the variables and characters that it says. First it prints the "i" variable which during the first loop is going to be 1. Then it prints the character "x" then i times the first variable which at this point is 3, and then a space. So altogether this line prints 1x3 and a space. Once this line is finished it will go back to the for loop and redo all the code until i equals 6.
        }

        Comment

        • lord_carbo
          FFR Player
          • Dec 2004
          • 6222

          #5
          Re: [College -Computer Science 165] PHP Programming

          Code:
          <html>
          <!-- test01.php -->
          <body>
          
             <?php 
                $x = 2;     //sets x at 2
                for ($i = 1; $i <= 6; $i++)           //first time it does $i=1. then $i=2. and it goes on until the last time, which is i=6.
                {
                    if ($i % 2 == 1)                  //if the remainder of $i/2 = 1, then increase $x by one. In other words, when $i is an odd number, then do $x++.
                    {                                 //note that the first time through, the if statement is true, so $x=3 by the time you get to the print function
                       $x++;
                    }
                    print $i . 'x' . $i * $x . " ";   //what this does is it first prints $i. Then x. Then $i * $x (x increases whenever i is odd). Then a space. 
                }                                     // in PHP, if you're going to switch from printing a variable to printing a string, you need to put a space, a period, another space, and then the string.
                                                      //for example, if $a=2, print "I have " . $a . " kittens."; will output "I have 2 kittens."
             ?>                                       //and: print "I have" . $a . "kittens."; will output "I have2kittens."
          </body>
          </html>
          any questions? I really don't know what you find confusing about it...

          ahhh damn you izzy :P
          Last edited by lord_carbo; 07-20-2009, 09:50 PM.
          last.fm

          Comment

          • Izzy
            Snek
            FFR Simfile Author
            • Jan 2003
            • 9195

            #6
            Re: [College -Computer Science 165] PHP Programming

            Just make sure you know that a percentage symbol means modulus and not division.

            Comment

            • Jintu
              FFR Player
              • Jul 2009
              • 15

              #7
              Re: [College -Computer Science 165] PHP Programming

              Thank you so much, I got what you mean and i fully comprehend the program...

              Appreaciate it, thanks a million

              Comment

              Working...