Chapter 8
Chapter 8
if ( c o n d itio n )
{ // o p e n b lo c k
// a ll e x e c u ta b le s ta te m e n ts
} // c lo s e b lo c k
if ( c o n d itio n ) { // o p e n b lo c k
// a ll e x e c u ta b le s ta te m e n ts
} // c lo s e b lo c k
Example: 2_e
2
8.2 Multiple branches
A multiple branch is a generalization of a one-sided branch.
if ( c o n d itio n ) if ( c o n d itio n 1 )
{ {
} }
e ls e e ls e if ( c o n d itio n 2 )
{ {
} }
e ls e if ( c o n d itio n 3 )
{
}
e ls e
{
}
If one condition is satisfied, the corresponding block (and only this one!) is executed.
If no condition is satisfied,
• either no block is executed (if no else-branch is present) or
• if else is present, the last block is executed (else-branch)
3
2
2nd Assignment: compute the solutions of the quadratic equation a x b x c 0
real solution:
input a=? a=0 b=? b0 c
a, b, c x1
b
a0 b=0
2
b 4 a c
compute discriminant D 2
4 a c=? c0 no solution
D=?
c=0
every number is solution
4
I Task
2
Compute the solutions of a x b x c 0
Input coefficients a, b, c
Compute solutions
Output solutions
IV Program
Write source code
5
algorithm if a 0 then
compute discriminant d= (b² - 4ac) / (4a²)
if d>0
compute real solutions x1 and x2
x1= -b/(2. * a) + sqrt(d)
x2= -b/(2. * a) - sqrt(d)
output of solutions
else if d<0
compute complex solutions x1 and x2
x1= -b/(2. * a) +I* sqrt(-d)
x2= -b/(2. * a) - I* sqrt(-d)
output of solutions
else (i.e. if d=0)
compute solution x1
x1= -b/(2. * a)
output of solution
else (i.e. if a = 0)
if b 0 the
compute real solution
x1= -c/b
output of solution
else (i.e. b=0)
if c 0
no solution
output
else (i.e. if c=0)
each number is solution
output
6
8.3 Loops
while-loop
w h ile ( c o n d itio n )
{
// s ta te m e n ts
}
If the condition is not equal 0 (i.e. logically true), the statements in the following
block are executed.
in t i, s ;
i = 0;
s = 0;
w h ile ( s < 1 0 )
Example: 3_e {
s c a n f ( “ % d “ , & i) ;
s = s + i;
}
p r in tf ( “ T h e s u m is % d “ , s ) ;
7
3rd Assignment: bending moment of a beam
3 3
q 1 l1 q 2 l 2 j I y1
M b
mit j
8 ( l1 l 2 j ) I y2
2 2
x q 1 l1 x x
M (x) M 1 for x l1 where 1
y b
l1 2
l1 l1
2
x l1 q 2 l 2
2
x l1 x l1
M (x) M 1
for x l1 where
y b
l2 2
2 2
l2 l 2
8
for-loop
f o r ( E x p r e s s io n 1 ; E x p r e s s io n 2 ; E x p r e s s io n 3 )
{
// s ta te m e n ts
Example: 4_d
9
Comparison of for and while-loop
Note:
The ‘for‘ loop is equivalent to the following ‘while‘ loop
E x p r e s s io n 1 ;
f o r ( E x p r e s s io n 1 ; E x p r e s s io n 2 ; E x p r e s s io n 3 ) w h i l e ( E x p r e s s io n 2 )
{ {
// s ta te m e n ts // s ta te m e n ts
E x p r e s s io n 3 ;
} }
10
Infinite loops
fo r ( ;;) w h ile ( 1 )
{ {
} }
f (x) 0 where f ( x ) ( x 1) ( x 1)
f ( a i ) (bi a i )
ui ai
I [a i , bi ] f ( a i ) f (bi ) 0 f (bi ) f ( a i )
y
f (ai )
ui bi
ai x
f (ui )
f (bi ) f (x)
12
• Input of interval (a,b)
Design of the program: • Check interval
• Regula falsi
• Output of solution
y y y
f (b)
f(x)
f(a) f(a)
x
a b a b x a b x
f (b)
f(a) f(x) f (b)
f(x)
f ( a ) f (b ) 0
13
Regula falsi
f (a )
u3 u2 u1
a b x
f (x)
f (u3)
f (u2) f (b )
f (u1)
14
Stop iteration if either
1.) the absolute value f (u i ) is smaller than a given tolerance , for example 110-6, or
y y
f (a )
f (a )
a b x a b x
f (x) f (x)
f (b ) f (b )
15
do-while-loop
while-loop do-while-loop
w h ile ( c o n d itio n ) do
{ {
// s ta te m e n ts // s ta te m e n ts
} }
w h ile ( c o n d itio n ) ;
s w it c h ( e x p r e s s io n )
{
c a s e c o n s ta n t1 : •The break-expression causes immediate
// s ta te m e n ts exit of the switch-expression
b re a k ;
c a s e c o n s ta n t2 : • If break is omitted, execution continues
// s ta te m e n ts with the next statement, even if a case
b re a k ; expression is passed over!
d e fa u lt:
// s ta te m e n ts
} Examples: 5_c
17
8.5 Frequently encountered programming errors in
connection with control structures
• End of statement
• Break statement
• Semicolon
Note: Computers make what we tell them to do, not what they should do!
Example: 5_d
18
8.6 Nested loops and branches
Rules
Example: 5_e
19
Excursion: one-dimensional arrays
Remember this … ? This is what arrays are for:
Type name_of_array[size_of_array];
21
Example for an algorithm with nested loops
Bubble-Sort algorithm
aj-1 > aj
yes no
swap
aj-1 and aj
Example: fpsort
22
Initial order: a0 a1 a2 a3 a4 a5 a6 a7
17 3 30 41 24 35 50 12
j=7 17 3 30 41 24 35 12 50
j=6 17 3 30 41 24 12 35 50
j=5 17 3 30 41 12 24 35 50
k=1 j=4 17 3 30 12 41 24 35 50
j=3 17 3 12 30 41 24 35 50
j=2 17 3 12 30 41 24 35 50
j=1 3 17 12 30 41 24 35 50
j=7 3 17 12 30 41 24 35 50
j=6 3 17 12 30 41 24 35 50
j=5 3 17 12 30 24 41 35 50
k=2
j=4 3 17 12 24 30 41 35 50
j=3 3 17 12 24 30 41 35 50
j=2 3 12 17 24 30 41 35 50
23
5th Assignment: bubble-sort
aj-1 > aj
yes no
swap
aj-1 and aj
24