Jump to content

[PASCAL] Conditional Branching


CouldnoT
 Share

Recommended Posts

Conditional Branching

 

One of the most important features of intelligent devices (like computers, programmable devices) is that they can take actions in different conditions.

This can be done by using conditional branching. For example, some cars lock the door when the speed reaches or exceeds 40 K/h. The condition in this case will be:

 

If speed is >= 40 and doors are unlocked, then lock door.

 

The If condition

 

The If condition statement in the Pascal language is very easy and clear. In the example below,

we want to decide whether to turn on the air-conditioner or turn it off, according to the entered room temperature: 

 

Air-Conditioner program:

var
 Temp: Single;
begin
 Write('Please enter Temperature of this room :');
 Readln(Temp);
 if Temp > 22 then
 Writeln('Please turn on air-condition')
 else
 Writeln('Please turn off air-condition');

 Write('Press enter key to close');
 Readln;
end.

 

We have introduced the if then else statement, and in this example: if the temperature is greater than 22, then display the first sentence::

Please turn on air-conditioner

else, if the condition is not met (less than or equal to 22) , then display this line:

Please turn off air-conditioner

 

We can write multiple conditions like:

 

var
 Temp: Single;
begin
 Write('Please enter Temperature of this room :');
 Readln(Temp);
 if Temp > 22 then
 Writeln('Please turn on air-conditioner')
 else
 if Temp < 18 then
 Writeln('Please turn off air-conditioner')
 else
 Writeln('Do nothing');

 

You can test the above example with different temperature values to see the results.

 

 

 

 

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.