Unit - 2 Complete-Conditional Statements and Loops
Unit - 2 Complete-Conditional Statements and Loops
UNIT-2
Conditionals: Conditional statement in Python (if-else statement, its working and execution),
Nested-if statement and Elif statement in Python, Expression Evaluation & Float Representation.
Loops: Purpose and working of loops , While loop including its working, For Loop , Nested Loops
, Break and Continue
1. Control Flow: It is the order in which the code of the program executes, the control flow
of a program is executed by
a. The conditional statements
b. The Iterative statement (Generally called loops)
1.1. The Conditional Statements- In order to write the useful programs, the ability to check the
conditions and change the behaviour of the program is important. This ability is obtained by
putting conditional statements in the program.
1.2. The if statement is one such statement commonly used even in our daily general language.
Let’s say for e.g. – In a grading system in class, the following guidelines are followed:
Excellent – If student obtains more than 95%.
Good – If student scores above 80% but less than 95%.
Average -- If student scores above 60% but less than 80%.
Below Average-- If student scores less than 60 %.
Conditional
Statement.
1|Page
PYTHON NOTES | Rahul Virmani
Like every conditional statement, the if statement actually evaluates if the condition is TRUE or
FALSE. The statement is actuated (within the block) only if the condition expression is TRUE.
NOTE- In PYTHON, the colon (:) is used to initialize the loop or a condition and the indentation is
used to create a block of code running under a loop or a condition. (Similar to Curly Braces ( { ) in
C, C++).
Example-
Initialization
Keyword
Indentation
Condition Statement
if Logic Flowchart.
Example -
2|Page
PYTHON NOTES | Rahul Virmani
1.3. Alternative Execution – if else statement, is basically an alternative execution in which there
are two possibilities, and the condition determines that which one will get executed.
The syntax for the if else statement can be given as:
Conditional Statement
Statement 1, to be
executed when the
condition is TRUE
Statement 2, to be
Keyword
executed when the
The condition is FALSE.
if-else logic, flowchart.
Example –
3|Page
PYTHON NOTES | Rahul Virmani
Elif : an abbreviated form of ‘else-if’. Again, exactly one branch will be executed.
NOTE- There is no limit on the number of Elif statement. But If there is an else clause, it has
to be at the end. However, there is no mandate that an else statement has to be used.
Example – Lets take the initial example we took, In a grading system in class, the following
guidelines are followed:
Excellent – If student obtains more than 95%.
Good – If student scores above 80% but less than 95%.
Average -- If student scores above 60% but less than 80%.
Below Average-- If student scores less than 60 %.
1.5. Nested Conditionals: one condition can also be nested within another. We could have
written the above example like this:
4|Page
PYTHON NOTES | Rahul Virmani
Also, the flowchart of the if…elif…else statement; can be restructured for nested conditionals.
The nested conditionals, although can be easily thought of by the user, the indentations to be used
while coding make the code readability bit tough. Hence, it’s a good idea to avoid the nested if-
else logic.
Example – Write a Program to take age as input from user and print. “You are not an adult”, if age
is less than 18. Otherwise print “You are an adult”. Also check that age cannot be 0, if so print “age
can’t be 0”.
5|Page
PYTHON NOTES | Rahul Virmani
So, before you update the variable, you have to initialize it using a simple assignment statement:
6|Page
PYTHON NOTES | Rahul Virmani
While Loop.
Nested While Loop.
For Loop.
Nested For Loop.
If a loop has to repeat the block of code till the condition is true, a WHILE loop is used.
Example – While n is greater than 0, display the value of n and reduce the value by 1. Print
“Blast Off”, when you get 0.
7|Page
PYTHON NOTES | Rahul Virmani
The variable that changes its value every time, is called iteration variable. If there is no iteration
variable then loop will run infinitely, resulting in an infinite loop.
Now, in this case the condition is always true so the loop will run infinitely.
8|Page
PYTHON NOTES | Rahul Virmani
Example - Write a Program to find the square of the number from 1 to 10.
By default,
If only a single value in the range function is given. Only the Stop value is given then It has a
default start value of 0, with a step of 1.
9|Page
PYTHON NOTES | Rahul Virmani
If only two values in the range function are given. The Start and Stop value are given in order,
with a step of 1(default).
If a Range function has three arguments, then start value, stop value and the step value are all
given. For e.g.
Example- Write a Program to print the even numbers between 1 and 100.
Unlike while loops, the for loop is used when we need to repeat the block of the code for fixed
number of times. In Python, the for loop is repeated for all the members of a sequence as in set,
list or tuple in order, executing the block of code every time, till no element is left in the sequence.
Unlike the while loop in which the condition is tested for every iteration.
10 | P a g e
PYTHON NOTES | Rahul Virmani
11 | P a g e
PYTHON NOTES | Rahul Virmani
Example- The nested while loop can be easily understood by the following example.
Question- What should be the values of num1 and num2 in the given code snippet, if the expected
output is 4?
a. 16,6
num1=
num2= b. 12,5
while (num1>=2): c. 8,2
if (num1>num2):
d. 16,2
num1=num1/2
else:
print(num1)
Example-
VS
1. While loop is used to iterate the block of codes until the given condition is True.
2. While loop in another While loop is called nested while loop.
3. Range() function returns a sequence of numbers.
4. A for loop is used for iterating over a sequence (string, list, tuple, set, etc-).
5. Nested for loop allows us to create one loop inside the other loop.
13 | P a g e
PYTHON NOTES | Rahul Virmani
Other than break and continue, there is another statement called pass statement; commonly known
as do-nothing statement. The continue statement skips the iteration while pass statement
continues with the loop, just wastes the execution time. The pass statement does nothing.
1. Jump statement are used to alter the flow of the loop, i.e., if you wish to skip an iteration
(continue) or just exit the loop on a certain condition (break).
14 | P a g e