Selection Statement

From SwinBrain

A Selection statement allows the program to select which path to take based upon a boolean expression.

The diagram shown here is a flow chart that contains a selection statement.

This flow chart shows the use of a selection statement.

The program initially reads a name from the console, then selects what to do based upon the expression name = 'Fred'. If name = 'Fred' is true then 'What a nice name' is written to the console, otherwise the message 'Well that is a silly name' is written to the console. This can be expressed using Pascal as shown below.

//This program displays different messages
//based upon a name that is entered by the user.
program SillyName ;
var
  Name : String; //The name of the user running the program
begin
  Write('Please enter your name: ');
  ReadLn(Name);
  
  if Name = 'Fred' then
  begin
    WriteLn('What a nice name');
  end
  else
  begin
    WriteLn('Well that is a silly name');
  end;
end.

Also See

Repetition Statement

[edit]Links: Topic Brains
Return to Swin Brain