Loops and conditions

In any form of programming or scripting you will probably want to do something more than once or maybe never depending on a condition.

Loops

Different languages have different types of loops but they all have the same effect. The run a chunk of code more than once without you having to type the code again. The two basic types are:

It is normally also possible to jump out of a loop at any time. This allows you to stop execution if something happens. This will normally be called a BREAK. One of the common problems with loops is setting one up which never ends causing the page to lock up.

Conditional statements

These basically ask a question and what they do depends on the answer. Put into English a typical one might be "If the time is before noon say good morning but if it's not say good afternoon". Many languages use the terms IF and ELSE for these conditions:

IF something
    do this                
ELSE
    do this instead
        

In both JavaScript and PHP that would look something like this:

IF (quantity<5){
    do this                
} ELSE {
    do this instead
}
        

The condition is inside brackets and the stuff which is done is inside curly brackets (braces).

submit to reddit Delicious Tweet