Decision Making Statement.

Conditional Statement

Decision making of an anticipation of condition occuring by execution of program and dpecific action is taken according to condtion.

Following is a general form of typical decision making structure found in most programming language.

Python programming language uses any non-zero or non-null value as true and if it is either zero or null then it is assume false value.

if Condition:

In python the block of statement is executed if the condition is true.

Syntax if (condition): Block(s) Example var1=100 if var1: print('i got an true value') print(var1) output i got an true value 100

Question: Write a program that prompt user to enter two integer values, print the message "equal" if both entered value are equal

a=int(input("Enter first number")) b=int(input("Enter second number")) if a==b: print("equal") output Enter first number 23 Enter second number 23 equal

if-else Condition:

The particular block of statement is executed if the if-condtion is true , else it will execute other block of statement.

Syntax if (condition): Block(s) else: Block(s)

Question: Write a program that prompt user to enter two integer values, print the message if one value is greater than other else vece-versa.

a=int(input("Enter first number")) b=int(input("Enter second number")) if a>b: print(a," is grreater") else: print(b," is grreater") output Enter first number 12 Enter second number 34 34 is grreater

if-elif Condition:

The particular block of statement is executed if the if-condtion is true , else it will execute other block of statement.

Syntax if (condition): Block(s) elif (condition): Block(s) else: Block(s)

Core python does not provide SWITCH statement as in other languages but we can use if-elif statement to simulate switch-case as follow:

if (condition): Block(s) elif (condition): Block(s) elif (condition): Block(s) elif (condition): Block(s) . . . . . . . . . . . . . . else: Block(s)

Question: Write a program to prompt a user to read the subject of 5, claculate total marks and % of the marks and display the grade based on given in table.



SUB1=int(input("Enter Subject 1 marks ")) SUB2=int(input("Enter Subject 2 marks ")) SUB3=int(input("Enter Subject 3 marks ")) SUB4=int(input("Enter Subject 4 marks ")) SUB5=int(input("Enter Subject 5 marks ")) total=(SUB1+SUB2+SUB3+SUB4+SUB5)/5 print(total) if total>=90: print("grade A") elif total>=80 and total <=90: print("grade B") elif total>=70 and total <=80: print("grade B") else: print("sit at home") output Enter Subject 1 marks 100 Enter Subject 2 marks 100 Enter Subject 3 marks 100 Enter Subject 4 marks 100 Enter Subject 5 marks 100 100.0 grade A

Nested if Statement:

When a programmar write one if statement inside another then it is called as Nested if statement.

Syntax if (condition): Block(s) if (condition): Block(s) elif (condition): Block(s) else: Block(s) elif (condition): Block(s) else: Block(s) Example var=int(input("enter a number ")) if var<200: if var==150: print("number is 150") elif var==100: print("number is 100") else: print("number is less then 200") else: print("number is more than 200") output enter a number 140 number is less then 200

Comments

Popular posts from this blog

Introduction to Python.