Lecture 3: Condition Statements
IF a condition is met, THEN an action is performed OTHERWISE perform another action
Today we’re talking coding and I had one goal in mind. Help kids learn to code with no expensive equipment. No computer, no tablet, no apps, just their bodies, and their mind. So we’re starting with one of the most basic parts of computer programming, the If-Then statement and turning it into a fun and active game.
Computers understand only 2 Things, True and False. If a computer receives 1, it understands it as True, If the computer receives 0 then it is interpreted as False.
The If-Then Statement:
If Then is what’s called a conditional statement in programming. The program queries if one condition exists, then it commands it to do something. It can be as basic as a True or False question and answer or it can prompt action.
Let’s play a game. For every round, there is one Programmer and everyone else is a Computer. The Programmer stands in front of the Computers and gives them his command. If I ____ (fill in the blank), then you _____ (fill in the blank). For example, the Programmer below gave the command “If I turn in a circle, Then you turn in a circle.”
If you understood the thing till here, Let’s proceed it further in such a way that your brain works it out!
Level 1 — If I Do This, Then You Do This:
In this Level, The kid would just tell the other kids to do the same as he does!
If I turn in a circle, Then you turn in a circle.====== IN PSUEDOCODE ======
If(I TURN IN CIRCLE) THEN
YOU ALL TURN IN CIRCLE BEHIND ME====== IN PROGRAMMING======
If("Kashif" speaks 'Turn in circle'):
print("We All are Turning in Circle Behind you")====== LITTLE MORE ADVANCE WITH MATHS ======
If(count == 5):
counta = 5 #Same Value of the Count
Level 2— If I Do This, Then You Do That:
This level adds the twist that the Computers should do something different than the Programmer, but still start and stop when they do
If I raise my right Arm, Then you raise your left Arm====== IN PSUEDOCODE ======
If(Kashif Raise his Right Arm)THEN
Raise Your Left Arm====== IN PROGRAMMING======
If('Kashif' Raise his Right Arm):
print("Raise Your Left Arm")====== LITTLE MORE ADVANCE WITH MATHS ======
If(count == 5):
counta = 10 #See a Different Inner Work!
Level 3— If I Do This, Then You Do That, Else You Do Something Else:
If I raise my right arm, Then you raise your left foot, Else raise your right foot====== IN PSUEDOCODE ======
If(Kashif Raise his Right Arm)THEN
Raise Your Left FOOT
ELSE:
Raise Your Right FOOT====== IN PROGRAMMING======
If('Kashif' Raise his Right Arm):
print("Raise Your Left FOOT")
else:
print("Raise Your Right FOOT")====== LITTLE MORE ADVANCE WITH MATHS ======
If(count == 5):
counta = 10
else:
counta = 20
So if You raise your Right Arm, Computer would raise Left Food or if you just stand there and do nothing, the Computers should all be raising its right foot.
Advancing If-Then Statement(With Programming):
The main logic operations, coder uses to create compound conditional are and
, not
, and or
. Here is what each one means:
and
means that every condition is true.not
changes the Boolean value of a statement: true becomes false, and false becomes true.or
means that at least one of the conditions is true.
IN PSEUDOCODE:
Here is the general pseudocode format of a compound conditional:
if homework is done and it's after 9pm then go to bed
The if
portion of a compound conditional can only return a Boolean value: true or false.
Here are some examples of compound conditionals in pseudocode:
if (weight >= 40 lbs and height >= 38 in) then (use booster seat)
if (not fuse tripped) then (electrical current flows)
if (SAT > 1500 or ACT > 33) then (earn scholarship)
if (Day == 'Saturday' or Day == 'Sunday') then (Its Weekend)
IN PYTHON
For young coders working in Python, compounding logical operators allow for structuring more precise control in their programs. For example, a program may be structured to compute different paychecks for people with different salaries, bonuses, and other variables. The table below shows the logical operators your young coder uses in Python.
Logical Operators in Python:
+-----------+---------------------------------+
| Operator | What It Means |
+-----------+---------------------------------+
| == | equivalent to |
| and | and |
| not | not |
| or | or |
+---------------+-----------------------------+
Examples:
var = 100
if var == 200:
print "1 - Got a true expression value"
print var
elif var == 150:
print "2 - Got a true expression value"
print var
elif var == 100:
print "3 - Got a true expression value"
print var
else:
print "4 - Got a false expression value"
print var
print "Good bye!"x = int(input())
if x > 0:
print(x)
else:
print(-x)
Now Let’s Do it a little Differently 🌝
#A Number is Positive if its Greater than 0 (number > 0)
#A Number is Negative if its Less than 0 (number < 0)x = int(input())
y = int(input())
if x > 0:
if y > 0:
# x is greater than 0, y is greater than 0
print("Quadrant I")
else:
# x is greater than 0, y is less or equal than 0
print("Quadrant IV")
else:
if y > 0:
# x is less or equal than 0, y is greater than 0
print("Quadrant II")
else:
# x is less or equal than 0, y is less or equal than 0
print("Quadrant III")
For any queries, you can ask in comments below!