Courses/Computer Science/CPSC 203/CPSC 203 Template/Labs Template/Problem Solving Week 1 - Lab 2

Introduction
Today's tutorial continues to build on the basics of programming, introduced in the last tutorial. After completing this module, you should be comfortable in demonstrating the following skills:
 * using loops
 * using conditions

Additional Reading
(none)

For Loop
We will look at an example of "looping", which is a technique to enumerate through a list of data repeatedly.

The for statement begins with a header line that specifies an assignment target or targets, along with an object you want to step through. The header is followed by a block of indented statements which you want to repeat.

The general format of a for statement looks like the following:

for target in object: statements else: statements

It assigns items in the sequence object to the target, one by one, and runs the loop body for each. The loop body typically uses the assignment target to refer to the current item in the sequence as if it were a cursor stepping through the sequence.

Example 1: the following code will add the sum of numbers from 1 to 4:

sum = 0 for x in [1, 2, 3, 4]: sum = sum + x

Example 2: Loops through the list having the numbers 1, 3, 6, and 9, and calculates and prints their product

def looping: product = 1 for x in [1, 3, 6, 9]: product = product * x      print "Current product = ", product print "Final product = ", product

Example 3: Loops through the numbers from 6 to 16 and print the result of dividing each number by 2

def looping2: for x in range(6,16): print x/2.0

Example 4: Loops through the numbers from 4 to 1, while subtracting 1 in every iteration

def looping: for i in range(4,1,-1): print i

While Loop
We will look at another example of "looping", through the while loop

The for statement begins with a header line that specifies an assignment target or targets, along with an object you want to step through. The header is followed by a block of indented statements which you want to repeat.

The general format of a while loop looks like the following:

while boolean-condition: body-of-while

body-of-while is repeated as long as boolean condition is true.

Example 5: Displays the number starting from 5 descendingly, while the displayed number is greater than zero

def looping: i = 5 while i > 0: print i      i -= 1

Conditions
Similar to the concepts learned in Excel and Access, you will learn to implement an if condition.

The if statement selects actions to perform. The if statement may contain other statements, including other if statements. The if statement can be followed by one or more optional elif statements and ends with an optional else block.

The general format of an if looks like the following:

if test1 statements1 elif test2 statements2 else statements3

Different operators can be used in the condition of an if statement:
 * == 	equal
 * !=	not equal
 * <	less than
 * <= 	less than or equal
 * >	greater than
 * >= 	greater than or equal
 * and
 * or
 * not

Example 1: In the code below a test to determine the response to what the conditions of the weather is outside. If the weather is sunny then the program will output the response "nice weather". Else if the weather is raining, then the output will be "bad weather". Finally, if the weather is not sunny or raining the program will output "Uncertain, don't plan anything":

weather = 'sunny' if weather == 'sunny': print "Nice weather" elif weather == 'raining': print "Bad weather" else: print "Uncertain, don't plan anything"

Example 2: The code below checks whether x is less than or greater than zero def Try_if(x): if x< 0: print x, "is less than zero" elif x == 0: print x, "is equal to zero" else: print x, "is not less than zero"