11 Computer Science-Python Conditional Statements-Notes
11 Computer Science-Python Conditional Statements-Notes
Computer Science
Conditional Statements
Notes
Types of statements in Python : Statements are the instructions given to the computer to
perform any kind of action. Python statements can belong to one of the following three
types.
Empty Statement
A statement that does nothing.In Python an empty statement is pass statement.It takes the
following form:
Pass
Whenever Python encounters a pass statement, Python does nothing and moves to next
statement in the flow of control.
Simple Statement
Any executable statement is a simple statement in Python. For example:
Name=input(“enter your name”)
Compound Statement
A compound statement represents a group of statements executed as a unit. The
compound statement in Python are written in a specific pattern .
It has :
-> A header line which begins with a keyword and ends with a colon.
-> A body consisting of one or more Python statements, each indented inside the header
line.
Sequential statements are set of statements where the execution process will
happen in sequence manner. So, these kind of statements are called as
sequential statements.
The problem in sequential statements is, if the logic has broken in any one
of the line, then complete source code execution will get broken. So, we are
going to decision making, iterative and jump statements to avoid this kind of
problems.
1. if statements
2. if-else statements
3. if-elif-else statements
4. nested if statements
Iteration
We can write programs that has more than one choice of actions depending on
a variable’s value.
You use the if statement to perform one action if one thing is true,
or any number of other actions, if something else is true.
We must use indentation to define that code that is executed, based on whether
a condition is met.
Syntax :
if <condition>:
statement
Example 1: if grade==’A’:
print(“well done”)
Example 2: if a>b :
print(“A has more than B has”)
Example 3: age=18
age=17
if age>= 18:
print "Person eligible for vote"
print "****************************"
OUTPUT
Person not eligible for vote
****************************
Person eligible for vote
Person not eligible for vote
FLOW CHART
If-else statement :
The else statement is an optional statement and there could be at most only one
else statement following if.
if expression:
statement(s)
else:
statement(s)
Example :
if grade == ‘A’ :
print (“well done”)
else:
print (”try again”)
FLOW CHART
Example1:
age=25
if age>18:
elif age==18:
else:
print "Person not eligible for vote"
Example 2:
This script will compare two strings based on the input from the use
if password == 'hello':
print'Password Accepted'
else:
print'Sorry, that is the wrong password.'
Example 3:
if (num%2==0) :
else:
print(num, “is odd number ”)
Sometimes there are more than two possibilities; in that case we can use the
elif statement
It stands for “else if,” which means that if the original if statement is
false and the elif statement is true, execute the block of code following
the elif statement.
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Example 1:
if num<0:
Let’s show one more examples, in which will also make use of the elif statement.
number = 20
if guess == number:
print('Congratulations, you guessed it.')
Example 3 :
Program to read two numbers and an arithmetic operator and displays the
computed result.
result=0
if op == ‘+’ :
result= num1+num2
result=num1-num2
Result=num1+num2
result=num1+num2
result=num1%num2
One conditional can also be nested within another. i.e., We can have a
if...elif...else statement inside another if...elif...else statement. This is called
nesting in computer programming.
Any number of these statements can be nested inside one another. Indentation is
the only way to figure out the level of nesting.
Initially, the flow of control enters the outer if and test expression is evaluated.
When resolving as true, it executes inside statements of if part and goes to inner
if.
Then, test expression of inner if is evaluated. If it is true, the body part of inner
if statements are executed. Otherwise, if it is false, the flow of control jumps out
to the outer if body part to evaluate test expression.
When resolve as false(outre if), the flow of control jumps to its else part.
if TEST EXPRESSION1:
if TEST EXPRESSION2:
STATEMENTS_B
else:
STATEMENTS_C
else:
if TEST EXPRESSION3:
STATEMENTS_D
else:
STATEMENTS_E
a=10
b=20
c=5
if a>b:
if a>c:
print("Greatest number is ",a)
else:
print("Greatest number is",c)
else:
if b>c:
print("Greatest number is ",b)
else:
print("Greatest number",c)
Example 2:
age=20
gpa=1.1
if age>18:
if gpa>0.8:
print("Your age and GPA are both enough");
print("You have been selected for university")
else:
print("your gpa is not enough")
else:
print("your age is not enough")
Example 3:
When we execute the above program, it will be produced the following result.
Output 1
Enter a number: 5
Number is positive
Output 2
Enter a number: -5
Number is Negative
Output 3
Enter a number :0
number is zero
1. Statements are the instructions given to the computer to perform any kind
of tasks.
2. Python statements can be on one of these types: empty statement,
single statement and compound statement.
3. An empty statement is the statement that does nothing. Python offers
pass statement as empty statement.
4. Single executable statement forms a simple statement.
5. A compound statement represents a group of statement executed as a unit.
6. Every compound statement of a python has a header and an indented body
below the header.
7. The flow of control in a program can be in three ways :
sequentially, selectively and iteratively.
8. The sequence constructs means execution of statements one after the
another.
9. The selection constructs means execution of statements depending upon
a condition.
10.The iteration constructs means execution of a set of statements
depending upon a condition test.
11.Python provides one selection statement in different forms-if…else and
if…..elif…else.
12.The if…..else statement can be nested also.