teach-ict.com logo

THE education site for computer science and ICT

4. IF ..... THEN ..... ELSE ..... END

This construct allows you to place a condition after the IF section, the statements inserted after the THEN part will be executed if the condition is true. On the othe hand the statements after the ELSE section are carried out should the condition be false. The END statement marks the end of the conditional block.

Example:

IF (x > 10) THEN
  	Print (x)	

  ELSE
   x = x + 1
END
  

This pseudo-code is not in any particular computer language. Each computer language will insist on slightly different way of laying it out. Perhaps curly brackets { } are used to mark out blocks.

For instance the code below is written in the PHP scripting language that is used extensively on web servers.

 

if (x>10) {
   print (x);
} else {
    x .= 1;
}   

Note the use of curly brackets and it does not even use the THEN key word as it is implied in the first bracket pair. Using semi-colon ; to mark the end of a line and the curious .= combination as shorthand for adding something to the same variable. Each language has its own syntax that you must learn before you can begin to code with it.

You do not have to use an ELSE statement every time. If no code needs to run if the condition is false, then no need to use it.

 

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: php conditional statements