Conditions: Minimum and Maximum. If statemens
Search for minimum and maximum
- Which of the two variables is greater (maximum):
var (x,y) := ReadInteger2; var Max: integer; if x>y then Max := x else Max := y;
- Which of the two variables is greater (maximum) and which one is less (minimum):
var (x,y) := ReadInteger2; var Min,Max: integer; if x>y then begin // don't forget about compound statement! Max := x; Min := y; end else begin Max := y; Min := x; end;
It is very important here not to forget about compound statement
begin..end
.Random function
Random
function is often used in Pascal.- To generate numbers from 0 to
n
not including the value ofn
itself (integers in the interval[0,N)
), you must write:
var genNumb:=random (n);
- To generate numbers in the range
[a,b]
, you must write:
var a:=readinteger(); var b:=readinteger(); var genNumb:=random (a,b);
To generate Tuple of two elements (Random2), or three elements (Random3):
var (a, b, c) := Random3(10.0, 20.0); // range [10, 20) write(a:0:2,' ',b:0:2,' ', c:0:2) // output: 14.73 18.63 19.72
Nested If statements. Using Assert function
Example:
To do: A Point (x,y) on а coordinate plane is given ( ≠ 0, ≠ 0). Output a number of quarter:
⤵ Решение:
var (x,y) := ReadInteger2; var quarter: integer; Assert((x<>0) and (y<>0), 'incorrect input'); // must return True if x>0 then if y>0 then quarter := 1 else quarter := 4 else if y>0 then quarter := 2 else quarter := 3;