IF Statement is one of the most helpful and highly used statements for the decision-making purpose after checking for the tests. They aren't meant for performing operations, instead they are primarily used for decision making. And, once the decision is made, a pre-defined code will get executed. That execution could be an operation or another decision-making statement.
The condition in the IF statement can only be either True or False. Either it will pass the test or it will not. Often there is a choice of two possibilities, only one of which will be executed, depending upon the test result.
Note: The test can be a comparison test or a logical test.
IF Statement
The first two lines are assigning the value to variables a & b. The next two lines are an IF statement- out of which the first line represents the comparison test and the second intended line gets executed if the first line passes the comparison test after referring to the value of a. If the comparison test fails, then it will skip the intended line.
In our example, the choice is between executing something (if it passes the comparison test) or doing nothing (if it fails the comparison test). Since it passes the comparison test (where a is actually equal to 3), it will execute the print command.
IF-ELSE Statement
The first two lines are assigning the value to a and b variables. The next four lines are IF-ELSE statements- out of which the first line represents the comparison test and the second intended line gets executed if the first line passes the comparison test after referring to the values of a and b. If the comparison test fails, then it will skip the intended line and execute whatever is specified in the ELSE statement.
In our example, the choice is between executing either the first intended line (if it passes the comparison test) or the second intended line (if it fails the comparison test). Since it fails the comparison test (where a is actually not equal to 5), it will refer to the code under the ELSE statement and execute the print command.
Multiple IF-ELSE Statements using ELIF Statement
The first two lines are assigning the value to a and b variables. The next six lines are an IF-ELSE statement- out of which the first line represents the comparison test and the second intended line gets executed if it passes the first comparison test after referring to the values of a and b. If it fails the first comparison test, then it will skip the intended line and the next two lines- out of which the first line represents the comparison test and the second intended line gets executed if it passes the first comparison test after referring to the values of a and b. If it fails the first comparison test, then it will again skip the intended line and execute whatever is specified in the ELSE statement.
However, in this example, the choice is between executing either the first intended line (if it passes the first comparison test) or the second intended line (if it fails the first comparison test but passes the second comparison test) or the third intended line (if it fails both- first and the second comparison test). Since it fails both the comparison test under IF statements and ELIF statement (where a is neither equal to 3 or 5), it will refer to the code under the ELSE statement and execute the print command.
Comments