DEV Community

Cover image for Conditional for beginners
Roghaye Mohammadi
Roghaye Mohammadi

Posted on

Conditional for beginners

Conditional statements, also called if statements, are one of the most important parts of programming. They allow a program to make decisions based on certain conditions. They check if a condition is true or false, and then run different parts of the code based on the result.

For example, if the user enters the correct username, the program will allow access. But if the username is wrong, it will show an error message.

if username =="admin":
print("Acsees granted)
else:
print("Error: wrong username")

There are different kinds of conditional statements, but the most common are if, else if (or elif) and else. The if statement checks a condition, else if checks another condition if the first one is false, and else is used when none of the conditions are true.

Conditional statements are very useful in programming because they make programs smarter and able to handle different situations. Without using conditional statements, a program would follow only one path and would not be flexible.

Top comments (0)