Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
There come situations in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations arise in programming also where we need to make some decisions and based on these decisions we will execute the next block of code. For example, in Python if x occurs then execute y else execute z. There can also be multiple conditions like in Python if x occurs then execute p, else if condition y occurs execute q, else execute r. This condition of Python else-if is one of the many ways of importing multiple conditions.
if statement in Python:
if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
The if statement is the simplest example of a decision structure in Python. In an if statement a logical test is made which can evaluate to either true or false. If the result of the test is true, the statements in the if branch are executed. If the result of the test is false, the statements in the else branch are executed. Once the statements in the appropriate branch have been executed, the flow of execution continues on to the statements that follow the if statement.
if(condition) { // Statements to execute if // condition is true }
Here, condition after evaluation will be either true or false. Python if statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise not. If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default if statement will consider the first immediately below statement to be inside its block.
a = int ( input ( "Enter a number: " ) ) b = int ( input ( "Enter another number: " ) ) if a > b : print ( "First number is greater than second number " )
if-else in Python
The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the Python else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }
a = int ( input ( "Enter a number: " ) ) b = int ( input ( "Enter another number: " ) ) if a > b : print ( "First number is greater than second number " ) else : print ( "Second number is greater than first number " )
Nested if statements
An if statement gives us the ability to ask a single question and do something in response to that question. Often, the logic of a particular situation will dictate that we ask more than one question to resolve a situation. In that case, we may need to make use of a nested if. The following example demonstrates how this works in practice. The program below has the user enter three floating point numbers that form the coefficients of a polynomial p(x) = a x2 + b x + c. The program will then try to determine what kind of roots the polynomial has.
The key decision that has to be made is based on the value of the descriminant, b2 - 4 a c. If that quantity is negative, the polynomial has no real roots. If it 0, the equation has one real root, and if it is greater than 0 there are two distinct real roots. The program below uses a pair of nested if statements to determine which of these three cases we are dealing with.
a = input("Enter a number: ") b = input("Enter another number: ") if a>b: print("First number is greater then Second number") elif a==b: print("Both number is equal") else: print("Second number is greater than First number")
No comments:
Post a Comment