Tuesday, July 27, 2021

Python - Day14 - if else

 Day/Session 14: Condition - 7/14/2021

Simple if
1. if lese
<syntax1>
if <condition>:
  st1
else:
  st2


When condition 1 is true it will execute statemetn1 and if fails, executes2.


first control will check the condition, if the condition is true, it will execute statement 1. it will skip the else statement.

if the condition is false, it will skip the if statement and goes to else part and executes else part.


<syntax2>
-------------
if <condition>:
  st1
  st2
else:
  st3
  st4

control will check the condition. if the condition is true it will enter into if condition and executes the statement 1 and statement 2. It skips the else part.
if condition is false, it skips the if part and conrol goes to else part and executes the else part.


eg1.
Write a python program to accept i and j value. Compare j and j values:

Step1: Design with o/p
----------------------

o/p
-----------------------
enter i value: 10
10 you store into i, i=10

enter j value: 5
store 5 int j, j=5

Write a condititon j is greater than i.

-> open your idle
File -> new file

i=int(input("Please enter i value"))
j=int(input("Enter j value"))
if i>j:
  print("i is greater than j")
else:
  print("j is greater than j")


run the program

enter 10, 10 will be stored in i
enter next number 5, stored in j.

next compares
i >j -> 10 > j -> condition is true so
it will execute if block. it skips else block.


There is a chance that user may type i=10 and j=10

what happens?
Since if condition evaluates and it is not true, it will skips the if blocks and goes to else part.

Since we don't have choice to write 3rd option, it will executes the else part.

to resolve this issue, we have to go to if, else, if part.

when do you use simple if?
-> we use simple if when particular condition need to verify.
when we have to c
in simple if, we have only one option. That means, when we have one option based on condition, we use simple if.

When do you use if else?
-> When we have 2 options, we want to execute one option based on condition.

3. if elif else
----------------
<SYNTAX>
---------
if <condition1>:
  st1
elif <condition2>:
  st2
else:
  st3

when you have more than 2 options.

first control will check condition1. if condition is true, it will execute st1 and skip elif and else part.

2nd option:

control checks the condition1 and if condition1 is false, it will skip and goes to elif and checks condition2. If its true, it executes and skips else part.

if elif contition 2 is false, it will skip the condition2 and control goes to else part and executes st3.


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

Eg,
Implement compare i and j using if elif else?
write down all the options

i=int(input("Enter i value:")
j=int(input("Enter j value:")

if i>j:
  print("i is greater than j")
elif:
  print(j is greater than i")
else:
  print("i is equal to j")

run the program:
enter i value: 10 -> 10 will be stored in i
enter j value: 5 -> 5 will stored in j


enter 5 and 10
5 greater than 10 -> skip if
compares elif part
conditon is true, executes
and prints the statemetn 2 and skips the else part

3rd, enter 10 and 10
compares first codition - flase,
end condition: false
executes else part.


you can use elif (3rd condition) but its not necessary. ..
Note: Do'nt use unnecesary conditions.


i=int(input("Enter i value:")
j=int(input("Enter j value:")

if i>j:
  print("i is greater than j")
elif:
  print(j is greater than i")
elif i==j:
  print("i is equal to j")


Q. Implement above program using the requiremet below.

a. user enters 10 and 5
b. User enters 10 and 10 value
c. User enters 5 and 10 values.
Write the code for this condition


Step1: Design with O/p

o/p
---------------------------------
Enter i value: 10 - user enter 10
10 stored in 10

enter j value: 5
user enter 5 and stored in j.

and display
i is greater than j.
j value is : 10

i=int(input("Enter i value:")
j=int(input("Enter j value:")

if i>j:
  print("i is greater than j")
  print(The value of i is:", i)
elif j>i:
  print(j is greater than i")
  print("i value is:",j)
elif i==j:
  print("i is equal to j")
  print("i value us:", i)
  print("j value is:",j)



10 adn 5

10>10
condition is true, executes the statemetn.
skips rest elif and else part

5>10 - false if skip
elif 5>10 -> false and executes

review the code above...


No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...