Module 2
Selection Structure
Lecture
Selection Structure
So far, we’ve written simple programs that execute all of its statements one after another. This is called sequential execution. But what if you want to selectively execute a particular statement? That is, a statement’s execution (or non-execution) depends on the conditions at run-time. This is called selective execution and can be achieved with conditional statements. Also known as selection statements or structure.
Single-selection Statements
This is the simplest form of conditional statements. It allows you to execute one statement or more if a condition is fulfilled. The condition is usually written as a Boolean expression.
Syntax
if condition:
statement
Example
if test_marks >= 50:
print “Passed\n”
As you can see, we may combine two Boolean expressions or more to be used as the condition in the if header. We may also execute more than one statement if the condition is fulfilled – just be sure that all of those statements are indented with the same level of indentation. And remember that at least one statement must be provided in the body of the conditional statement.
Double-selection Statements
The single-selection statement executes a block of statements if a condition is fulfilled and doesn’t do anything else if the condition is not fulfilled. But what if you want your program to do something if a condition is not fulfilled? You use a double-selection statement.
Syntax
if condition:
statement
else:
statement
Example 1
if test_marks >= 50:
print “Passed\n”
else:
print “Failed\n”
Take note that we do not specify a condition in the else header. That would be a syntax error.
Example 2
n = input (“Number? ”)
if n < 0:
print “The absolute value of”, n, “ is ”, -n
else:
print “The absolute value of”, n, “ is ”, n
Try the above codes and see what the output of these codes is.
Condition Expressions
There are several tests that can be done in a condition expression. Below is the list of symbols of the different tests:
Symbols Test Description < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to != Not equal to <> Not equal to
Boolean Expressions
Boolean expressions are expressions that have a truth value. When evaluated, its end value is either True or False. The type of the value is bool. True is represented by integer 1 and False is represented by integer 0.
Boolean expressions are composed of operands and operators just like arithmetical expressions. Boolean operators are comparison operators and logical operators.
Comparison Operators
As the name suggests, comparison operators compare two values in terms of magnitude. They’re shown below.
Operator Explanation Expression Truth Value == is equal to 2 == 3 False != is not equal to 2 != 3 True < is less than 2 < 3 True <= is less or equal to 2 <= 3 True > is greater than 2 > 3 False >= is greater or equal to 2 >= 3 False
Logical Operators
Logical operators are used to combine two Boolean expressions into one. They’re shown below.
Operator Explanation Expression Truth Value and both expressions must be true True and True True True and False False False and True False False and False False or at least one expression must be true True or True True True or False True False or True True False or False False not inverse the truth value of the expression not True False not False True
Multiple-selection Statements
If you’re still not satisfied with the double-selection statement, then there’s the multiple-selection statement which is used to branch into any one of a range of outcomes.
Syntax
if condition:
statement
elif condition:
statement
else:
statement
Example 1 test_marks = input(“Enter marks: ”)
if test_marks >= 80:
grade = “A”
elif test_marks >= 70:
grade = “B”
elif test_marks >= 60:
grade = “C”
elif test_marks >= 50:
grade = “D”
else:
grade = “F”
print “Grade: ”, grade
…
You may specify as many elif headers as you need. Take note that each elif header also requires a condition.
elif stands for else if. If the original statement is false and the elif statement is True, then the block of statements after elif will be executed.
Run the following samples and look at the output.
Example 2
color = raw_input(“Enter your favourite color : ”)
if color == “green”:
print “You entered green!”
elif color == “purple”:
print “You entered purple!”
else:
print “You did not enter green or purple.”
Example 3
#Asks for a number.
#Prints if it is even or odd
number = input("Tell me a number: ")
if number % 2 == 0:
print number,"is even."
elif number % 2 == 1:
print number,"is odd."
else:
print number,"is very strange."
Lab Exercise
Selection Structure
Name: ID:
Write a simple password program. Use the following output as a guide for your output layout.
Modify the above password program to keep track of how many times the user has entered the password wrong. If it is more than 3 times, print “Access Denied. Please try again”
3. In command-line mode, type the following expressions and write down the correct output. If you get an error, explain, in your own words, why the error occurred.
type(False) Output: __________________________________________________
type(true) Output: __________________________________________________
bool(1) Output: __________________________________________________
2 * 3 < 4 Output: __________________________________________________
4 != 2 * 3 Output: __________________________________________________
“kate” > “katie” Output: __________________________________________________
( 2 * 3 < 4) and ( 4 != 2 * 3)
Output: __________________________________________________
not (1 + 1 == 2) Output: __________________________________________________
CMPF144 Fundamentals of Computing Theory
Revised & updated by Badariah Solemon (4/4/2008) Page 1 of 6
Username : shahrulPassword : passShahUsername or password is incorrect
Username : shahrulPassword : sayaShahWelcome Mohamad Shahrul Shahidan
Description
So far, we’ve written simple programs that execute all of its statements one after another. This is called sequential execution. But what if you want to selectively execute a particular statement? That is, a statement’s execution (or non-execution) depends on the conditions at run-time. This is called selective execution and can be achieved with conditional statements. Also known as selection statements or structure.
Presentation Transcript
Your Facebook Friends on WizIQ