DEV Community

Cover image for A Study on Conditional Statements and Control Flow in Python
Vinayagam
Vinayagam

Posted on

A Study on Conditional Statements and Control Flow in Python

Introduction

In programming, we don’t just write code line by line. Sometimes we need to make decisions and sometimes we need to repeat the same task multiple times. For this, Python provides control flow statements and loops.

Before writing code, it is important to understand how to think. My trainer explained that we should not directly jump to the final output. Instead, we should think step by step and solve the problem gradually.


Basic Programming Approach

While learning programming, I understood some important points:

  • Always try, even if the answer is not clear
  • Start from what you already know
  • Do not think about the full problem at once
  • Solve one step at a time
  • Use variables properly
  • Follow a small step to big step approach

This method helps in writing correct logic.


Conditional Statements

Conditional statements are used to make decisions in a program.

Example:

no1 = 10
no2 = 20

if no1 > no2:
    print(no1)
else:
    print(no2)
Enter fullscreen mode Exit fullscreen mode

In this example, the program checks whether no1 is greater than no2.
If the condition is true, it prints no1. Otherwise, it prints no2.

So the output will be:

20
Enter fullscreen mode Exit fullscreen mode

Control Flow

Control flow means how the program executes.

Normally, Python runs code from top to bottom. But using conditions and loops, we can change the flow of execution. The program can skip some parts or repeat some parts.


While Loop

A loop is used when we want to repeat something.

The while loop runs until a condition becomes false.

Example:

count = 1

while count <= 5:
    print(1, end=' ')
    count = count + 1
Enter fullscreen mode Exit fullscreen mode

Output:

1 1 1 1 1
Enter fullscreen mode Exit fullscreen mode

Here, the loop runs 5 times and prints 1 each time.


Printing Numbers Using Loop

Even Numbers:

count = 1

while count <= 5:
    print(count * 2, end=' ')
    count = count + 1
Enter fullscreen mode Exit fullscreen mode

Output:

2 4 6 8 10
Enter fullscreen mode Exit fullscreen mode

Multiples of 3:

count = 1

while count <= 5:
    print(count * 3, end=' ')
    count = count + 1
Enter fullscreen mode Exit fullscreen mode

Output:

3 6 9 12 15
Enter fullscreen mode Exit fullscreen mode

Increment in Python

In some languages like C or Java, we use ++ or --.
But in Python, these operators are not used.

Instead, we write:

count = count + 1
Enter fullscreen mode Exit fullscreen mode

or

count += 1
Enter fullscreen mode Exit fullscreen mode

Real Life Example

Saving Money:

bag = 0
day = 1

while day <= 10:
    bag = bag + 5
    print("Day", day, "-->", bag, "Rs")
    day = day + 1
Enter fullscreen mode Exit fullscreen mode

This program shows how money increases when we save 5 rupees every day.


Multiplication Table

no1 = 1
no2 = 3
count = 1

while count <= 20:
    print(no1, "* 3 =", no2)
    no1 = no1 + 1
    no2 = no2 + 3
    count = count + 1
Enter fullscreen mode Exit fullscreen mode

This prints the multiplication table of 3.

Better Version:

no1 = 1

while no1 <= 20:
    print(no1, "* 3 =", no1 * 3)
    no1 += 1
Enter fullscreen mode Exit fullscreen mode

This version is easier to understand and cleaner.


Reverse Example

no = 25

while no >= 5:
    print(no)
    no = no - 5
Enter fullscreen mode Exit fullscreen mode

Output:

25
20
15
10
5
Enter fullscreen mode Exit fullscreen mode

Print Features

Using end:

print("Hi", "Hello", "Good Evening", end='*')
Enter fullscreen mode Exit fullscreen mode

Output:

Hi Hello Good Evening*
Enter fullscreen mode Exit fullscreen mode

Using sep:

print("Hi", "Hello", "Good Evening", sep='*')
Enter fullscreen mode Exit fullscreen mode

Output:

Hi*Hello*Good Evening
Enter fullscreen mode Exit fullscreen mode

String Multiplication:

print(3 * "hello")
Enter fullscreen mode Exit fullscreen mode

Output:

hellohellohello
Enter fullscreen mode Exit fullscreen mode

Top comments (0)