ITEC113 Algorithms and Programming Techniques

advertisement
ITEC113 Algorithms and Programming Techniques
Fall 2010-11
ITEC 113 Algorithms and Programming Techniques
Lecture 3 Loops
Aim:


Understand the need for loops
Lear how to represent different types of loops in flowcharts, pseudocodes and C
Revision
Sequence Statements :
Initialization and assignment statements are considered as sequence statements and they are represented
with PROCESS symbol (Rectangle) in FLOWCHARTs
Flowchart Example
pi  3.14
radius  2
area  pi*radius*radius
Pseudocode Examples
pi  3.14
name  “Cem”
Initialization
color  ‘Y’
birthyear  1969
area  pi x radius x radius
age  2001 - birthyear
profitrate  (benefit - cost) / cost
Input Statements:
Flowchart Example
Pseudocode Example
Input n
n
Output Statements:
Flowchart Example
Pseudocode Examples
Assignment
Statements
2
Display sum
sum
Decision (Selection) Statements :
Flowchart Example
Pseudocode Examples
?
N <= 100
YES
if n<=100
<statements>
else
<statements>
end if
NO
Important points to remember about flowchart symbols:
 Every flowchart has a starts with the START symbol and ends with the END symbol
 Processes have only one entry and one exit point
 Decisions (Selections) have only one entry point and two exit points: one exit point (labeled as
YES) is used if the decision condition is satisfied and the other exit point (labeled as NO) is used
if the decision condition is not satisfied.
New Material
Looping Statements:
Loops are used to perform repetitive tasks such as printing sequential numbers on screen. The statements
enclosed inside a loop are usually executed more than once. Each execution is called an iteration.
There are four different LOOP Structures:
1. WHILE …… ENDWHILE loop
2. DO …….. WHILE loop
3. REPEAT……UNTIL loop ( not AVAILABLE in C )
4. FOR …… ENDFOR loop
3
1. WHILE…ENDWHILE Structure
Flowchart
Pseudocode
?
Condition
WHILE condition
Instruction-1
Instruction-2
…
ENDWHILE
NO
YES
Instruction 1
Instruction 2
….
Characteristics :
 Iteration count is greater than and equal to 0 (zero).
 If the condition is initially FALSE Loop Body will never be executed.
 Working Mechanism : First Check then Execute
 The loop is repeated as long as the condition is true!
Example: Display all integer numbers between 1 and 10 on screen. The boundaries (1 and 10) should
also be displayed.
Flowchart
Pseudocode
i 1
START
WHILE i <= 10
Display i
i  i +1
i1
ENDWHILE
?
i<=10
NO
YES
i
ii+1
END
4
2. DO…..WHILE Structure
Flowchart
Pseudocode
Instruction 1
Instruction 2
….
YES
?
Condition
DO
Instruction-1
Instruction-2
…
WHILE condition
NO
Characteristics :
 Iteration time is greater than and equal to 1 .
 Even if the condition is initially FALSE the Loop will be executed at least once.
 Working Mechanism is First Execute and Then Check
 Iterations are carried out while condition is TRUE, and stopped when the condition is FALSE.
Example: Display all integer numbers between 1 and 10 on screen. The boundaries (1 and 10) should
also be displayed.
Flowchart
Pseudocode
i 1
DO
Display i
i  i+1
START
i1
WHILE i <= 10
i
ii+1
YES
?
i<=10
NO
END
5
3. REPEAT…..UNTIL Structure
Flowchart
Pseudocode
REPEAT
Instruction-1
Instruction-2
…
UNTIL condition
Instruction 1
Instruction 2
….
NO
?
Condition
YES
Characteristics :




Iteration time is greater than or equal to 1 .
Even if the condition is initially TRUE the Loop will be executed at least once.
Working Mechanism is First Execute and Then Check
Iterations are carried out while condition is FALSE, and stopped when the condition is TRUE.
Example: Display all integer numbers between 1 and 10 on screen. The boundaries (1 and 10) should
also be displayed.
Flowchart
Pseudocode
START
i1
REPEAT
ii+1
Display i
UNTIL i > 10
i1
i
ii+1
NO
?
i>10
YES
END
6
4. FOR … ENDFOR Structure :
Flowchart
lowerlimit
stepsize
Pseudocode
Upper
limit
Instruction 1
Instruction 2
….
YES
FOR loopcounter=lowerlimit to
upperlimit with increment = stepsize
Instruction-1
Instruction-2
…
ENDFOR
NO
Example: Display all integer numbers between 1 and 10 on screen. The boundaries (1 and 10) should
also be displayed.
Flowchart
Pseudocode
i1
START
FOR i =1 to 10 with increments = 1
DISPLAY total
END FOR
i1
ii+1
?
i <=10
NO
YES
ii+1
i
END
7
EXERCISES :
Write down the ‘pseducodes’ for the following flowcharts :
(1)
START
PI  3.14
A  PI*R*R
R
STOP
A
(2)
START
FACTOR 1
COU NT0
?
COUNT=0
YES
NUM
COUNTçNUM
Count
Num
1
FACTORCOUNTXFACTOR
COUNTCOUNT-1
STOP
NO
1
8
(3)
START
TOTAL0
COUNTER0
NUM
?
COUNTER<NUM
YES
TOTALTOTAL+COUNTER
COUNTERCOUNTER+1
NO
STOP
(4)
TOTAL0
COUNTER0
START
NUM
STOP
NO
?
COUNTER<NUM
YES
TOTALTOTAL+COUNTER
COUNTERCOUNTER+1
9
(5)
START
?
N1 != 0
IS N1 NOT EQUAL TO
ZERO ?
N1  1
N2  1
?
N1!=0
YES
NO
N1, N2
?
N1>N2
YES
“N1 GREATER THAN N2"
NO
STOP
?
N1<N2
NO
YES
“N2 GREATER THAN N1"
N1 EQUAL TO N2
Download