0% found this document useful (0 votes)
11 views20 pages

Conditions Loops

Uploaded by

hanyeelovesgod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views20 pages

Conditions Loops

Uploaded by

hanyeelovesgod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

035.

001 Spring, 2024

Digital Computer Concept and Practice


Conditional Statements and 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)’.

2) Series of independent IF statements => All conditions of interest should be run.


>> Syntax : if conditional_test 1 :
#action 1
if conditional_test 2 :
#action 2
IF statements - Example
IF statements – Forms (con’t)
3) IF-ELSE statement => One of two possible actions always needs to be executed.
>> Syntax : S

if conditional_test 1 : Condition 1 Action 1


#action 1
else :
else Action 2
#action 2
#action 3 Action 3

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

if conditional_test 1 : Condition 1 Action 1


#action 1
elif conditional_test 2 : Condition 2 Action 2
#action 2
else :
#action 3 else Action 3
#action 4 Action 4
In-class exercise :
1. How many hours do you sleep in a day?
=> Define it as sleep_hr variable
2. How many cups of coffee do you drink in a day?
=> Define it as coffee_num variable
3. What will be printed with the if statement?
IF statements – Role of a list as conditions
 Conditional statements can be defined with a list.
• empty list == False boolean variable, denoted as 0 S
In-class exercise :
Draw a flowchart for below codes.
Loop
 Loop : Iterations until a terminal condition is reached.
 Type & Condition & Usage :  Basic flowchart : S

Loop Type For While


Initialization
Terminal The end of False
Condition a given sequence boolean variable
Update Loop
Already know Do not know
General variable Terminal Condition
the number of the number of
Usage
iterations iterations

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.

>> The range() function :


Returns a sequence of numbers from 0 (inclusive)
to a specified number (exclusive),
with increment 1.
E.g. : range(len(pets)) = [0, 1, 2]
For loop – Controlling statements
 Break : Terminate a loop immediately when it is encountered.
 Continue : Jump to the next iteration within a loop.
>> Syntax : for itr_var in sequence :
#action 1
if conditional_test 1 :
#action 2
break
elif conditional_test 2 :
#action 3
continue
else :
#action 4
#action 5
For loop – Controlling statements (con’t)
 Example for Break  Example for Continue
While loop - Introduction
 While loop : Iterations as long as conditional statement is true.
>> Syntax :
while conditional_test : • Boolean expression
• Non-empty sequences of list, tuple, string etc.
#action 1
#action 2

 Infinite While loop : The loop running for infinite times.


>> How to avoid it? : Track the update of temporary iteration variables.
>> How to stop it? : Press [c] key while holding down [Ctrl] key.
While loop - Example
In-Class Exercise :
Q1. What are the results of executing code (a) and (b), respectively?
Q2. How are the results different? Why?
(a) (b)
While loop – Example (con’t)
In-Class
 loop can: be executed by a list.
WhileExercise
Q1. What is the result of executing the While loop?
Q2. Report the final status of two lists (unconfirmed_users, confirmed_users)

• Built-in function of a list


>> The title() function :
Changes the initial character
in each word to uppercase.
While loop – Controlling statements
 Break : Terminate a loop immediately when it is encountered.
 Continue : Jump to the next iteration within a loop.
>> Syntax : while conditional_test :
#action 1
#action 2
continue
#action 3
break
else : (#optional)
#action 4
#action 5
While loop – Controlling statements (con’t)
 Example for Break  Example for Continue
In-Class exercises 1 & 2 : Mixture of IF and Loop
Details will be given during the lecture !
Take-home points (THPs)
-
-
-
…

You might also like