How to use if else statement in pascal

A key feature of computer programs is decision making, where the program selects a result from among a list of alternatives. The PASCAL statements that support such decision making are collectively called selection structures and include the IF..THEN..ELSE and CASE statements. We will later see that the CASE statement is not in vogue (i.e., isn't used much) because it is harder to read clearly than an IF statement.

Computer program design is also facilitated by the use of program structures called loops that cause the block of statements within the loop to repeat or iterate . The latter term gives rise to the concept of iteration structures , which in PASCAL include the FOR, WHILE, and REPEAT statements.

In Section 4.1, we discuss the basic justification and concepts associated with iteration and selection structures, with examples of modularity in the block-IF statement. We then discuss usage of the IF and CASE statements (Section 4.2). An overview of iteration structures is given in Section 4.3, with usage of the WHILE, FOR, and REPEAT statements discussed in Section 4.4. The final topic of this section is the use of variable loop limits, which allows you to "customize" a loop with limit values that are passed into a procedure through its argument list.

4.1. Overview of Selection Structures.

Definition. A logical operator is an operation that inputs one or more values equal to zero or one, and outputs a zero (i.e., logical false ) or a one (i.e., logical true ).

Definition. A relational operator is an operation that inputs one or more numeric or alphabetical values and compares them to a prespecified criterion (e.g., is x greater than 3?). The result of a relational operator is a zero, if the test that the operator implements fails, and a one if the test succeeds.

Definition. A logical predicate is comprised of one or more logical or relational operations, and produces a zero or one as a result.

Definition. A Boolean value is a one or a zero.

Definition. The logical operators NOT( x ), AND( x , y ), and OR( x , y ) are defined as follows, where x and y ) are Boolean values:

NOT(x) x=0 x=1 --------+----------+ | 1 0 | +----------+ AND(x,y) x=0 x=1 --------+----------+ y=0 | 0 0 | | | y=1 | 0 1 | +----------+ OR(x,y) x=0 x=1 --------+----------+ y=0 | 0 1 | | | y=1 | 1 1 | +----------+

Definition. The relational operators are defined as:

NAME SYMBOL MEANING --------------- ---------- ------------------------------------- greater-than x > y "x" is greater than "y" greater-equal x >= y "x" is greater than or equal to "y" equals x == y "x" equals "y" not-equals x <> y "x" does not equal "y" less-equal x <= y "x" is less than or equal to "y" less-than x < y "x" is less than "y"

Observation. As we mentioned in Section 3, various attempts have been made to render computer languages more like English. One of the key constructs in this effort is the IF..THEN..ELSE statement, which is structured as follows:

IF logical-predicate THEN -- statements to execute if the predicate evaluates to true -- ELSE -- statements to execute if the predicate evaluates to false -- ENDIF

Example. Suppose your program has a flag (a logical or Boolean decision variable) that causes statements to be printed to your computer monitor. For example:

IF flag THEN WRITELN('Flag is true') ELSE WRITELN('Flag is false') ENDIF

Remark. PASCAL facilitates decisions among multiple alternatives via the BLOCK-IF statement, which is comprised of multiple IF..THEN..ELSE statements that are chained together , as follows:

IF predicate-1 THEN -- statements to execute if predicate-1 evaluates to true -- ELSEIF predicate-2 THEN -- statements to execute if predicate-2 evaluates to true -- ELSEIF . ELSEIF predicate-n THEN -- statements to execute if predicate-n evaluates to true -- ELSE -- statements to execute if the preceding predicates all evaluate to false -- ENDIF

In the 1970s, programming language designers were looking for simpler ways to write statements that were previously involved expressions. Since the programmers made more errors on complex statements, it seemed reasonable to attempt to distill the BLOCK-IF into a more concise representation. In PASCAL, this representation is called the CASE statement. Instead of relational operators, the CASE statement simply specifies possible values for a variable, together with statements to be executed if that variable has a given value. For example, consider the following CASE statement (written in pseudocode) for a academic grade evaluation:

CASE OF grade: 'A': WRITELN('Excellent work!'); 'B': WRITELN('You did well. '); 'C': WRITELN('Average performance.'); 'D': WRITELN('Needs some improvement. '); 'E': WRITELN('Trouble ahead!'); END-CASE

Certainly this simple instance of the CASE statement is easier to read than the corresponding BLOCK-IF statement:

IF grade == 'A' THEN WRITELN('Excellent work!') ELSEIF grade == 'B' THEN WRITELN('You did well. '); ELSEIF grade == 'C' THEN WRITELN('Average performance.'); ELSEIF grade == 'D' THEN WRITELN('Needs some improvement. '); ELSEIF grade == 'E' THEN WRITELN('Trouble ahead!'); ENDIF

The problem with CASE statements occurs not when the statement blocks are small, or when there are a few alternatives. Rather, the CASE statement begins to defeat its design goal of conciseness and readability when the blocks of statements become large or when there are many alternatives. In such expressions, a given CASE statement may span several pages or more. When you are trying to debug such statements, it becomes very difficult to remember where you are in the statement, and one tends to feel lost. As a result, we prefer to use only IF or BLOCK-IF statements for decision structures.

4.2. PASCAL Selection Structures.

The PASCAL language provides IF or BLOCK-IF constructs, where the latter is a variation of the IF statement, as discussed in Section 3.1. We define these statements as follows:

Purpose: The IF statement provides a mechanism for decision based on a logical predicate.