Quantcast
Channel: TriumVir News » Programming
Viewing all articles
Browse latest Browse all 7

Conditional statements in Pascal

$
0
0

In Pascal commands are organized into groups called modules (units). If you are using a particular feature of the module, you need to tell the compiler, using the keyword “uses” relating to crt module, for example, uses crt;, this provision will allow typing a command cleaning screen.

Conditional statements in Pascal

Often it is necessary to check if a certain condition is met, for example, if x> 5, and the execution of certain operations. We use the so-called conditional statements. In Pascal one of these statements is “if” that we translate as if or supposing. The syntax (construction) of IF statement in Pascal is:
IF condition THEN statement; When the condition is true, the statement will be fulfilled.
In the program IF statement can take the form of:
if x>1 THEN writein (‘the variable x is greater than 1’);
In the above notation (when the condition is actually true) is executed first instruction located immediately after the THEN, and then the next lines of program code.
When you want the block of instructions to be made, you have to close it with the buckles BEGIN and END.

program asterisk;
var
x: integer;
begin
writein (‘enter a number from 0 to 10, if you give five I will paint a star’);
readln (x);
if x = 5 then writeln (‘*’);
writein (‘ending the program’);
end.

If you enter a value of 5, the asterisk will be displayed and each time printed the inscription: ending the program. If after the condition is met there has to be next group of instructions performed (we are talking about the so-called compound statement), we use the notation:

program asterisk;
var
x: integer
writein (‘enter a number from 0 to 10, if you give five I will paint a star’);
readln (x);
if x = 5 then writeln (‘*’);
begin
writeln (‘*’);
x:=x+1;
writeln(‘Now x has the value:’ ,x) ;
end; writeln(‘ending the program’); end.

As you can see in the above example, a compound statement (block of executed instructions) is included in brackets begin and end, terminated with a semicolon. In the previous program, in the case of fulfilling the condition, after IF there was instruction executed after THEN and all the subsequent ones.
Conditional statement IF you can use so the program executes the next instruction only if the condition is not met, then instruction that follows will then be omitted. The construction of this provision will have the form:
if condition then statement_1 else statement_2;
If the condition is met, the statement_1 will be executed (or appearing in its place a block of statements in braces begin, end;), and statement_2 (or similarly block of instructions) that occurs after ELSE will be ignored. We mean ELSE as “otherwise”.

The post Conditional statements in Pascal appeared first on TriumVir News.


Viewing all articles
Browse latest Browse all 7