`If` and `else` Statements

This lesson will introduce us to the concept and syntax of `if` and `else` statements in Powershell and Python.

We'll cover the following

In programming, it is often necessary to check some conditions and change the behavior of the program accordingly. So let’s learn about syntax and concepts of if and else statements in Python and PowerShell.

If statement

Conditional statements are part of every programming language. These run or do not run a part of code depending upon the condition defined in the program. Programs that run based on whether those conditions are met are called conditional codes.

Let’s begin with the if statement, which is the first building block of a conditional statement. They are used to evaluate whether a statement was True or False. It only executes the body of the if statement if the condition was True.

Syntax of the if statement in Python:

if expression:
   statement

Let’s take an example in Python where we have a variable that has an integer value assigned to it. We want a different portion of the program to run if and only if the integer value is positive, negative, or zero.

Get hands-on with 1200+ tech skills courses.