Welcome! Log In Create A New Profile

Advanced

PHP Five - Programming Loops

Posted by 77914228_Tman 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
PHP Five - Programming Loops
February 19, 2013 12:44PM
when I started this course never thought I'd enjoy it. but now I'm enjoying every moment of it. I just like how the For Loops works...spinning smiley sticking its tongue out
Re: PHP Five - Programming Loops
February 22, 2013 05:43AM
I too am enjoying this course... even making more time to do it which is unheard of smile
Done
avatar Re: PHP Five - Programming Loops
February 24, 2013 09:36PM
I use loops like 20 times a day but never in this way but I did enjoy this part more than the previous section.

I'm starting to thank my lucky stars that we do not have to do a written exam on all this cause I'll never remember everything eye rolling smiley

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: PHP Five - Programming Loops
February 28, 2013 02:42PM
loops are very fun indeed.. you can make 100's of line of code work in 10.. loops in loops in loops.. works lekker with functions as well !

off to the next section smileys with beer

Student number : 7803-010-2
Email and Gtalk for support : wilcovandeijl@gmail.com
Re: PHP Five - Programming Loops
March 01, 2013 11:39AM
Section5 was Quick & Fun,
had no problems
avatar Re: PHP Five - Programming Loops
March 03, 2013 01:20AM
Gotta love the for loops, so much fun. I just found the following different results for counter++ and counter+1 a bit puzzling.
The result of the following code is a list of numbers 1-10 as indicated in the tut:

Language: PHP
$counter = 0; $start = 1;   for($start; $start<11; $start++) { $counter = $counter+1; print $counter."</br>"; }

BUT if I replace
Language: PHP
$counter = $counter+1
with
Language: PHP
$counter = $counter++

I get a different result, just a list that contains ten 0's
I understood that ++ is the same as +1?
I think I'm missing something, or does someone else also get the same result?
Re: PHP Five - Programming Loops
March 03, 2013 08:19PM
Section 5 - Programming Loops - Done!

Enjoyed the various repetition & selection options.

Now moving on to arrays ...smile
Re: PHP Five - Programming Loops
March 05, 2013 06:11PM
For loop and any other programming loops are fun work with. They are a convenient way of iterating instructions in your program. The only thing that can be frustrating is messing up the syntax, which subsequently will change the behaviour of your program.
avatar
Mac
Re: PHP Five - Programming Loops
March 06, 2013 08:44AM
77929284 Wrote:
-------------------------------------------------------
> Gotta love the for loops, so much fun. I just
> found the following different results for
> counter++ and counter+1 a bit puzzling.
> The result of the following code is a list of
> numbers 1-10 as indicated in the tut:
>
>
Language: PHP
> $counter = 0; > $start = 1; > > for($start; $start<11; $start++) { > $counter = $counter+1; > print $counter."</br>"; > } >
>
> BUT if I replace
>
Language: PHP
> $counter = $counter+1
with
Language: PHP
> $counter = $counter++ >
>
> I get a different result, just a list that
> contains ten 0's
> I understood that ++ is the same as +1?
> I think I'm missing something, or does someone
> else also get the same result?

counter is set as zero. If you use ++, you are telling it the whole time "the counter is 0, increase it by one. The answer is always 1. The for loop just tells it to repeat this answer 10 times. therefore you have tell it to add one i.e. counter +1.
Re: PHP Five - Programming Loops
March 06, 2013 11:36AM
I read in a book once that the origins of c++ language name actually came from C programming. hence when improvements were made the next programming language after C was to be called "C++", refering to the idea that it was C language + an extra technology to it, hence best explained as C++ is like saying C=C+1......I agree with mac and 77932455 on th reason for your differences. What i would strongly encourage you to do is to take great caution on ur syntax, where you put your curly brackets and where you declare the value variables of what program you want i.e. within or outside the curly brackets because that is what will loop and be changing, and especially the formula you are using, because it may vary. eg printing out a list of prime numbers, even numbers, odd numbers, fibonacci sequence and even real life sequence like turning degrees celcius in farenheits. make sure your formula is in par with your variables and your looping statements

..&ru..
Re: PHP Five - Programming Loops
March 06, 2013 04:25PM
Done and dusted... Interesting how the different loops work.... Moving on to arrays
avatar Re: PHP Five - Programming Loops
March 13, 2013 01:00AM
For and While loops are just great. More ways to add functionality is becoming clearer.
Re: PHP Five - Programming Loops
March 17, 2013 04:25PM
Very nice thing to do and very easy to understand. This woks the same like java
Re: PHP Five - Programming Loops
March 18, 2013 09:37AM
Something interesting is what I learned with C++ and what you can use with PHP is to use physical Boolean values(True / 1 or False / 0) that you declare in a Variable outside and use inside the condition of the loop. I am not referring to the current condition of loops as there default function is true and false inside the round brackets already like while() for() and so on. ALSO you can check if a input was performed before continuing with the rest of the code or code block (statements) making you program more redundant for small errors in your code.

Student Number: 78042879 (Part Time Courses)
Re: PHP Five - Programming Loops
March 19, 2013 11:21AM
Quick little section but very interesting. Never thought that I would say it but PHP is a lot of fun smiling smiley, the loops make life so much easier and I can see they will be used quite a bit! On to the next section thumbs up
Re: PHP Five - Programming Loops
March 20, 2013 10:27AM
done. standard c/c++ syntax
avatar Re: PHP Five - Programming Loops
March 20, 2013 11:30PM
Loops are amazing. They can save lots of coding time and manual work.
I prefer to use the syntax $counter++
Maybe you could call me lazy, but it makes my code look a lot neater.
The structure of a while loop is definitely easier to understand.
As long as the condition is true the loop will continue running.
Re: PHP Five - Programming Loops
March 21, 2013 01:37PM
TIP!

As what I saw in my C++ course:
Sorry If I mixed C++ with PHP allot, but I need to learn both at the same time.

ONLY use FOR Loops when you know how much the "counter" will be. It is considered bad programming practice to use it for anything else.

Rather use WHILE and DO WHILE loops allot especially if you do not know what the "counter" is going to be or how many times the code must loop before a false statement is reach.

DID YOU KNOW << LOL
You can use the FOR LOOP for mathematical formulas ? Like to work out the square-root of something ?

THIS IS JUST AN EXAMPLE:

Language: PHP
$tolerance = 0.0001;   int i = 0; for (x = a; x*x - a > $tolerance && i < 20; x = (x + a / x) /2 ) { echo x; i++; }

Point is. Just shows you how flexible you can make code to adjust to your requirements.

Student Number: 78042879 (Part Time Courses)
Re: PHP Five - Programming Loops
March 21, 2013 03:32PM
Loopedy loop about loops - I really like loops smiling smiley
avatar Re: PHP Five - Programming Loops
March 22, 2013 10:51PM
This was an short and sweet exercise.thumbs up
I did enjoy it, and learned some interesting new things.
Re: PHP Five - Programming Loops
March 26, 2013 01:30PM
Language: PHP
<?php // Testing Variables $counter = 0; $start = 1;   for($start; $start<11; $start++) { $counter++; print ($counter); } ?>



Hi 77929284
The ++ operator doesn't seem to work with the equal sign must be put in as $counter++ and not $counter=$counter++ the latter confuses it completely so it does nothing.
Re: PHP Five - Programming Loops
March 26, 2013 01:40PM
AngelaWoodend-77672860 Wrote:
-------------------------------------------------------
>
Language: PHP
> <?php > // Testing Variables > $counter = 0; > $start = 1; > > for($start; $start<11; $start++) > { > $counter++; > print ($counter); > } > ?> >
>
>
>
> Hi 77929284
> The ++ operator doesn't seem to work with the
> equal sign must be put in as $counter++ and not
> $counter=$counter++ the latter confuses it
> completely so it does nothing.


In this way it will work with the "=" assignment operator.

Remember "=" is not a equal sign in programming it is an assignment operator. You are "assigning" a value to a variable from left to right.

One thing that I learned.: For every function / statement / calculation... Make sure the value is "catch" in a variable(placed inside the memory / RAM) or else it is just thrown away.

Language: PHP
<?php // Testing Variables $counter = 0; $start = 1;   for($start; $start<11; $start++) { $counter1 = $counter++; print ($counter1); } ?>

ALSO Remember. When assigning a variable inside a loop. The variable is destroyed when the loops ends / exit. You also can't access the value of a variable that was declared inside a loop, but you can pass the value of a variable out of a loop, but the loop needs to execute the statement that "returns" the value.

Student Number: 78042879 (Part Time Courses)
Re: PHP Five - Programming Loops
March 31, 2013 11:42AM
Done
Re: PHP Five - Programming Loops
April 01, 2013 10:23PM
Today I tried 'while()' loop within the 'for()' loop and within 'if()' loop. It worked out nicely. I was displaying the table of information about students and courses.
Sorry, only registered users may post in this forum.

Click here to login