Lect 1.4 If Else
Lect 1.4 If Else
Decision making is the most important aspect of almost all the programming languages. As the name
implies, decision making allows us to run a particular block of code for a particular decision. Here, the decisions are
made on the validity of the particular conditions. Condition checking is the backbone of decision making.
In python, decision making is performed by the following statements.
The if statement
The if statement is used to test a particular condition and if the condition is true, it executes a block of code known
as if-block. The condition of if statement can be any valid logical expression which can be either evaluated to true or
false.
Python indentation
Python indentation uses to define the block of the code. The other programming languages such as C, C++, and Java
use curly braces {}, whereas Python uses an indentation. Whitespaces are used as indentation in Python.
Indentation uses at the beginning of the code and ends with the unintended line. That same line indentation defines
the block of the code (body of a function, loop, etc.)
Generally, four whitespaces are used as the indentation. The amount of indentation depends on user, but it must be
consistent throughout that block.
n = int(input("Enter the number:"))
if(n%2 == 0):
print("Even Number")
else:
print("Odd Number")
print("Task Complete")
The above code, if and else are two separate code blocks. Both code blocks are indented four spaces. The
print("Task Complete") statement is not indented four whitespaces and it is out of the if-else block.
If the indentation is not used properly, then that will result in IndentationError.