SmithLogo

CSC 111

Introduction to Computer Science Through Programming

Smith Computer Science



Lecture Notes 07: Conditionals and Operators





The IF statement

An If block is a piece of code that will be executed only when a condition (a boolean expression) is evaluated as True.

This is what it looks like:

if <condition> :
    <Block To Execute>


Notice the main parts:



An Example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
# before the if
x = 2

# if statement
if x > 0:
  print ('positive') # if body

# after the if
print ('done')


Comparison Operators



The condition inside the if statement x < 2 can be read as:
IF x has a value that is smaller than 2, resolve to True; False otherwise.

The basic numeric comparison operators are:


Also, you'll notice that in the example, the if statement is sandwitched between two statements.

Activity 1 [2 minutes]:
What is the order of operations executed in this code?
(Wait; then Click)

In this case, it is simple: 2, 5, 6, 9


What is the order of operations if we change the value assigned to x to -1?
(Wait; then Click)

It skips the body!!: 2, 5, 9



Equality



Asking the question:
Is this equal to that is tricky, since we are already using the equals sign (  =   ) to indicate an assignment.

This is why we need to have a different way of checking for equality.

The symbols we use to ask for equality are the double equal signs (  ==   ).

For example:
# before the if
x = 2

# if statement
if x == 2:
  print ('it is a two') # if body

# after the if
print ('done')


Activity 2 [2 minutes]:
What hapens if we use a single equals for the comparison?


Difference



Asking the question:
Is this different to that uses the sign (  !=   ).

Example:

# before the if
x = 2

# if statement
if x != 0:
    print ('x is not zero')

# after the if
print ('done')


Activity 3 [2 minutes]:
Write an example, using the scratch pad, where:
  1. The program asks for an input integer
  2. The program indicates whether the integer is positive, negative, or Zero


Complex expressions



The condition can be anything that evaluates to True or False.

For example, the following is a valid condition:

# before the if
x = 6

# complex condition
if x%2 == 0:
    print ('x is divisible by 2')

# after the if
print ('done')




The IF-ELSE statement

In the example above, we have three options that can be executed.

The nature of the problem makes it so that one of them always is (a number has to be <0, 0, or >= than 0).

However, there are problems where a sequence of if statements might not be ideal, because there is always a default option: none of them get executed!

For example:

# before the if
x = 2
y = 2

# if statement
if x < y:
  print ('x is smallest')

# another (independent) if statement
if x > y:
  print ('y is smallest')

# after the if
print ('done')




If we want one of two options to be executed, we can use an IF-Else statement.

This is what it looks like:
if <condition> :
    <Block To Execute>
else:
    <Alternative Block To Execute>


Notice that the else has no condition.
It is the alternative that gets ecxecuted when the IF block is not.

This is an example of how to use it:

# before the if
x = 2
y = 2

# if-else statement
if x < y:
  print ('x is smallest')
else:
  print ('y is smaller or equal than x')

# after the if
print ('done')


Activity 4 [2 minutes]:
Write an example, using the scratch pad, where:
  1. The program asks for an input integer
  2. The program indicates whether the integer is greater than zero or not




The IF-ELIF-ELSE statement

What happens when we want alternative paths, each with its own condition?

It turns out that you can combine IF and IF-ELSE statements to do anything, but to make things easier,
Python provides the if-elif-else block:

if <condition> :
    <Block To Execute>
elif <condition>:
    <Another Block To Execute>
else:
    <Default Block To Execute>


An example of how to use it:

# before the if
x = 2
y = 2

# if-elif-else statement
if x < y:
    print ('x is smallest')
elif x > y:
    print ('y is smallest')
else:
    print ('x and y are equal')

# after the if
print ('done')


Activity 5 [2 minutes]:
Try it out in the scratch pad
  1. Then modify it to display who the greatest is




Scope and IF-blocks

A variable's scope is that of the innermost function, class or module in which they're assigned.

The IF/ELIF/ELSE block does not establish its own scope (This is different than in Java or C!).

Example:

x = -2

if x<0:
  y = x+1
  print('y is the next number:',y)
    
print(x)
print(y)


Try it out




Cascading If statements

Sometimes, we want sub-conditions to be true before we perform an action.

Think of the following situation:

A professor issues two assignments X and Y.
And the rules for grading are:

  1. If students flunk X (a passing grade is 60), they need a grade of 80 or above in Y to pass the class (C+)
  2. .
  3. If students pass X, and if they also get 80 or above in Y, students get a final grade of A (otherwise C+).

Activity 6 [4 minutes]:
How would we code this using the if blocks (of any kind) seen above?



The following is an alternative:

We can have conditional statements INSIDE conditional statements

For example, you could have this:
# before the if
x = 6

# a "nested" conditional
if x%2 == 0:
    print ('x is divisible by 2')
    if x%3 == 0:
        print ('x is divisible by 6')

# after the if
print ('done')


Let's see a diagram of the solution to the problem shown above:



Activity 7 [2 minutes]:
Write the code for the logic shown in the diagram. Use the scratch pad.




Forgotten Expressions

I forgot to show you some "shorthand" for some arithmetic operations:




Homework

[Due for everyone]
Homework 01 is due on Friday 02/11 at 5PM

[Optional] TODO