Module 3 — Conditional Statements and Iterations

A.I Hub
6 min readJul 29, 2023

--

In this step by step guide, we will walk you through conditional statements and iterations by using python programming. We exploring the concept of conditional statements and iterations with theoretical + practical approach. We covering fundamentals and necessary tactics between programming.

Introduction

Conditional statement are used when we need to make a decisions in our program. A block of code in executed only if a certain condition is satisfied and a different piece of code is run otherwise. Python provides some conditional statements are listed below.

  • If
  • Elif
  • Else

If

If statements can be used standalone but elif and else statements are always accompanied by a corresponding if statements.

if (Condition):

If statement is the simplest conditional statement. The syntax proper arrangement of symbols and keywords to constitute a correct programming command of an if statement is:

If (Condition):

Statement1

Now you remember one thing, the line is following the if(condition):

is indented. We may specify more indented statements after the statement 1. Statement indented the same amount after the colon(:) are a part of the if statement. These statements run when the condition of the if statement is True. In case, this condition is False, statement 1 and other indented statements will not be executed.

We want to record and display the marks a student obtained in a particular subject. The total marks obtained should not exceed 100. The program should display an error or warning when the marks are greater than 100.

Student_marks = 90

if(Student_marks > 100):

Print(“ Marks exceed 100 “)

The colon (:) after if (Student_marks > 100) is important because it separates the condition from the statements. The condition inside if(condition) is evaluated. It returns True if the condition is fulfilled and False otherwise. Arithmetic, logical and conditional operators can be employed to design a condition.

An input from a user can also be used to form a condition. A user of the program can be asked to enter Student_marks that can be used inside an if statement.

The input function gets the input from the user. It saves the input as a string. We use int(input()) to get an integer from the string. If we execute the aforementioned program and input marks are greater than 100. We get a warning “ Marks exceed 100 “. If the entered marks are 100 or less than no warning message is displayed.

Else

The else statement is always accompanied by an accompanying if statement (if-else). The syntax of if-else is given below.

if(Condition):

Indented statement(s) when the condition is True.

else:

Indented statement when the condition is False. In our previous examples on student marks, let us display the message “Outstanding” if Student_marks are 80 or greater. A message “Not outstanding” is displayed otherwise.

The flowchart presents of an if-else statements. It can be observed that one of two block of code will be executed based upon the condition to be evaluated.

Nested Decisions

To make decisions under multiple conditions, python allows us to perform nested decisions. The if-el if-else statements are employed to accomplish nested decisions. Continuing with the example of student marks. If the marks of a student are greater than 100 or less than 0, a warning should be displayed. Additionally if the obtained marks are 80 or greater, outstanding should be displayed. Otherwise not outstanding should be displayed.

We have used a logical operator or to combine two conditions together inside the if statement.

The flow chart of an if-elif-else statements. It can be observed that one block of code will be executed based on the condition to be evaluated.

Iterations

  • For Loop
  • While Loop
  • Nested Loop

For Loop

Iteration statements provided to us by python allows us to perform a task more than once. A for loop is used to iterate a task fixed number of times. For loop has a definite beginning and end. We provide a sequence or a variable to the loop as an input that causes the loop to execute a fixed number of times.

for loop_variable in sequence:

Statement to be executed in the for loop.

The statements to be executed in the body of the for loop are indented. The loop_variable is used inside the for loops and the number of times the for loop runs depends upon the length of the sequence.

Subjects = [ “Probability” , “Statistics” , “Machine Learning” , “Data Science” , “Artificial Intelligence”]

for k in subjects:

print(k)

In this example, “subjects” is a variable containing five items.

This is used to decide the number of iterations of a for loop.

The loop runs five times because the number of items in the

variable subjects is five. The function range() is normally used in a for loop to generate

a sequence of numbers. For example, range(5) generates

numbers from 0 to 4 (five numbers).

for k in range(5):

Print(k)

We can also specify a step size other than 1 within the range

() function as follows.

In range(3, 12, 3), 3 is the step size. The statements break
and continue are sometimes used inside the loops. The break

statement discontinues the execution of the loop. The continue statement skips all the statements of the for loop following the continue statement.

The name Yash is not printed in the output because the continue statement is executed when the value of k equals Yash. Note that print(k) statement is not indented with the if statement. Thus, it is not part of the if statement. Moreover the code breaks right after it has printed the name Ram.

While Loop

The while loop iteratively runs certain statements until its condition is fulfilled. The syntax of the while loop is given below.

while(Condition):

Statement to be executed in the while loop. If you want to add natural numbers up to the number input by a user, we use a while loop as follows.

The user of this program is asked to input a natural number upon execution of this program. A sum of 10 is obtained if the number 4 is entered and a sum of 55 is returned upon entering the number 10. The while loop allows us to use the break, continue and else statements inside a while loop like a for loop.

Nested Loop

A loop either for or while can be used inside another loop. This is known as nested loops that can be used when we work with the data in two dimensions. This program uses two for loops one nested inside another to print all the combinations of two variables.

Conclusion

Finally, we completing the fundamentals of conditional statements and iteration using python and in this entire walk-in through guide we will snatched out the hidden concepts and also covering importance of loops and conditional statements in any programming paradigm.

--

--

A.I Hub
A.I Hub

Written by A.I Hub

We writes about Data Science | Software Development | Machine Learning | Artificial Intelligence | Ethical Hacking and much more. Unleash your potential with us

No responses yet