DEV Community

Cover image for Conditional Statements and Control Flow in Python
Kathirvel S
Kathirvel S

Posted on

Conditional Statements and Control Flow in Python

When we first start learning programming, most beginners immediately jump into writing code. But before writing even a single line, there is something more important:

Thinking.

Programming is not only about typing code.
Programming is about teaching the computer how to think step by step.

A computer is very fast, but it is not intelligent by itself.
It only follows instructions that we give.

So before learning Python syntax, ask yourself:

  • How does a program make decisions?
  • How does a program repeat tasks?
  • How does a computer choose between two options?

This is where Conditional Statements and Control Flow come into the picture.


What is Control Flow?

Control flow means:

The order in which a program executes instructions.

Think of it like traffic on a road.

A program does not run everything at once.
It moves step by step.

Sometimes:

  • it goes forward,
  • sometimes it takes a different path,
  • sometimes it repeats the same path again and again.

Python controls this flow using:

  • if
  • elif
  • else
  • loops like while

These statements help Python make decisions like a human.


Imagine This Real-Life Situation

Suppose three students scored marks:

  • Arun → 15
  • Bala → 20
  • Charles → 45

Now someone asks:

“Who scored the highest marks?”

Before answering, your brain automatically compares all three values.

You think like this:

  • Is Arun greater than Bala and Charles?
  • No.
  • Is Bala greater than Arun and Charles?
  • No.
  • Then Charles must be the highest.

This exact thinking process is what we teach Python.


Conditional Statements in Python

Python uses:

if
elif
else
Enter fullscreen mode Exit fullscreen mode

to make decisions.


Finding the Largest Number

Now let’s convert our thinking into code.

a = 15
b = 20
c = 45

if a > b and a > c:
    print(a)

elif b > a and b > c:
    print(b)

else:
    print(c)
Enter fullscreen mode Exit fullscreen mode

Understanding the Code Slowly

Step 1 — Creating Variables

a = 15
b = 20
c = 45
Enter fullscreen mode Exit fullscreen mode

Variables are containers that store values.

Here:

  • a stores 15
  • b stores 20
  • c stores 45

Step 2 — The if Statement

if a > b and a > c:
Enter fullscreen mode Exit fullscreen mode

Python checks:

  • Is a greater than b?
  • AND
  • Is a greater than c?

The keyword and means:

Both conditions must be True.

Now Python checks:

15 > 20    False
15 > 45    False
Enter fullscreen mode Exit fullscreen mode

Since the condition is False, Python skips this block.


Step 3 — elif Statement

elif b > a and b > c:
Enter fullscreen mode Exit fullscreen mode

elif means:

“Check another condition.”

Now Python checks:

20 > 15    True
20 > 45    False
Enter fullscreen mode Exit fullscreen mode

Again False.

So Python skips this too.


Step 4 — else Statement

else:
    print(c)
Enter fullscreen mode Exit fullscreen mode

else means:

“If nothing above is true, run this.”

So Python prints:

45
Enter fullscreen mode Exit fullscreen mode

Output

45
Enter fullscreen mode Exit fullscreen mode

Why This Program Matters

This small program teaches powerful programming concepts:

  • Decision making
  • Comparison operators
  • Logical thinking
  • Problem solving

This is the foundation of real programming.


Another Example — Comparing Two Numbers

Before coding, think first.

Suppose:

  • a = 10
  • b = 5

Question:

Which number is greater?

Your brain instantly says:

10 is greater.

Now let’s teach Python the same thinking.


a = 10
b = 5

if a > b:
    print(a)

else:
    print(b)
Enter fullscreen mode Exit fullscreen mode

Understanding the Logic

Python checks:

10 > 5
Enter fullscreen mode Exit fullscreen mode

This is True.

So Python executes:

print(a)
Enter fullscreen mode Exit fullscreen mode

Output:

10
Enter fullscreen mode Exit fullscreen mode

Since the condition is True, the else block is ignored.


Truthy and Falsy Values

Now let’s explore something interesting.

Before coding, think carefully:

If I write:

if a:
Enter fullscreen mode Exit fullscreen mode

What is Python checking?

Most beginners get confused here.


The Important Idea

In Python:

  • Some values behave like True
  • Some values behave like False

This is called:

Truthy and Falsy Values


Example

a = -0.1

if a:
    print("a")

else:
    print("b")
Enter fullscreen mode Exit fullscreen mode

What Happens Here?

Python checks:

if -0.1
Enter fullscreen mode Exit fullscreen mode

Since -0.1 is not zero, Python treats it as True.

So output becomes:

a
Enter fullscreen mode Exit fullscreen mode

Important Rule

In Python:

These are considered False:

0
0.0
False
None
''
[]
{}
Enter fullscreen mode Exit fullscreen mode

Everything else is usually True.


Why This Concept Is Powerful

This helps programmers write cleaner and smarter code.

You will see this concept everywhere in real-world Python applications.


Now Let’s Think About Repetition

Imagine printing numbers manually:

print(1)
print(1)
print(1)
print(1)
print(1)
Enter fullscreen mode Exit fullscreen mode

This is boring and repetitive.

A programmer always asks:

“Can I automate this?”

That is why loops exist.


What is a while Loop?

A while loop repeats code while a condition remains True.

Basic syntax:

while condition:
    code
Enter fullscreen mode Exit fullscreen mode

Printing the Same Number Multiple Times

count = 1

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

Think Before Understanding the Loop

Ask yourself:

  • Where does the loop start?
  • When does it stop?
  • What changes inside the loop?

These three questions help you understand any loop in programming.


Step-by-Step Explanation

Step 1 — Initialize Variable

count = 1
Enter fullscreen mode Exit fullscreen mode

This variable controls the loop.


Step 2 — Condition

while count <= 5:
Enter fullscreen mode Exit fullscreen mode

Meaning:

Repeat while count is less than or equal to 5.


Step 3 — Print Statement

print(1, end=' ')
Enter fullscreen mode Exit fullscreen mode

This prints 1.

The end=' ' keeps everything on the same line.


Step 4 — Increment

count = count + 1
Enter fullscreen mode Exit fullscreen mode

This increases the count value.

Without this line, the loop would never stop.


Output

1 1 1 1 1
Enter fullscreen mode Exit fullscreen mode

Printing Numbers from 1 to 5

Now instead of printing the same number, let’s print the changing value of count.

count = 1

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

What Happens Internally?

count Output
1 1
2 2
3 3
4 4
5 5

When count becomes 6:

6 <= 5
Enter fullscreen mode Exit fullscreen mode

This becomes False.

So the loop stops.


Output

1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode

Printing with Addition Logic

Now let’s make the loop smarter.

count = 1

while count <= 5:
    print(count + 5, end=' ')
    count = count + 1

print(count)
print(count * 2)
Enter fullscreen mode Exit fullscreen mode

Thinking Process

Instead of printing count, we print:

count + 5
Enter fullscreen mode Exit fullscreen mode

So Python calculates:

count count + 5
1 6
2 7
3 8
4 9
5 10

Output from Loop

6 7 8 9 10
Enter fullscreen mode Exit fullscreen mode

What Happens After the Loop?

After the loop finishes:

count = 6
Enter fullscreen mode Exit fullscreen mode

Because the loop stops only after count becomes greater than 5.


First Print

print(count)
Enter fullscreen mode Exit fullscreen mode

Output:

6
Enter fullscreen mode Exit fullscreen mode

Second Print

print(count * 2)
Enter fullscreen mode Exit fullscreen mode

Python calculates:

6 * 2 = 12
Enter fullscreen mode Exit fullscreen mode

Output:

12
Enter fullscreen mode Exit fullscreen mode

Final Output

6 7 8 9 10
6
12
Enter fullscreen mode Exit fullscreen mode

Common Beginner Mistakes

1. Forgetting Indentation

Python uses spaces to understand blocks.

Correct:

if a > b:
    print(a)
Enter fullscreen mode Exit fullscreen mode

Wrong:

if a > b:
print(a)
Enter fullscreen mode Exit fullscreen mode

2. Infinite Loops

If you forget:

count = count + 1
Enter fullscreen mode Exit fullscreen mode

the loop runs forever.


3. Confusing = and ==

=    assignment
==   comparison
Enter fullscreen mode Exit fullscreen mode

This mistake is extremely common for beginners.


Final Thoughts

Programming is not about memorizing syntax.

It is about:

  • observing problems,
  • thinking logically,
  • and breaking solutions into steps.

Conditional statements teach computers how to make decisions.

Loops teach computers how to repeat tasks efficiently.

Once you truly understand these concepts, you start thinking like a programmer.

And that is the real beginning of learning Python.

Top comments (0)