Multiple conditions can be checked using if else if (in C++) and if elif (Python). In the following example, two numbers are compared and three possible conditions are checked. The else is used to check the last or the default condition if all the other conditions have failed.
| C++ | Python | ||
|---|---|---|---|
| Syntax if (condition1) { statement1; statement2; ... } else if (condition2) { statement1; statement2; ... } else { statement1; statement2; ... } |
Syntax if condition1: statement1; statement2; ... elif condition2: statement1; statement2; ... else: statement1; statement2; ... |
||
Example int a = 10; int b = 15; if (a<b) { cout<<"a < b."; } else if (a>b) { cout<<"a > b."; } else { cout<<"a = b."; } | Example a,b = 10, 15; if a<b: print("a < b") elif a>b: print("a > b") else: print("a = b") |
No comments:
Post a Comment