C Programming 4
C Programming 4
1 / 50
Outline
1 Loops
while loop
do-while loop
for loop
Examples of Loops
2 Miscellaneous
3 How to Debug
C Programming 2 / 50
An opening example
• Calculate S = 5x=1 x12
P
C Programming 3 / 50
Motivation of loops
C Programming 4 / 50
Loops
• To repeat statements as long as a certain condition is true (non-zero)
• C offers 3 different loops
• We can replace one with another
• Different loop offers different convenience
while ( condition )
statement ;
do
statement ;
while ( condition ) ;
f o r ( i n i t i a l i z a t i o n ; condition ; statement )
statement ;
1 Loops
while loop
do-while loop
for loop
Examples of Loops
2 Miscellaneous
3 How to Debug
C Programming 6 / 50
while loop control (1)
zero
condition
non-zero
statement(s)
C Programming 7 / 50
while loop control (2)
• The execution of checks if the condition is still non-zero
• If it is, execute the statement(s)
• Otherwise, gets out from the loop
1 int i = 2;
2 w h i l e ( i > 0)
3 −− i ;
4 p r i n t f ( ” done \n” ) ;
1 int i = 2;
2 w h i l e ( i > 0)
3 −− i ;
4 p r i n t f ( ” done \n” ) ;
1 int i = 2;
2 w h i l e ( i > 0)
3 −− i ;
4 p r i n t f ( ” done \n” ) ;
1 int i = 2;
2 w h i l e ( i > 0)
3 −− i ;
4 p r i n t f ( ” done \n” ) ;
1 int i = 2;
2 w h i l e ( i > 0)
3 −− i ;
4 p r i n t f ( ” done \n” ) ;
1 int i = 2;
2 w h i l e ( i > 0)
3 −− i ;
4 p r i n t f ( ” done \n” ) ;
1 int i = 2;
2 w h i l e ( i > 0)
3 −− i ;
4 p r i n t f ( ” done \n” ) ;
C Programming 9 / 50
Check Prime Number (2)
1 Start from 2 to N
2 Check whether N is dividable by any number in this range
1 i n t i = 2 , N = 177;
2 i n t PRIME = 1 ;
3 w h i l e ( i < N)
4 {
5 i f (N%i == 0 )
6 {
7 PRIME = 0 ;
8 }
9 }
10 i f ( PRIME )
11 p r i n t f ( ”%d i s a p r i m e number \n” , N) ;
12 else
13 p r i n t f ( ”%d i s n o t a p r i m e number \n” , N) ;
• Do we miss anything?
C Programming 10 / 50
Check Prime Number (3)
1 Start from 2 to N
2 Check whether N is dividable by any number in this range
1 i n t i = 2 , N = 177;
2 i n t PRIME = 1 ;
3 w h i l e ( i < N)
4 {
5 i f (N%i == 0 )
6 {
7 PRIME = 0 ;
8 }
9 i ++;
10 }
11 i f ( PRIME )
12 p r i n t f ( ”%d i s a p r i m e number \n” , N) ;
13 else
14 p r i n t f ( ”%d i s n o t a p r i m e number \n” , N) ;
C Programming 11 / 50
Check Prime Number (4)
1 In the above example, we no need to √all the numbers in [2· · · N-1]
2 N is not dividable by numbers after ⌈ N⌉
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e <math . h>
3 i n t main ( )
4 {
5 i n t i = 2 , N = 177;
6 i n t PRIME = 1 , bnd = ( i n t ) c e i l ( s q r t (N) ) ;
7 w h i l e ( i <= bnd )
8 {
9 i f (N%i == 0 )
10 {
11 PRIME = 0 ;
12 }
13 i ++;
14 }
15 i f ( PRIME )
16 p r i n t f ( ”%d i s a p r i m e number \n” , N) ;
17 else
18 p r i n t f ( ”%d i s n o t a p r i m e number \n” , N) ;
19 return 0;
20 }
C Programming 12 / 50
Outline
1 Loops
while loop
do-while loop
for loop
Examples of Loops
2 Miscellaneous
3 How to Debug
C Programming 13 / 50
do...while (1)
1 int i = 3; 1 int i = 3;
2 do 2 w h i l e ( i > 1)
3 { 3 {
4 −− i ; 4 −− i ;
5 p r i n t f ( ” i=%d\n” , i ) ; 5 p r i n t f ( ” i=%d\n” , i ) ;
6 } w h i l e ( i > 1) ; 6 }
C Programming 14 / 50
do...while (2)
1 i =2 1 i =2
2 i =1 2 i =1
C Programming 15 / 50
do...while (3)
statement(s)
non-zero
condition
zero
C Programming 16 / 50
Example 1 (1)
√
• Calculate x = a
• xn+1 = 21 (xn + xa )
n
• Loop until error less than 10−5 in consecutive iterations
• Hints: x1 is an arbitrary positive value
C Programming 17 / 50
Example 1 (2)
√
• Calculate x = a
• xn+1 = 21 (xn + xa )
n
• Loop until error less than 10−5 in consecutive iterations
• Hints: x1 is an arbitrary positive value
1 We need a loop (while? do-while or for?)
2 We need to keep two results from consecutive iterations
3 Anything else??
4 Let’s do it!
C Programming 18 / 50
Example 1 (3)
√
• Calculate x = a
• xn+1 = 21 (xn + xa )
n
• Loop until error less than 10−5 in consecutive iterations
• Hints: x1 is an arbitrary positive value
1 f l o a t a = 5 , x0 = 3 . 1 , xn = 0 , e r r = 0 ;
2 do
3 {
4 xn = 0 . 5 ∗ ( xn+a / xn ) ;
5 e r r = a b s ( xn − x0 ) ;
6 } w h i l e ( e r r >= 0 . 0 0 0 0 1 )
7 p r i n t f ( ” s q r t ( a )=%f \n” , xn ) ;
• Anything wrong??
C Programming 19 / 50
Example 1 (4)
√
• Calculate x = a
• xn+1 = 21 (xn + xa )
n
• Loop until error less than 10−5 in consecutive iterations
• Hints: x1 is an arbitrary positive value
1 f l o a t a = 5 , xk = 0 , xn = 3 . 1 , e r r = 0 ;
2 int i = 0;
3 do
4 {
5 xk = xn ;
6 xn = 0 . 5 ∗ ( xk+a / xk ) ;
7 // p r i n t f (”% f \ t%f \ t%f \n ” , xk , xn , e r r ) ;
8 e r r = f a b s ( xn − xk ) ;
9 i ++;
10 } w h i l e ( e r r >= 0 . 0 0 0 0 1 ) ;
11 p r i n t f ( ” i t e r s = %d , s q r t ( a )=%f \n” , i , xn ) ;
1 Loops
while loop
do-while loop
for loop
Examples of Loops
2 Miscellaneous
3 How to Debug
C Programming 21 / 50
for loop (1)
The For-Loop is comfortable for iterating. It takes three arguments.
• Initialization (i=0;)
• Condition (i < 5;)
• Iteration statement (i+=1)
START
initialize i = 0;
test i < 5;
body of loop
increment i++
C Programming 22 / 50
for loop (2)
int i ;
f o r ( i = 1 ; i <= 1 0 ; ++i )
{
p r i n t f ( ”%d\n” , i ) ;
}
• i starts from 1
• Check if i is less than or equal to 10
• Go into the loop if it is true (non-zeor)
• Increment i, e.g., i++ or i+=2
C Programming 23 / 50
break
• Similar as switch-case
• break can be used inside a loop
• Jumping out from the loop as soon as it is called
int i , s = 0;
f o r ( i = 1 ; i <= 1 0 ; ++i )
{
s += 2∗ i ;
i f ( i %4 == 0 )
break ;
}
p r i n t f ( ” s=%d\n” , s ) ;
C Programming 24 / 50
continue
1 int i , s = 0; 1 int i , s = 0;
2 f o r ( i = 1 ; i <= 1 0 ; ++i ) 2 f o r ( i = 1 ; i <= 1 0 ; ++i )
3 { 3 {
4 s += 2∗ i ; 4 s += 2∗ i ;
5 i f ( i %4 == 0 ) 5 i f ( i %4 == 0 )
6 break ; 6 continue ;
7 } 7 }
8 p r i n t f ( ” s=%d\n” , s ) ; 8 p r i n t f ( ” s=%d\n” , s ) ;
C Programming 25 / 50
Outline
1 Loops
while loop
do-while loop
for loop
Examples of Loops
2 Miscellaneous
3 How to Debug
C Programming 26 / 50
Example 2 (1)
C Programming 27 / 50
Example 2 (2)
1 f l o a t n1 = 2 , n2 = 3 ;
2 i n t d1 = 1 , d2 = 2 ;
3 f o r ( i = ? ; i <= 2 0 ; ++i )
4 {
5 ....
6 }
C Programming 28 / 50
Example 2 (3)
1 f l o a t n1 = 2 , n2 = 3 ;
2 i n t d1 = 1 , d2 = 2 , i = 0 ;
3 f l o a t s = n1 / d1 + n2 / d2 ;
4 f o r ( i = ? ; i <= 2 0 ; ++i )
5 {
6 ....
7 }
8 p r i n t f ( ” s=%f \n” , s ) ;
C Programming 29 / 50
Example 2 (4)
• Given following series of fraction
• 21 , 32 , 35 , 85 , 13 21
8 , 13 ,· · ·
• Work out the sum of first 20 terms
• We need an iterator, start from where??
• How to work out n1 and d1
1 f l o a t n1 = 2 , n2 = 3 ;
2 i n t d1 = 1 , d2 = 2 , i = 0 ;
3 f l o a t s = n1 / d1 + n2 / d2 ;
4 f o r ( i = ? ; i <= 2 0 ; ++i )
5 {
6 n1 = ? ;
7 d1 = ? ;
8 s += n1 / d1 ;
9 }
10 p r i n t f ( ” s=%f \n” , s ) ;
C Programming 30 / 50
Example 2 (5)
• Given following series of fraction
• 21 , 32 , 35 , 85 , 13 21
8 , 13 ,· · ·
• Work out the sum of first 20 terms
• The full story
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 f l o a t n1 = 2 , n2 = 3 ;
5 i n t d1 = 1 , d2 = 2 , i = 0 ;
6 f l o a t s = n1 / d1 + n2 / d2 ;
7 f o r ( i = 3 ; i <= 2 0 ; ++i ) 15 p r i n t f ( ” s=%f \n” , s ) ;
8 { 16 return 0;
9 n2 = n1 + n2 ; 17 }
10 d2 = d1 + d2 ;
11 s += n2 / d2 ;
12 n1 = n2 − n1 ;
13 d1 = d2 − d1 ;
14 }
15 }
C Programming 31 / 50
Example 3 (1)
*
***
*****
*******
*****
***
*
C Programming 32 / 50
Example 3 (2)
C Programming 33 / 50
Example 3 (3)
C Programming 34 / 50
Example 3 (4)
C Programming 35 / 50
Example 3 (5)
1 Print things to the bounds
int n = 5 , i = 0 , j = 0; nb += 2 ; //<−−why ?
i n t n s = 1 , nb = n −1; n s −= 4 ; //<−−why ?
f o r ( i = 0 ; i < n ; i ++) f o r ( i = 0 ; i < n ; i ++)
{ {
f o r ( j = 0 ; j < nb ; j ++) f o r ( j = 0 ; j < nb ; j ++)
{ {
printf (” ”) ; printf (” ”) ;
} }
f o r ( j = 0 ; j < n s ; j ++) f o r ( j = 0 ; j < n s ; j ++)
{ {
p r i n t f ( ”∗” ) ; p r i n t f ( ”∗” ) ;
} }
n s += 2 ; n s −= 2 ;
nb−−; nb++;
p r i n t f ( ” \n” ) ; p r i n t f ( ” \n” ) ;
} }
C Programming 36 / 50
Example 4 (1): solving by exhaustive search
C Programming 37 / 50
Example 4 (2): solving by exhaustive search
• Enumerate x from 1 to 30
• Enumerate y from 1 to 30
• Enumerate z from 1 to 30
• Now let’s do it!
C Programming 38 / 50
Example 4 (3): solving by exhaustive search
• Enumerate x from 1 to 30
• Enumerate y from 1 to 30
• Enumerate z from 1 to 30
3 ∗ x + 2 ∗ y + z = 50
x + y + z = 30
f o r ( x = 0 ; x <= 3 0 ; x++)
{
f o r ( y = 0 ; y <= 3 0 ; y++)
{
f o r ( z = 0 ; z <= 3 0 ; z++)
{
....
}
}
}
C Programming 39 / 50
Example 4 (4): solving by exhaustive search
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 i n t x = 0 , y = 0 , z = 0 , c1 = 0 , c2 = 0 ;
5 f o r ( x = 0 ; x <= 3 0 ; x++)
6 { f o r ( y = 0 ; y <= 3 0 ; y++)
7 { f o r ( z = 0 ; z <= 3 0 ; z++)
8 {
9 c1 = 3∗ x+2∗y+z ;
10 c2 = x+y+z ;
11 i f ( c1 == 50 && c2 == 3 0 )
12 {
13 p r i n t f ( ” x = %d , y = %d , z = %d\n” , x , y , z ) ;
14 }
15 } // f o r ( z )
16 } // f o r ( y )
17 } // f o r ( x )
18 }
C Programming 40 / 50
Example 4 (5): solving by exhaustive search
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 i n t x = 0 , y = 0 , z = 0 , c1 = 0 , c2 = 0 ;
5 f o r ( x = 0 ; x < 1 7 ; x++) //<−− why ??
6 { f o r ( y = 0 ; y <= 2 5 ; y++) //<−− why ??
7 { f o r ( z = 0 ; z <= 3 0 ; z++)
8 {
9 c1 = 3∗ x+2∗y+z ;
10 c2 = x+y+z ;
11 i f ( c1 == 50 && c2 == 3 0 )
12 {
13 p r i n t f ( ” x = %d , y = %d , z = %d\n” , x , y , z ) ;
14 }
15 } // f o r ( z )
16 } // f o r ( y )
17 } // f o r ( x )
18 }
C Programming 41 / 50
Outline
1 Loops
while loop
do-while loop
for loop
Examples of Loops
2 Miscellaneous
3 How to Debug
C Programming 42 / 50
Try to avoid following case
• Be careful, this
1 w h i l e (1 > 0)
2 p r i n t f ( ” Did you m i s s me?\ n” ) ;
Do not be evil!
C Programming 43 / 50
Valid variants of for-loop (1)
int i = 1;
f o r ( ; i <= 1 0 ; ++i )
p r i n t f ( ”%d\n” , i ) ;
f o r ( i = 1 ; i <= 1 0 ; )
p r i n t f ( ”%d\n” , ++i ) ; /∗ seems more l i k e a w h i l e l o o p
∗/
C Programming 44 / 50
Valid variants of for-loop (2)
for (;;)
p r i n t f ( ” I ’m s t i l l h e r e \n” ) ;
C Programming 45 / 50
Outline
1 Loops
while loop
do-while loop
for loop
Examples of Loops
2 Miscellaneous
3 How to Debug
C Programming 46 / 50
Story behind Bug
C Programming 47 / 50
Overview about Programming Bugs (1)
• It happens!
• Two types of bugs
1 Grammar mistakes
2 Logic bugs
1 i n t main ( ) 1 i n t main ( )
2 { 2 {
3 int i = 0 , s = 0; 3 int i = 0, s ;
4 f o r ( i = 0 ; i < 5 ; i ++) 4 f o r ( i = 0 ; i < 5 ; i ++) ;
5 { 5 {
6 s += i ; 6 s += i ;
7 } 7 }
8 return 0; 8 return 0;
9 } 9 }
C Programming 48 / 50
Overview about Programming Bugs (2)
• From my humble point of view
• Two types of bugs
1 Non-memory leakage bugs
2 Memory leakage bugs
• Grammar mistakes are not bug!!
1 i n t main ( )
1 i n t main ( )
2 {
2 {
3 int i = 0;
3 int i = 0 , s = 0;
4 i n t ∗p = NULL ;
4 f o r ( i = 0 ; i < 5 ; i −−) ;
5 f o r ( i = 0 ; i < 5 ; i ++)
5 {
6 {
6 s += i ;
7 p = NULL ;
7 i f ( i % 2 == 1 ) ;
8 p = ( i n t ∗) malloc (
8 continue ;
sizeof ( int )) ;
9 }
9 }
10 return 0;
10 return 0;
11 }
11 }
C Programming 49 / 50
Debug by “printf()”
• From my humble point of view
• Two types of bugs
1 Non-memory leakage bugs
2 Memory leakage bugs
• Grammar mistakes are not bug!!
1 i n t main ( )
2 {
3 int i = 0 , s = 0;
4 p r i n t f ( ” bug 1 . 0 \ n” ) ;
5 f o r ( i = 0 ; i < 5 ; i −−) ;
6 {
7 s += i ;
8 p r i n t f ( ” bug 1 . 1 %d %d\n” , i , s ) ;
9 i f ( i % 2 == 1 ) ;
10 continue ;
11 }
12 p r i n t f ( ” bug 2 . 0 \ n” ) ;
13 return 0;
14 }
C Programming 50 / 50