Tuesday, May 15, 2007

Selection structure

The Selection structure is primarily achieved by using the if() condition (flowchart attached below).


The if() condition allows branching of program codes based on a condition. The general structure looks like this:
if(condition)
{
// Write statements to do if condition is true
}
else
{
// Write statements to do if condition is false
}
We can also nest (put) if() conditions within another if() conditions to form complex logic for our programs. Do take care of the curly brackets, and always use tabs to indent the statements properly to aid debugging.

Things to note about if() conditions:
  • an if() can have one and only one else or not at all
  • a else must come with a if()
  • if() conditions are independent of each other

No comments: