Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Comparison Operators

You can probably see how the IF statements can be used to make decisions, using any of the comparison operators

  • (equal to) =
  • (less than) <
  • (greater than) >
  • (less than or equal to) <=
  • (greater than or equal to) >=
  • (not equal to) <>


Just in case you can't though I will explain. When the program is running it comes across the line

IF (first = second) THEN


the program evaluates the condition, if it is true then the statement or the compound statement following the THEN is executed. In the case of this program, the line

WRITELN(first,' is equal to ',second)


If the above condition was true then the lines

ELSE IF (first < second) THEN WRITELN(first,' ',second) ELSE WRITELN(second,' ',first);


would be ignored by the program. If however it was false then the program would move on to the next condition

ELSE IF (first < second) THEN


if this was evaluated as true then again the statement or the compound statement following the THEN would be executed. In this program if this line is also false then there is only one other course of action. That is to execute the line

WRITELN(second,' ',first);


Note: Notice also the use of the type CHAR. I briefly talked about this type in chapter 1. In line 3 since the only other option after line 1 and 2 is that first is greater than second, there is no need to spell that out with a boolean condition. Note too, that the ordering comes from the relative order prescribed by the ASCII table. With the looping structures, the instruction or group of instructions are repeated until a condition is met. The simplest looping syntax in Pascal is the REPEAT - UNTIL loop. You can imagine repeating an instruction until some condition is met. In the flowchart, we would loop around checking the loop condition at each loop until it is found to be TRUE. We could think of REPEAT the instruction wash your hands UNTIL they are clean. The loop exit condition is when your hands are clean.

4. Looping : REPEAT - UNTIL

This program is another very simple program which demonstrates the use of the IF statement to select choices from a menu. It also incorporates a REPEAT - UNTIL loop in order to recall the menu until the exit condition is satisfied.

Program 3

PROGRAM menu(INPUT,OUTPUT); { a short menu demonstration program } VAR choice : INTEGER; BEGIN REPEAT WRITELN('To exit press : 0'); WRITELN('To choose first option, enter : 1'); WRITELN('To choose second option, enter : 2'); WRITELN('To choose third option, enter : 3'); WRITELN('To choose fourth option, enter : 4'); WRITELN('To choose fifth option, enter : 5'); WRITE('Enter choice : '); READLN(choice); WRITELN; IF (choice < 6) AND (choice > -1) THEN BEGIN IF choice = 1 THEN WRITELN('You chose option 1') ELSE IF choice = 2 THEN WRITELN('You chose option 2') ELSE IF choice = 3 THEN WRITELN('You chose option 3') ELSE IF choice = 4 THEN WRITELN('You chose option 4') ELSE IF choice = 5 THEN WRITELN('You chose option 5'); END ELSE WRITELN('Option "',choice:0,'" is out of range'); WRITELN; UNTIL (choice = 0); { exit loop condition } WRITELN('* Quitting *'); WRITELN; END. { program menu }


Notice the introduction of a REPEAT - UNTIL loop. This brackets a piece of code, making it repeat (loop round) until the boolean condition



UNTIL (choice = 0);


is satisfied. The parentheses are optional. Also notice, in the IF statement the use of another BEGIN - END pair. This is the syntax of a compound statement.

program body compound statement


Date: 2016-03-03; view: 660


<== previous page | next page ==>
Reading And Writing Variables | Syntax of the REPEAT - UNTIL
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)