C_Programming__3_
C_Programming__3_
a=a-b a=b-a
1 / 59
Outline
3 Program structure
5 Conditional Controls
if-else clause
Logical operators
6 Conditions
C Programming 2 / 59
Programs for programming
C Programming 3 / 59
Outline
3 Program structure
5 Conditional Controls
if-else clause
Logical operators
6 Conditions
C Programming 4 / 59
The first program
C Programming 5 / 59
From source to bits
Source code: main.c
⇓
$ g c c main . c
C Programming 6 / 59
Outline
3 Program structure
5 Conditional Controls
if-else clause
Logical operators
6 Conditions
C Programming 7 / 59
A basic program
C Programming 8 / 59
The main function
1 i n t main ( v o i d )
2 {
C Programming 9 / 59
The main function scope
1 return 0;
2 }
• Last statement, ends main function (and thus the whole program)
• 0 tells the OS that everything went right
• ’}’ marks the end of the main function scope
C Programming 10 / 59
Statements
1 p r i n t f ( ” H e l l o World ! \ n” ) ;
C Programming 11 / 59
Comments
• Information for the programmer, cut out before compilation
Single line comments:
1 // P r i n t s ” H e l l o World ! ” on t h e command l i n e
C Programming 12 / 59
Order of execution
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 { 1 H e l l o China !
4 p r i n t f ( ” H e l l o C h i n a ! \ n” ) ; 2 H e l l o World !
5 p r i n t f ( ” H e l l o World ! \ n” ) ; 3 Hello Universe !
6 p r i n t f ( ” H e l l o U n i v e r s e ! \ n” ) ; 4
7 return 0;
8 }
C Programming 13 / 59
Outline
3 Program structure
5 Conditional Controls
if-else clause
Logical operators
6 Conditions
C Programming 14 / 59
Calculate the Area of a circle (1)
• Available information
• radius, π = 3.1415
• Requirements
• Allows user input radius of a circle
• Calculate its area and print it out
a = pi · r 2
• Let’s do it step by step
C Programming 15 / 59
Calculate the Area of a circle (2)
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 return 0;
5 }
6
C Programming 16 / 59
Calculate the Area of a circle (3)
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 float pi = 3.1415;
5 f l o a t r = 0;
6 return 0;
7 }
8
C Programming 17 / 59
Calculate the Area of a circle (4)
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 float pi = 3.1415;
5 f l o a t r = 0;
6 s c a n f ( ”%f ” , &r ) ;
7 return 0;
8 }
9
C Programming 18 / 59
Calculate the Area of a circle (5)
C Programming 19 / 59
Solve Quadratic Equation (1)
• Solve x out
C Programming 20 / 59
Solve Quadratic Equation (2)
−b √
2 −4ac
p= , q = b 2a
2a
x1 = p + q, x2 = p − q
C Programming 21 / 59
Solve Quadratic Equation (3)
• Let’s now think about how to implement it in C
−b √
2 −4ac
p= , q = b 2a
2a
x1 = p + q, x2 = p − q
C Programming 22 / 59
Solve Quadratic Equation (4)
−b √
2 −4ac
p= , q = b 2a
2a
x1 = p + q, x2 = p − q
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 f l o a t a = 0 , b = 0 , c = 0 , delta = 0;
6 f l o a t x1 = 0 , x2 = 0 , p = 0 , q = 0 ;
7 p r i n t f ( ” I n p u t a , b and c : \ n” ) ;
8 s c a n f ( ”%f%f%f ” , &a , &b , &c ) ;
9 d e l t a = b∗b − 4∗ a ∗ c ;
10 p = −b / ( 2 ∗ a ) ;
11 q = s q r t ( d e l t a ) /(2∗ a ) ;
12 x1 = p + q ; x2 = p − q ;
13 p r i n t f ( ” x1=%f , x2=%f \n” , x1 , x2 ) ;
14 return 0;
15 }
16
C Programming 23 / 59
Comments
−b √
2 −4ac
p= , q = b 2a
2a
x1 = p + q, x2 = p − q
C Programming 24 / 59
Outline
3 Program structure
5 Conditional Controls
if-else clause
Logical operators
6 Conditions
C Programming 25 / 59
Outline
3 Program structure
5 Conditional Controls
if-else clause
Logical operators
6 Conditions
C Programming 26 / 59
Start with a simple example
• Guess what the following code for
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 int x = 5;
5 i f ( x%2 == 0 )
6 {
7 p r i n t f ( ” x i s an e v e n number . ” ) ;
8 }
9 return 0;
10 }
11
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 int x = 5;
5 if ( conditional expression )
6 {
7 p r i n t f ( ” x i s an e v e n number . ” ) ;
8 }
9 return 0;
10 }
11
1 i n t main ( )
2 {
3 int a = 0 , b = 0 , c = 0;
4 a = (3 > 5) ;
5 b = (2∗2 > 4) ;
6 c = ( 3 == 3 ) ;
7 return 0;
8 }
C Programming 29 / 59
Comments
−b √
2 −4ac
p= , q = b 2a
2a
x1 = p + q, x2 = p − q
C Programming 30 / 59
Solve Quadratic Equation
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 f l o a t a = 0 , b = 0 , c = 0 , delta = 0;
6 f l o a t x1 = 0 , x2 = 0 , p = 0 , q = 0 ;
7 p r i n t f ( ” I n p u t a , b and c : \ n” ) ;
8 s c a n f ( ”%f%f%f ” , &a , &b , &c ) ;
9 d e l t a = b∗b − 4∗ a ∗ c ;
10 i f ( d e l t a >= 0 ) {
11 p = −b / ( 2 ∗ a ) ;
12 q = s q r t ( d e l t a ) /(2∗ a ) ;
13 x1 = p + q ; x2 = p − q ;
14 p r i n t f ( ” x1=%f , x2=%f \n” , x1 , x2 ) ;
15 }else{
16 p r i n t f ( ”No r e a l s o l u t i o n ! \ n” ) ;
17 }
18 return 0;
19 }
C Programming 31 / 59
Exercise
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 i n t a = −1;
5 unsigned i n t b = 600;
6 i f (a > b)
7 {
8 p r i n t f ( ”%d i s g r e a t e r t h a n %d\n” , a , b ) ;
9 }else{
10 p r i n t f ( ”%d i s s m a l l e r t h a n %d\n” , a , b ) ;
11 }
12 return 0;
13 }
14
C Programming 32 / 59
Outline
3 Program structure
5 Conditional Controls
if-else clause
Logical operators
6 Conditions
C Programming 33 / 59
Logical Operators (1)
C Programming 34 / 59
Logical Operators (2)
• AND (c1 && c2): means only when c1 and c2 both are true, it is true
• OR (c1 || c2): means when either c1 or c2 both is true, it is true
• NOT (!c1): means reverse it, c1 is true, !c1 is false; c1 is false, !c1 is
true
1 i n t main ( )
2 {
3 int a = 0 , b = 0 , c = 0;
4 a = ( 3 > 5 ) &&(2 > 1 ) ;
5 b = ( 2 ∗ 2 > 4 ) | | ( 2 == 1 ) ;
6 c = ! ( 3 == 3 ) ;
7 return 0;
8 }
C Programming 35 / 59
Logical Operators (3): truth tables
c1 c2 c1 && c2 c1 c2 c1 || c2
1 1 1 1 1 1 c1 !c1
1 0 0 1 0 1 1 0
0 1 0 0 1 1 0 1
0 0 0 0 0 0
• One should be able to deduce for cases that more than two
statements are involved
C Programming 36 / 59
Priority of Relational Operators (1)
C Programming 37 / 59
Priority of Relational Operators (2)
C Programming 38 / 59
Priority of Relational Operators (3)
C Programming 39 / 59
How the logic expression is evaluated in C
• C actually checks only whether it is zero or non-zero
• For example
1 i n t main ( )
2 {
3 f l o a t a = 3.1 , b = 0;
4 i f (a){
5 printf (” it i s true ”) ;
6 }else{
7 printf (” it is false ”) ;
8 }
9 i f ( a && b ) {
10 printf (” it i s true ”) ;
11 }else{
12 printf (” it is false ”) ;
13 }
14 return 0;
15 }
C Programming 40 / 59
Outline
3 Program structure
5 Conditional Controls
if-else clause
Logical operators
6 Conditions
C Programming 41 / 59
if...else
Decisions are made during run time:
i f ( condition )
statement1 ;
else
statement2 ;
To differentiate between more than two cases, you can use the if condition
as a statement in the else body:
condition1 i f ( condition1 )
false statement1 ;
condition2 else i f ( condition2 )
statement2 ;
false
true else i f ( condition3 )
condition3 statement3 ;
true else
false
statement4 ;
true
st.1 st.2 st.3 st.4
C Programming 43 / 59
Judge the Type of an Input Character (1)
C Programming 44 / 59
Judge the Type of an Input Character (2)
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 c h a r ch = ’ ’ ;
5 p r i n t f (” Please input a character : ”) ;
6 ch = g e t c h a r ( ) ;
7 p r i n t f ( ” C h a r a c t e r i s : %c ” , ch ) ;
8 i f ( ch >= ’ 0 ’ && ch <= ’ 9 ’ ) {
9 p r i n t f ( ” a d i g i t \n” ) ;
10 } e l s e i f ( ch >= ’ a ’ && ch <= ’ z ’ ) {
11 p r i n t f ( ” c h a r i n l o w e r c a s e \n” ) ;
12 } e l s e i f ( ch >= ’A ’ && ch <= ’ Z ’ ) {
13 p r i n t f ( ” c h a r i n u p p e r c a s e \n” ) ;
14 } e l s e i f ( ch == ’ ’ ) {
15 p r i n t f ( ” I t i s s p a c e \n” ) ;
16 }else{
17 p r i n t f ( ” n o t d i g i t , c h a r o r s p a c e \n” ) ;
18 }
19 return 0;
20 }
C Programming 45 / 59
Judge whether it is a leap year (1)
C Programming 46 / 59
Judge whether it is a leap year (2)
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 int year = 0 , leap = 0;
5 p r i n t f ( ” Please enter the year : ” ) ;
6 s c a n f ( ”%d” , &y e a r ) ;
7 i f ( y e a r %400 == 0 ) {
8 leap = 1;
9 } e l s e i f ( y e a r%4 == 0 && y e a r %100 != 0 ) {
10 leap = 1;
11 }else{
12 leap = 0;
13 }
14 i f ( l e a p == 1 ) {
15 p r i n t f ( ”%d i s l e a p y e a r \n” , y e a r ) ;
16 }else{
17 p r i n t f ( ”%d i s n o t l e a p y e a r \n” , y e a r ) ;
18 }
19 }
C Programming 47 / 59
More about if-else clause (1)
1 i n t main ( )
2 {
3 int a = 3 , b = 5 , c = 3;
4 i f ( a != 3 )
5 i f ( b > 9)
6 p r i n t f ( ”b = %d” , b ) ;
7 else
8 p r i n t f ( ” c = %d” , c ) ;
9 return 0;
10 }
C Programming 48 / 59
More about if-else clause (2)
1 i n t main ( )
1 i n t main ( )
2 {
2 {
3 int a = 3 , b = 5 , c = 3;
3 int a = 3 , b = 5;
4 i f ( a != 3 )
4 int c = 3;
5 {
5 i f ( a != 3 )
6 i f ( b > 9)
6 i f ( b > 9)
7 p r i n t f ( ”b = %d” , b ) ;
7 p r i n t f ( ”b = %d” , b ) ;
8 else
8 else
9 p r i n t f ( ” c = %d” , c ) ;
9 p r i n t f ( ” c = %d” , c ) ;
10 }
10 return 0;
11 return 0;
11 }
12 }
C Programming 49 / 59
A few words on style
i f ( cond ) { s t a t e m e n t ; } /∗ bad s t y l e ∗/
i f ( cond ) { /∗ l o o k s b e t t e r , s t i l l bad s t y l e ∗/
statement ;
}
i f ( cond )
statement ; /∗ I t i s OK b u t n o t recommnended , p u t {} a l l
t h e t i m e ∗/
C Programming 50 / 59
More words on style
• Inside an if-else structure
• Put all blocks of this structure in braces
i f ( cond ) /∗ bad s t y l e , i n c o n s i s t e n t ∗/
statement ;
else {
statement ;
statement ;
}
i f ( cond )
i f ( cond ) {
{
/∗ way b e t t e r s t y l e ∗/
statement ;
statement ;
}else
}else{
{
statement ;
statement ;
statement ;
statement ;
}
}
C Programming 51 / 59
Operator: L=a⋇b?c:d
C Programming 52 / 59
Application: Convert lower case char to upper case (1)
C Programming 53 / 59
Application: Convert lower case char to upper case (2)
1 i n t main ( )
2 { 1 i n t main ( )
3 c h a r ch = g e t c h a r ( ) ; 2 {
4 i f ( ch >= ’ a ’ && ch <= ’ z ’ ) 3 c h a r ch = g e t c h a r ( ) ;
5 { 4 ch = ( ch>=’ a ’ && ch <= ’ z ’ )
6 ch = ch − 3 2 ; ? ( ch −32) : ch ;
7 } 5 p r i n t f ( ” ch = %c ” , ch ) ;
8 p r i n t f ( ” ch = %c ” , ch ) ; 6 return 0;
9 return 0; 7 }
10 }
• It is concise
• Do not make your expression too long
• Take the left way when you are uncertain
C Programming 54 / 59
if clause: the last example
1 i n t main ( )
2 {
3 int a = 3 , b = 5 , c = 2;
4 i f ( a > b) ;
5 {
6 a = c;
7 }
8 a = a ∗2;
9 p r i n t f ( ” a = %d\n” , a ) ;
10 return 0;
11 }
C Programming 55 / 59
switch-case clause (1)
• Now you are given a new task
• Convert numbers (1-12) to Month (January - December)
• We can do it by if-else clause
i n t main ( )
{
int n = 0;
s c a n f ( ”%d” , &n ) ;
i f ( n == 1 )
{
p r i n t f ( ” J a n u a r y \n” ) ;
} e l s e i f ( n == 2 ) {
p r i n t f ( ” F e b u a r y \n” ) ;
} e l s e i f ( n == 3 ) {
p r i n t f ( ” March \n” ) ;
}
...
...
...
}
C Programming 56 / 59
switch-case clause (2)
• Now you are given a new task
• Convert numbers (1-12) to Month (January - December)
• We can do it by if-else clause
i n t main ( )
{
int n = 0;
s c a n f ( ”%d” , &n ) ;
switch (n)
{
c a s e 1 : { p r i n t f ( ” J a n u a r y \n” ) ; b r e a k ; }
case 2: { p r i n t f ( ” Febuary ” ) ; break ; }
c a s e 3 : { p r i n t f ( ” March \n” ) ; b r e a k ; }
c a s e 1 2 : { p r i n t f ( ” December \n” ) ; b r e a k ; }
}
return 0;
}
C Programming 57 / 59
switch-case clause (3)
switch ( variable )
{
case option1 : statement1 ; break ;
case option2 : statement2 ; break ;
case option3 : statement3 ; break ;
d e f a u l t : statement4 ; break ;
}
C Programming 58 / 59
switch-case clause (4)
• What break means
• Work out the output of following codes
i n t main ( )
{
int n = 3;
switch (n)
{
c a s e 1 : { p r i n t f ( ” J a n u a r y \n” ) ; }
case 2: { p r i n t f ( ” Febuary ” ) ; break ; }
c a s e 3 : { p r i n t f ( ” March \n” ) ; }
c a s e 4 : { p r i n t f ( ” A p r i l \n” ) ; }
c a s e 5 : { p r i n t f ( ”May\n” ) ; b r e a k ; }
c a s e 1 2 : { p r i n t f ( ” December \n” ) ; b r e a k ; }
d e f a u l t : break ;
}
return 0;
}
C Programming 59 / 59