Under certain conditions, some decisions are sometimes in normal life inevitable, as we can see in our photo. It’s the same for every program, which has to solve some useful problem. There is hardly a way to program without having branches in the flow of code.
In programming and scripting languages, conditional statements or conditional constructs are used to perform different computations or actions depending on whether a condition evaluates to true or false. (Please note that true and false are always written as True and False in Python.)
The condition usually uses comparisons and arithmetic expressions with variables. These expressions are evaluated to the Boolean values True or False. The statements for the decision taking are called conditional statements, alternatively, they are also known as conditional expressions or conditional constructs.
The if-then construct (sometimes called if-then-else) is common across many programming languages, but the syntax varies from language to language.
In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Conditional statements give us this ability. The simplest form is the if statement, which has the general form:
if BOOLEAN EXPRESSION: STATEMENTS
A few important things to note about if
statements:
:
) is significant and required. It separates the header of the compound statement from the body.Here is an example:
food = 'spam' if food == 'spam': print('Ummmm, my favorite!') print('I feel like saying it 100 times...') print(100 * (food + '! '))
The boolean expression after the if
statement is called the condition. If it is true, then all the indented statements get executed. What happens if the condition is false, and food
is not equal to 'spam'
? In a simple if
statement like this, nothing happens, and the program continues on to the next statement.
Unfortunately it is not as easy in real life as it is in Python to differentiate between true and false:
The following objects are evaluated by Python as False:
All other values are considered to be True.
C programmers usually know the following abbreviated notation for the if construct:
max = (a > b) ? a : b;
This is an abbreviation for the following C code:
if (a > b) max=a; else max=b;
C programmers have to get used to a different notation in Python:
max = a if (a > b) else b;
You can have if
statements inside if
statements, this is called nested if
statements.
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
if
statements cannot be empty, but if you for some reason have an if
statement with no content put in the pass
statement to avoid getting an error.
a = 33
b = 200
if b > a:
pass
In this article, We studied what are conditional statements how to use them with examples.
This brings the end of this Blog. We really appreciate your time.
Hope you liked it.
Do visit our page www.zigya.com/blog for more informative blogs on Data Science
Keep Reading! Cheers!
Zigya Academy
BEING RELEVANT
Through the standard form offers different advantages in mathematical calculations and scientific notation. Firstly, it…
Introduction Stress is a feeling caused by an external trigger that makes us frustrated, such…
Sociology is a broad discipline that examines societal issues. It looks at the meaningful patterns…
Some info about Inch Inches are a unique measure that persuades us that even the…
You should be familiar with logarithms to understand antilogarithms in a better manner. Logarithms involve…
यहां "नाटककार सुरेंद्र वर्मा" पुस्तक की पीडीएफ विद्यार्थी, शोधार्थी और जो इसका अभ्यास के लिए…