Conditions Loops
Conditions Loops
Soohyun Yang
College of Engineering
Department of Civil and Environmental Engineering
IF statements – Introduction & Forms
Aim : To examine the current state of a program and respond appropriately
to that state.
Four basic forms
1) A simple IF statement => One test gives one action.
>> Syntax : if conditional_test 1 : Do not forget ‘colon (:)’ and
v #action 1 ‘indentation (use the Tab key)’.
In-class exercise :
What will be printed when you define the variable
coffee_order as ‘No’?
IF statements – Forms (con’t)
4) IF-ELIF-ELSE statement => More than two possible conditions should be tested.
>> Syntax : S
Statement 1
Statement 2
For loop - Introduction
For loop : Iterations over any sequences such as list, tuple, dictionary.
>> Syntax :
for itr_var in sequence : • Using a temporary iteration variable (e.g., itr_var)
enhances the readability of your codes.
#action 1 • The For loop terminates,
#action 2 when we reach the last element in the sequence.
Advantages
• Shorten your codes to iterate the same actions with every item in a sequence.
• Minimize your code revision when iterative conditions should be changed.
For loop - Example
Example : Iteration over a list
In-class exercise :
What are the results of executing above codes?
For loop – Example (con’t)
For loop can be iterated by the index of elements in the sequence.