Lesson+5

**LESSON 5** //**Selective structures: If... then... end if**// Selective structures are the means by which a program chooses between tow or more conditions. For example, let's say you have a class list and want to output only those students whose last name begins with "T". You would use a selective structure (sometimes called a decision structure) in order to do so. The syntax for such a structure is: //** if... **// **//elsif...// ** **//else...// ****//end if.// **

Try this:

%Choosing between two alternatives **var mark: int** **put "Enter mark:"..** **get mark** **if mark >= 50** **then** **put "you pass"** **else** **put "time to enroll in summer school "** <span style="font-family: 'Courier New',Courier,monospace; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">**end if**

<span style="font-family: Arial,Helvetica,sans-serif; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Notice that as with repetitive structures, selective structures must be "ended": each if statement must be accompanied by an end if. Try this:

<span style="font-family: 'Courier New',Courier,monospace; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">%Choosing between many alternatives <span style="font-family: 'Courier New',Courier,monospace;">var **mark:int**
 * <span style="font-family: 'Courier New',Courier,monospace;">put "Enter mark:".. get mark **
 * <span style="font-family: 'Courier New',Courier,monospace;">if mark >= 80 then **
 * <span style="font-family: 'Courier New',Courier,monospace;">put "You got and A " **
 * <span style="font-family: 'Courier New',Courier,monospace;">elsif mark >= 70 then **
 * <span style="font-family: 'Courier New',Courier,monospace;">put "You got a B " **
 * <span style="font-family: 'Courier New',Courier,monospace;">elsif mark >= 50 then **
 * <span style="font-family: 'Courier New',Courier,monospace;">put "You got a C " **
 * <span style="font-family: 'Courier New',Courier,monospace;">else **
 * <span style="font-family: 'Courier New',Courier,monospace;">put "Receiving an F could jeopardise your summer " **
 * <span style="font-family: 'Courier New',Courier,monospace;">end if **

Notice that the else condition takes care of any possibility not otherwise accounted for by the series of if's & elsif's.


 * Task**
 * 1) Modify any program you have written such that it is menu driven.
 * 2) Write a program which quizzes the user on some academic subject other than computer science (for example geography or history). Give the user three chances at each of the ten questions. Inform the user if they guessed correctly or incorrectly.
 * 3) Modify 2 such that it calculates a percentage correct and creates a graph displaying the percentage results


 * Remember to make your programs as user-friendly as possible, never insult the intelligence of the user.**