• No results found

Conditional Constructs

In document Making Use of Python (Page 105-108)

■■ Looping constructs

Let’s look at each of these in detail.

Conditional Constructs

Conditional constructs are used to incorporate decision making into programs. The result of this decision making determines the sequence in which a program will execute instructions. You can control the flow of a program by using conditional constructs. These constructs allow the selective execution of statements depending on the value of the expressions associated with them. This section will discuss the programming con- structs available in Python, such as if, if...else, elif, and nested if.

The if Statement

The if statement of Python is similar to that of other languages. The if statement con- tains a logical expression using which data is compared, and a decision is made based on the result of the comparison. The syntax of the if statement is:

if condition: statement_true

All the statements indented by the same number of character spaces after a pro- gramming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.

In the if statement, condition is evaluated first. If condition is true—that is, if its value is nonzero—then the statements in the statement_true block are executed. Otherwise, the next statement following the statement_true block is executed. For example,

>>> x=10 >>> if x>0:

... print ‘Hello’

In this example, the if statement prints Hello if the value of the variable x is greater than 0.

The else Statement

An else statement can be combined with an if statement. An else statement con- tains the block of code that executes if the conditional expression in the if statement resolves to 0 or a false value. The syntax for the if...else construct is this:

if condition: statement_true else:

To print whether a given integer variable is even or odd, the code will be this:

if num%2==0: print ‘even’ else:

print ‘odd’

In the preceding example, ‘even’ is printed if the modulo of the division of num by 2is 0; otherwise, ‘odd’ is printed.

The elif Statement

While making decisions, you may want to include a condition in the else statement so that the statements in the else block are executed only when that condition is true. The elif statement in Python allows you to test multiple expressions for one truth value and executes a particular block of code as soon as one of the conditions evaluates to true. The syntax for an elif construct is this:

if condition1: statement1_true elif condition2: statements2_true : : elif condition: statementN_true else: statement_none_of_above

The following example uses an elif construct to determine if the input character is a vowel. Otherwise, it prints an appropriate message.

in_chr=raw_input(“Enter a character:”) if(in_chr == ‘a’):

print”Vowel a” elif (in_chr == ‘e’):

print”Vowel e” elif (in_chr == ‘i’):

print”Vowel i” elif (in_chr == ‘o’):

print”Vowel o” elif(in_chr == ‘u’):

print”Vowel u” else:

print “The character is not a vowel”

The preceding code takes input from the user in the variable in_chr. It checks if the value of in_chr equals a. If the condition is satisfied, it prints Vowel a. If the condi- tion is not satisfied, the control of the program moves to the first elif statement. This goes on until a satisfying condition is found in the elif statements. If a match is found in an elif statement, the statement block in that elif statement is executed and then

the control comes out of the if construct. If it does not find a match, the control moves to the else statement. If the user enters the value o, the output is Vowel o. If the user enters h, none of the conditions in if and elif statements is met. Therefore, the out- put The character is not a vowel is displayed.

Nested if

You may also want to see what happens when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if con- struct. In a nested if construct, you can have an if...else construct inside another if...elseconstruct. The following code uses a nested if construct to check if the input character is an uppercase or a lowercase letter.

inp=raw_input(‘Enter a character’)

# Find if the character is lowercase or uppercase if (inp >= ‘A’):

if(inp <= ‘Z’): print “Uppercase” elif (inp >= ‘a’):

if(inp <= ‘z’): print “Lowercase” else:

print “Input character > z” else:

print “Input character > z but less than a” else:

print “Input character less than A”

You might be confused by now about deciding which else statement belongs to which if statement. The answer to this is indentation. Notice that each line in a basic block is indented by the same number of character spaces.

In the preceding example, consider a case where the user enters the value D. The exe- cution of the code will be as follows:

1. Check if the input value (D)>= 'A'. If yes, check if the input value <= 'Z'. 2. In this case, because the input value (D) >= 'A' and <= 'Z', the output will be

Uppercase.

Similarly, consider the case where the user enters f. The execution of the code would be as follows:

1. Check if the input value (f)>= 'A'. Because the input value ‘f’ is not >= 'A' and is not <= 'Z', the control passes to the elif statement that belongs to the second if statement.

2. The elif statement will check if the input value >= 'a' and <='z'. Because it is, the output will be Lowercase.

If all the preceding three if statements return false, the output will be 'Input character > z'.

So far, all the conditional statements that you have worked with execute in a linear fashion. This means that these statements execute only once and stop when the condi- tion is fulfilled. There might be situations, though, when you want a set of statements to repeat a specific number of times. Loops can be used to achieve this type of functionality.

In document Making Use of Python (Page 105-108)