0% found this document useful (0 votes)
5 views10 pages

1

The document discusses the implementation of decision-making in programming languages, focusing on conditional constructs such as if, elif, else, and nested if statements. It provides various examples and syntax for using these constructs to evaluate conditions and execute code accordingly. Additionally, it covers shorthand if-else statements, indentation in Python, and includes lab activities with solutions for practical understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

1

The document discusses the implementation of decision-making in programming languages, focusing on conditional constructs such as if, elif, else, and nested if statements. It provides various examples and syntax for using these constructs to evaluate conditions and execute code accordingly. Additionally, it covers shorthand if-else statements, indentation in Python, and includes lab activities with solutions for practical understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Implementing decision making is the most important aspect of almost all the

programming languages. The decisions are made on the evaluated result of the
conditions.

Conditional constructs allow the selective execution of statements, depending on


the value of expression associated with them.

The comparison operators are required for evaluating the conditions.

The various conditional constructs are:

If
Elif
Else
Nested if

Short hand if
Short hand if …else

If Statement:
--------------

If certain conditions are true we direct the program to do one course of actions.

If certain conditions are false we direct the program to do some other course of
action.

Syntax:
--------

if cond:
#Statements to be executed if the condition is true

Programs:
----------

1. program to check if the number is positive:

a=10
if a>0:
print("The number is positive")

--------------------------------------------------

2. Program to check if a person is eligible for voting

age=21
if age>=18:
print("Person is eligible for voting")
But there can be cases when the condition is False.

==================================================

2. if-else:
------------

3. program to check if the number is positive or not:

a=10
if a>0:
print("The number is positive")
else:
print("Negative")

------------------------------------

4. Program to check if a person is eligible or not eligible for voting

age=16
if age>=18:
print("Person is eligible for voting")
else:
print("Person is not eligible")

--------------------------------------------------

5. Pgm to compare two values based on a condition and display the greater one.

a=4
b=9
if a>b:
print("a is positive")
else:
print("b is positive")

------------------------------------------------------------

a and b are variables, so in order to replace them using the values, we use format
strings.

In format strings, the print function starts with f and the variables are written
inside the curly braces.

a=4
b=9
if a>b:
print(f"{a} is positive")
else:
print(f"{b} is positive")

Now, instead of taking numbers directly, we can take numbers from user, so use
input() function to take input from user and the input must be converted into its
respective datatype.

a=int(input("Enter num1"))
b=int(input("Enter num2"))
if a>b:
print(f"{a} is positive")
else:
print(f"{b} is positive")

----------------------------------------------------
6. Write a program to validate a password.

password=input("enter password:")
if password=="123":
print("password is valid")
else:
print("password is invalid, try again..!")

taking multiple values as input:


-------------------------------

a, b = input("Enter two numbers: ").split()


print(f"a = {a}, b = {b}")

a, b = map(int, input("Enter two numbers: ").split()) #convert to int

Inorder to take list as input:

numbers = list(map(int, input("Enter numbers separated by space: ").split()))


print(numbers)
-----------------------------------------------------

Indentation in Python:
-----------------------

Python doesn't allow the use of parentheses for the block level code.

The block representation can be implemented using indentation.

For the ease of programming and to achieve simplicity, indentation is used to


declare a block.
If two statements are at the same indentation level, then the interpreter considers
that they are the part of the same block.
By default four spaces are given to indent the statements but it can be increased
or decreased by the programmer.

The statements with in the same indentation block is called as a suite. All the
statements of one block are intended at the same level indentation.

==========================================================

Short hand if else:


---------------------

If we want to write the codes in a single line for readability and to make it
concise, we shall use short hand if else.

syntax:
--------

code_if_cond_is_true if cond else code_if_cond_false

7. Write a program using short hand if else to find the greater number among two.

num1=int(input("Enter number 1:")


num2=int(input("Enter number 2:")
print("Num is greater") if num1>num2 else print("Num 2 is greater")

------------------------------------------

8. Write a program using short hand if else to validate a password.

password=input("enter password:")
print("password is valid") if password=="123" else print("password is invalid, try
again..!")

===============================================

3. Nested if:
--------------

It is the implementation of using an if else block in another if block.

When we have a condition that depends on another condition, we use nested if.

9. validating username and password

username=input("enter username:")
password=input("enter password:")
if username=="admin":
print("account is existing")
if password=="abc":
print("welcome")
else:
print("Incorrect password")

else:
print("account doesnt exist")
print("The username is invalid")

----------------------------------------------

10. #comparing 3 numbers.

a=int(input("Enter Num1")) # 12
b=int(input("Enter Num2")) # 10
c=int(input("Enter Num3")) # 18
if a>b:
if a>c:
print(f"{a} is greater")
else:
print(f"{c} is greater")
elif b>c:
print(f"{b} is greater")
else:
print(f"{c} is greater")

===================================================

#Elif:
------

The elif keyword

The elif keyword in Python is short for "else if".

if...elif...else constructs to check additional conditions after an initial if


condition has been evaluated as False.

else statement is optional


If none of the if or elif conditions are True, the code block associated with the
else statement (if present) is executed.

11. Program to compare 2 numbers with multiple if:


---------------------------------------------------

a=10
b=8
if a>b:
print(" A is Gr")
if a<b:
print("B is Gr")
if a==b:
print("A eq B")
if a!=b:
print("A not Eq B")

12. Program to compare 2 numbers with elif:


---------------------------------------------

a=10
b=8
if a>b:
print(f"{a} is greater")
elif a<b:
print(f"{b} is greater")
elif a==b:
print(f"{a} is equal to {b}")
else:
print(f"{a} is not equal to {b}")

13. Grading system using " if ".


---------------------------------

marks=int(input("enter marks"))
if marks>=90:
print("grade A")

if marks>=80:
print("grade B")

if marks>=70:
print("grade C")

if marks>=60:
print("grade D")

if marks>=50:
print("grade E")

if marks<50:
print("Fail")

Here, multiple grades are assigned to the student. lets try to change the code.

14. Modify the above code


---------------------------

marks=int(input("enter marks"))
if marks>=90:
print("grade A")

if marks>=80 and marks<90:


print("grade B")

if marks>=70 and marks<80:


print("grade C")

if marks>=60 and marks<70:


print("grade D")

if marks>=50 and marks<60:


print("grade E")

if marks<50:
print("Fail")

Now we got the correct output, but the prblm here is we need lot of conditions and
it may lead to a confusion while writing conditions. it is a limitation here, so we
use another conditional construct called "elif"

15. using elif:


------------

marks=int(input("enter marks"))
if marks>=90:
print("grade A")

elif marks>=80:
print("grade B")

elif marks>=70:
print("grade C")

elif marks>=60:
print("grade D")

elif marks>=50:
print("grade E")

else:
print("Fail")

========================================================
Lab Activities with Solutions:
--------------------------

1. Write a program to allow the use to enter 5 subject marks to be stored in


subject elements, find sum of all subjects and avg of total.

Evaluate Grade of a student based on the following conditions

marks above 60, you can give a Grade: A


marks above 50, and marks below 60 you can give a Grade: B
marks above 40, and marks below 50 you can give a Grade: C
Otherwise the Grade can be Fail

print("Enter Subject marks")


s1=int(input("Enter Sub1"))
s2=int(input("Enter Sub2"))
s3=int(input("Enter Sub3"))
s4=int(input("Enter Sub4"))
s5=int(input("Enter Sub5"))
sum=s1+s2+s3+s4+s5
avg=sum//5
if avg>=60:
print("Grade A")
print("Marks:",sum)
elif avg>=50 and avg<60:
print("Grade B")
print("Marks:", sum)
elif avg >= 40 and avg < 50:
print("Grade C")
print("Marks:", sum)
else:
print("Grade: Fail")
print("Marks:", sum)

=============================================

2. Program to Evaluate the eligibility of a candidate for a position.

The candidate should get an avg score of 80 in the given test.


For this position only Male are eligible.
The candidate must belongs to Local Region.

score=int(input('Enter ur score'))
gender=input("Enter ur Gender(M/F)")
Region=input("Enter ur Region")

if score>=80 and gender=='M' and Region=="Hyderabad":


print("Eligible")
else:
print("Not eligible")

========================================================
3. Write a code using short hand if else to tell if a person is eligible to vote or
not.

age = 19
status = "Can Vote" if age >= 18 else "Cannot vote"
print(status)

========================================================

4. WAP to read three numbers from user, if any of three numbers are 0, it should
display the number must be more than 0.
The application has to display after comparing the numbers,
"Entered three numbers are Equal” if all three entered numbers are
equal,
"Entered three numbers are different" if all three numbers are
different and it should display
"Neither all are equal or different" otherwise

print("Enter Num1")
n1=int(input())
print("Enter Num2")
n2=int(input())
print("Enter Num3")
n3=int(input())
if n1==0 or n2==0 or n3==0:
print("Values should be non zero")
elif n1==n2 and n1==n3:
print("All Values are equal")
elif n1==n2 or n1==n3 or n3==n2:
print("Neither all are equal or different")
else:
print("All numbers are different")

==========================================================

5. WAP to read three numbers from user and should evaluate whether
those numbers as increasing order or in decreasing order.
The program has to display
"Increasing Order” if numbers are in increasing order
The program has to display
"Decreasing Order” if numbers are in decreasing order
Otherwise, it should display as
"Not in increasing and decreasing order"
Testcase1
Enter number1: 1234
Enter number2: 3456
Enter number3: 5678
Increasing Order
Testcase2
Enter number1: 1234
Enter number2: 1221
Enter number3: 5678
Not in increasing order and not in decreasing order

print("Enter Num1")
n1=int(input())
print("Enter Num2")
n2=int(input())
print("Enter Num3")
n3=int(input())
if n1==0 or n2==0 or n3==0:
print("Values should be non zero")
elif n1<n2 and n2<n3:
print("Increasing Order")
elif n1>n2 and n2>n3:
print("Decreasing Order")
else:
print("Not in increasing and decreasing order")

=================================================================

You might also like