Jump to content

loops and arrays assignment.


Hillary

Recommended Posts

this is my homework assignment #5 about loops and arrays part 1 of 3:

 

Create a program that uses a while loop to count by 5 from zero to 100 displaying each count as it goes. The loop should output 0, 5, 10, 15.....95, 100:

 

 

 

<head>

<title></title>

</head>

<body>

<?php

 

// initialize sentury variable

 

 

$sent = 0;

 

while($sent <= 100)

{

echo"Count#" $sent "\<br />";

 

$sent++;

 

}

 

?>

 

</body>

</html>

 

 

the error i get is that i am missing a "," or ";" on line 17? which is the echo.... i'm not sure if this is right? well, obviously its not but can you let me know what i'm doing wrong? i'm sure theres a lot!! THANKS ;D

 

 

 

 

 

Link to comment
Share on other sites

Not entirely sure what you were trying to achieve with the extra double quotes and backslash, but this "works":

 

<html>

  <head>
     <title></title>
  </head>
  <body>
<?php

// initialize sentury variable


$sent = 0;

while($sent <= 100) 
{
  echo"Count# $sent ";   

  $sent++;

}

?>

 

I'll leave you to figure out how to make it increase by 5 each time.

 

Just FYI: You can join strings (for echoing etc) by using the concatenation operator(.). For example:

 

<?php
$foo = 'foo';
$bar = 'bar';
echo $foo.$bar; //foobar
?>

 

The backslash is the escape chacacter. This means the character following will be interpreted as a literal, and not have it's special function. For example, you may wish to echo a single double quote, and might try:

 

<?php
echo """;
?>

 

This will, however, throw an error. This is because the string being echoed will be terminated by the 2nd double quote, meaning the third is just floating about, confusing PHP. To get around this, you could do this:

 

<?php
echo "\""
?>

 

This means the second double quote is treated as a literal, and so does not terminate the string being echoed.

 

In this instance, it would probably have been easier to use single quotes to surround the string being echoed:

 

<?php
echo '"';//equivilant to the above, but a bit easier on the eye.
?>

 

Finally, you may ask what the difference between using single and double quotes is. Well, when you use double quotes, variables contained within that string are evaluated. That is, their value is echoed to the screen. For example:

 

<?php
$foo = 'bar';
echo "String 1: $foo";//echos string 1: bar
echo '<br />';
echo 'String 2:$foo';//echo string 2: $foo
?>

 

Some of this issues may have been where the confusion in your code lay. Hopefully the crash course might have helped.

Link to comment
Share on other sites

i figured that one out!!! $sent+=5; so simple!!!! THANK YOU!!

 

 

ASSIGNMENT #2 for me is this: Create a program with a while loop that picks random values between 1 and 100. Each pick is added into a running total. When the total exceeds 1000, the loop exits. Display each number as it is picked. Here's the pseudo code:

 

initialize total to 0

while total is less than 1000

    pick a random number

    display the random number

    add the random number to the total

Display the total

 

i put

$sent1 = rand(1,100);

print $sent1;

$sent2 = rand(1,100);

print $sent2;

 

 

but in preview that displays a number like 8657, 7073..... random numbers but they couldn't possibly add up to that much PLUS i never added them together yet??!? i don't understand... i'll keep working any help is appreciated, and i'm NEVER asking people to do my work for me... i just don't know anything...well, i feel like i know but just not on my own... i need people to make me think of it... THANKS in advance

 

 

Link to comment
Share on other sites

When you issue a "print" or "echo" it just displays the value with nothing after the value, so two echo (or print) statements will show the values after one another

<?php
echo "a";
echo "b";
?>

 

will display "ab". If you want to separate the values you have to do it yourself:

<?php
echo "a<br>";
echo "b";
?>

will display

a
b

 

Ken

Link to comment
Share on other sites

just follow the pseudocode

<?php
$total = 0;               # initialize total to 0                                            
while ($total < 1000)     # while total is less than 1000                                    
{                                                                                            
    $x = rand (1,100);    # pick a random number                                             
    echo $x, '<br/>';     # display the random number                                        
    $total += $x;         # add the random number to the total                               
}                                                              
echo "<br/>TOTAL: $total";# Display the total                                                
?>

Link to comment
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.