Many programming tasks, however, require conditional control, that is, the ability to react differently based upon some condition. For example, consider the task of assigning students letter grades. Depending upon what the student's average is, a different letter grade must be assigned (e.g., 90 to 100 is an A, 80 to 89 is a B, etc.). In this lesson, you will be introduced to the if statement which is used to perform such conditional execution. Based upon some condition, an if statement can choose among alternative sequences of code to execute, or even choose to execute no code at all.
The general form of an if statement is as follows, where the else case is optional:
The CONDITION in an if statement can be any boolean expression, that is, any expression that evaluates to either true or false. The following relational operators can be used to build boolean expressions:
|!=||not equal to|
|<=||less than or equal to|
|>=||greater than or equal to|
Write a code segment which prompts the user for their name, and then displays a special greeting to that person if their name is the same as yours. If the name entered by the user is anything other than your name, your code should not produce any output.
At Dickinson, "roll call" grades are assigned around midterm to help students to assess their performance. A common rule-of-thumb is to consider a grade of C or better to be Satisfactory, while C- or worse is Unsatisfactory. Write a code segment which prompts the user for their average, and then displays a message assessing their performance. If their average is 73 or higher, they should receive a Satisfactory rating, otherwise Unsatisfactory.
In its simplest form, an if statement can decide between executing a sequence of statements once, or not at all. For example, in the first example of an if statement above, the code will either display a message on the condition that a number is positive, or else it will do nothing at all. Using an else case, an if statement can decide between two alternative sections of code, as in the example where a different message is displayed for positive and non-positive numbers.
There are examples, however, in which more than two alternatives must be considered. For example, suppose you were to write code for assigning letter grades in a class based on the student's final class average. In a rigid scoring system, any grade of 90 or better would receive an "A", any grade between 80 and 89 a "B", and so on down through "C", "D", and "F". Thus, there are 5 alternatives for assigning the grade, conditional upon where the student's average.
|Nested if Statements||Cascading if Statements|
The term cascading refers to the way that control cascades down the statement like water down a waterfall. The top-most test is evaluated first, in this case (grade >= 90). If this test succeeds, then the corresponding statements are executed and control passes to the end of the if statement. If not, then control cascades down to the next if test, in this case (grade >= 80). In general, control cascades down the statement from one test to another until one succeeds or the end of the statement is reached.
Cut-and-paste the above cascading if statement into the interpreter. Add a prompt which asks the user for their number grade and reads that value into the grade variable. Similarly, add a write statement at the end to display the letter grade. Execute the code on each of the following grades and report the result.
Cut-and-paste the code from Exercise 1 which displayed whether a number is positive or not. Modify that statement so that it prints one of three messages, stating whether the number is positive, negative, or zero. Test your new statement on a variety of numbers to be sure that it works correctly.
Once written, a control statement is just like any other statement (e.g., an assignment or a call to document.write). In particular, you can nest a control statement inside another control statement. You have already seen this with a cascading if statement, which is really just a series of nested if statements. The same holds for the other control statement you know, the for loop. You can nest one for loop inside another, although this is usually avoided since tracing the flow of control can become tricky. You can also nest an if statement inside a for loop, and vice versa.
Consider the following code segment, which prints a table of powers of 2. The number of rows in the table is specified by the user in response to a prompt.
Add an appropriate if statement to this code to verify that the number entered by the user is indeed 1 or greater. If so, it should proceed to display the table as is. If not, it should display an error message and avoid executing any of the table code.