Image

Anyone Can Code.





Image


Conditionals Syntax



Avi Bhattacharya



Syntax: Conditional constructs are formatted as follows:
if (condition):
    (code to follow if condition(s) is/are met)
elif:
    (alternative code)
else:
    (code to execute if none of the conditions are satisfied)

Indentation

The codes within the if subsection is indented i.e. you will notice when coding yourself that after a conditional statement, the next line will be written slightly ahead instead of directly below the if statement. The indented code is the one which will be followed by the program if the condition is true.

Creatng Conditions

Conditions are dependent on boolean values i.e. True and False. Your condition can use any operators which return a boolean value. Other conditions will not be executed by the interpreter. The major operators you can use in conditions are comparison operators, membership operators, logical operators and identity operators since these return boolean values. You can also use boolean values themselves to create a condition.

You can find all these operators in our Operators Tutorial.

By using these operators, we can check create pre requisites to a certain lines of code. For example, if we only want our output when two values are equal, then we can use these operators to create a conditional statement.


That's it for this tutorial! Click Next to access the next tutorial!

❮ Back

Next ❯









Copyright © 2020, Q-Programming | Created by Arjun Sodhi, Balpreet Juneja and Sammarth Kumar | Developed and Designed by Sammarth Kumar