|
|
Notes 0008PL/SQL IF StatementPL/SQL, being a procedural language naturally has lots of flow control constructs, from IF statements to WHILE loops. In these notes we will examine some of the more popular ones. Remember to type: SET SERVEROUTPUT ON in SQL*Plus before running any programs, so that you can see the output. IF - THEN StructureThe general format of an IF statement is:
Assuming we all know how to program, and know what IF statements are, I'm not going to spend too much time on the obvious. An example program that uses an IF statement is:
Which produces the expected output of: Ans: 23 is less than 115 IF - ELSE StructureJust as in any programming language that has an IF statement, there is also the ELSE clause to the IF statement. The full structure of an IF statement is thus:
Let's modify our simple example to:
Note that we've also modified the B := A * 5 to B := A / 5 in order to test the ELSE condition. IF Statement nestingWe can also put IF statements inside other IF statements. Here, again, let's jump right into an example:
The code above finds the maximum value (ABCMAX) of three variables (A, B, and C). The code looks self explanatory; so we won't spend much time on it. IF – ELSIF StructureWhen IF and ELSE are not enough, we can resort to using ELSIF. This is an else if equivalent in C (and in Perl it is actually named elsif). Let's say we wanted to calculate the letter grade given a number grade, we may write a program such as:
Which for our particular example number grade produces output: Grade 82.5 is B That about covers the IF statement.
|