Welcome! Log In Create A New Profile

Advanced

Section 5 - Programming Loops

Posted by kiroant-77337840 
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
Section 5 - Programming Loops
July 16, 2013 07:35PM
Completed this section. Can anyone please give me a better (or practical) explanation of when breaking out of loops will be required (executed)? I know how, just don't know why (sorry for the stupid question).
Re: Section 5 - Programming Loops
July 16, 2013 08:54PM
Hey Kiroat

Here is a really short explanation:

"One of the key features of any programming language is to automatically generate content. If there are certain tasks you want to be repeated during the script execution, you don't have to write them many times"
from: inobscuro.com/tutorials/loops-in-php-26

Guys you can correct me if i'm wrong (I only scanned threw it) but the link is just a explanation on a "simple avatar gallery" U can go threw it if U want , but this section did cover what u need to know.

I finished this Chapter and I do understand it but, I went back to the other sections allot.

Regards

M
Re: Section 5 - Programming Loops
July 17, 2013 09:34AM
In loop u have a condition that has to be met, U can include an if statement in your loop to test whether the condition has been met (if so), set your loop counter to stop executing then break out of that loop.

In short, when your condition has been met, that's the right time to break out, to stop the whole execution of the loop.

Hope that makes sense..!
Re: Section 5 - Programming Loops
July 17, 2013 02:58PM
Thanks Murray and Kayc, both explanations were helpful. I also spoke to another person who actively works in PHP and he gave me an example from one of his projects where it was necessary, and that also helped a lot.
avatar
Mac
Re: Section 5 - Programming Loops
July 17, 2013 03:30PM
Only used in for, foreach, while, do, and switch

Language: PHP
$number = 10; //start with 10 for ($count=1; $count<=10; $count++) { //do 10 loops if ($number>50) { break; } echo $number . "<br>"; $number += 10; //add 10 to the number }


The output would be 10, 20, 30, 40, 50.

Of course, you may think it is easier to just do just have 5 loops from the start to get to 50

Language: PHP
$number = 10; //start with 10 for ($count=1; $count<=5; $count++) { //do 10 loops echo $number . "<br>"; $number += 10; //add 10 to the number }

but if you have a different number, say 13, and want to keep it under 50, you will need to work out what to add to $count <= (i.e. 3). So now you are doing "calculations" that the code can do for you.

Perhaps a stupid example....
Re: Section 5 - Programming Loops
July 17, 2013 03:39PM
I've reached my first wall in PHP, > Loops
I suck a math and so the moment i looked at the Loop code format...confused smiley
Help??
Explain as if i were a Lloyd (Jim Carrey-Dumb and dumber) ...
avatar
Mac
Re: Section 5 - Programming Loops
July 17, 2013 05:19PM
Give an example, else I will be here all night explaining smiling smiley
Re: Section 5 - Programming Loops
July 17, 2013 07:14PM
Hi

In section 6 you use loops aswell.

Best Wishes

M
Re: Section 5 - Programming Loops
July 17, 2013 08:51PM
Done with the for loops and will be starting with the while loops.
Re: Section 5 - Programming Loops
July 17, 2013 10:13PM
Hi Prof Mac,
Sorry for only replying now,
I've been using multiple sources for learning php,I got distracted/confused by them,
Had a problem understanding what was going on in general.

But eventually realized how simple it was when explained by the homeandlearn tutorial.
Other tutorials don't break it down as well as homeandlearn, they dumb it down for vegetables like mesmile

Thanks.
avatar
Mac
Re: Section 5 - Programming Loops
July 18, 2013 08:18AM
Yip, H&L does the best job to get you to the point where you start with the real stuff - the portfolio!
Re: Section 5 - Programming Loops
July 18, 2013 06:46PM
I'm done with this section, all understood!

As a summary :
For loops are generally for when you know how many times the loop is going to run
Do While loops will execute the loop at least once and then check to see if a condition is met
While loops will only execute if a condition is met, so the whole loop will be skipped if it's not met

grinning smiley
Re: Section 5 - Programming Loops
July 19, 2013 02:07PM
Thanks Mac smiling smiley
Re: Section 5 - Programming Loops
July 19, 2013 05:10PM
Loving H&L, this stuff is awesome. Tried to redo the checkbox exercise from the forms section using loops to generate the inputs and content but got really confused when saving the checks. I'll have to have another stab it at a later stage when I feel more comfortable.
Re: Section 5 - Programming Loops
July 22, 2013 03:52PM
Re: Section 5 - Programming Loops
July 23, 2013 05:35PM
seems like all programming languages are common when it come to loops
done with this section..on to the next one

@ Rushil-77923553{
breeze intead mate thumbs up
}
avatar Re: Section 5 - Programming Loops
July 23, 2013 07:42PM
Finished with section 5 and understood. I will do some more examples though as I haven't done programming before and although I can read and understand the code, I am not completely ready to write it myself without referring back.
Re: Section 5 - Programming Loops
July 24, 2013 01:36PM
Finished this section. Will have to come back and revise. Practice practice practice.
avatar Re: Section 5 - Programming Loops
July 26, 2013 05:25PM
Beware of infinite loops they can be frustrating!!!!!!!grinning smiley
Re: Section 5 - Programming Loops
July 28, 2013 09:28PM
Section complete.
Re: Section 5 - Programming Loops
July 30, 2013 08:25PM
section complete....
Re: Section 5 - Programming Loops
July 31, 2013 07:31PM
I have read and understood this section.

It's pretty standard.

S
Re: Section 5 - Programming Loops
July 31, 2013 07:40PM
Finished this section, moving on to arrays...
Re: Section 5 - Programming Loops
August 01, 2013 11:24PM
completed section and understand it easy enough.
Re: Section 5 - Programming Loops
August 06, 2013 05:14PM
Completed this section.

@ Sipho, I got interested at your notice and I thought to check

An infinite loop (also known as an endless loop or unproductive loop) is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over. In older operating systems with cooperative multitasking, infinite loops normally caused the entire system to become unresponsive. With the now-prevalent preemptive multitasking model, infinite loops usually cause the program to consume all available processor time, but can usually be terminated by the user. Busy-wait loops are also sometimes called "infinite loops". One possible cause of a computer "freezing" is an infinite loop; others include deadlock and access violations.

source> wikipedia.org/wiki/Infinite_loop

thumbs up
Re: Section 5 - Programming Loops
August 09, 2013 04:56PM
Completed section on loops.

Videos that I found helpful:
For Loops
While loops
Do While Loops

Thank you gouwem for the extra info on infinite loops. I found this forum with examples of when one would actually use infinite loops.
Re: Section 5 - Programming Loops
August 12, 2013 11:04AM
Section 5 - Programming Loops : Completed , no problems .
Re: Section 5 - Programming Loops
August 13, 2013 11:35AM
Completed section 5.
Re: Section 5 - Programming Loops
August 14, 2013 04:37PM
Section completed!

A comment I'd like to add is that my experience in the Web Design short course I did last semester really helped me so far in this course. Specifically talking about HTML, CSS and Javascript relating to the PHP structure. Just some good advice to pass onto future students! spinning smiley sticking its tongue out

Warren
Re: Section 5 - Programming Loops
August 14, 2013 08:14PM
Talking of previous experience, my previous experience of javascript and C++ also helped me through the section.

Switch statement can also be used:
<?php
$favcolor="red";
switch ($favcolor)
{
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, or green!";
}
?>
Sorry, only registered users may post in this forum.

Click here to login