DEV Community

AravindhBalakrishnan
AravindhBalakrishnan

Posted on

DS - if Condition

Data structure is structured way of storing, organising, managing data on a computer memory so that we can easily organise and perform actions on the data effectively.

To start with DS we can start with if-confition . if condition is like perform choosing between 2 options or performing yes or no question as condition and to perform certain operation based on the outcome of the condition.

To understand we can execute the below line of code

total1 = 345
total2 = 245
if total1 > total2:
     print(total1, "is greater")
else : 
     print(total2, "is greater") 

Output : 345 is greater


lets us change total 2 value to 450

total1 = 345
total2 = 450
if total1 > total2:
     print(total1, "is greater")
else : 
     print(total2, "is greater") 

Output : 450 is greater

Enter fullscreen mode Exit fullscreen mode

This clearly show if the condition is True then first statement in executed. If condition is False then second statement is executed.

We can also use this condition for 3 variable to check what is the greatest of three numbers.

num1 = 35
num2 = 25
num3 = 45
if num1 >= num2 and num1 >= num3:
    print(num1, "is greater than", num2, "and", num3)

elif num2 >= num1 and num2 >= num3:
    print(num2, "is greater than", num1, "and", num3)

else:
    print(num3, "is greater than", num1, "and", num2)
Enter fullscreen mode Exit fullscreen mode

Here we can choose between 3 different numbers using && (and) operator.

Using if and if else condition we can decide to execute a particular block of code based on the True or False of the conditional statement.

Top comments (0)