0% found this document useful (0 votes)
4 views59 pages

C_Programming__3_

Uploaded by

laihoptuan2006
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)
4 views59 pages

C_Programming__3_

Uploaded by

laihoptuan2006
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/ 59

C Programming

Sequential and Conditional Control


No
a>b
Yes

a=a-b a=b-a

1 / 59
Outline

1 Editors and IDE

2 Basic Ingradients of a C Program

3 Program structure

4 Program of Sequtial Control

5 Conditional Controls
if-else clause
Logical operators

6 Conditions

C Programming 2 / 59
Programs for programming

• Integrated Development Environment


• Microsoft Visual Studio (default option)
• Dev-C++ (recommended)
• VScode
• Codeblocks
• Any text editors + C compiler
• Microsoft C compiler (default option)
• GCC (recommended)
• ICC: believed to be the most efficient one
• Turbo C: classic but being forgotten

C Programming 3 / 59
Outline

1 Editors and IDE

2 Basic Ingradients of a C Program

3 Program structure

4 Program of Sequtial Control

5 Conditional Controls
if-else clause
Logical operators

6 Conditions

C Programming 4 / 59
The first program

• Create a new file named main.c.


• Open it in your text editor of choice.
• Fill it as follows:
1 #i n c l u d e < s t d i o . h>
2 i n t main ( v o i d )
3 {
4 p r i n t f ( ” H e l l o World ! \ n” ) ;
5 /∗ P r i n t ” H e l l o World ! ” on t h e command l i n e ∗/
6 return 0;
7 }
8

C Programming 5 / 59
From source to bits
Source code: main.c

$ g c c main . c

(Preprocessing → compiling → assembling → linking)



Executable program

Linux/Mac OS X (a.out) Windows (a.exe)


$ . / a . out $ ./ a .
$ Hello exe
World ! $
H e l l o World !

C Programming 6 / 59
Outline

1 Editors and IDE

2 Basic Ingradients of a C Program

3 Program structure

4 Program of Sequtial Control

5 Conditional Controls
if-else clause
Logical operators

6 Conditions

C Programming 7 / 59
A basic program

1 #i n c l u d e < s t d i o . h> Preprocessing statements


2 i n t main ( )
3 { 
4 p r i n t f ( ” H e l l o World ! \ n” ) ; 


/∗ P r i n t ” H e l l o World ! ” on t h e
5



command l i n e ∗/

6 return 0;
Main function

}
7




8

• Processed before compilation


• Have their own language, start with a #
• In ‘stdio.h’, function ‘printf()’ has been defined

C Programming 8 / 59
The main function

• Basic function of every program


• Exists exactly once per program
• Called on program start

1 i n t main ( v o i d )
2 {

• As a function, main() can take parameters and return a value


• Get used to void and int. They will be explained later
• ’{’ marks the start of the main function scope

C Programming 9 / 59
The main function scope

• Contains program statements


• They are processed from top to bottom

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

• Instructions for the computer


• End with a ; (semicolon)

1 p r i n t f ( ” H e l l o World ! \ n” ) ;

• Here is the empty statement:

• All statements are located in function blocks

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

Block comments (multi-line):


1 /∗ P r i n t s ” H e l l o World ! ”
2 on t h e command l i n e ∗/

Better style of block comments:


1 /∗
2 ∗ P r i n t s ” H e l l o World ! ”
3 ∗ on t h e command l i n e
4 ∗/

C Programming 12 / 59
Order of execution

• Statements inside one function executed from top to bottom


• This is a convention for languages

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 }

• For clarity, one statement in one line

C Programming 13 / 59
Outline

1 Editors and IDE

2 Basic Ingradients of a C Program

3 Program structure

4 Program of Sequtial Control

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)

• Create a new file named main.c


• Open it in your editor
• Fill it as follows:

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)

• Define variables needed

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)

• Allows user to input radius,

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)

• The complete program


• Allows user to input radius
1 #i n c l u d e < s t d i o . h>
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
2 i n t main ( )
3 {
3 {
4 float pi = 3.1415;
4 float pi = 3.1415;
5 f l o a t r = 0 , area = 0;
5 f l o a t r = 0 , area = 0;
6 s c a n f ( ”%f ” , &r ) ;
6 s c a n f ( ”%f ” , &r ) ;
7 area = r ∗ r ∗ pi ;
7 area = r ∗ r ∗ pi ;
8 p r i n t f ( ” Area : %f ” , a r e a ) ;
8 return 0;
9 return 0;
9 }
10 }
10
11

C Programming 19 / 59
Solve Quadratic Equation (1)

• Given following equation


• Allows user input a, b and c
ax 2 + bx + c = 0

• Solve x out

C Programming 20 / 59
Solve Quadratic Equation (2)

• The solution for this quadratic equation is well-known


• Given b 2 − 4ac > 0, we have

−b +b 2 − 4ac
x1 =
√2a
−b − b 2 − 4ac
x2 =
2a

• In order to simplify the calculation


• We have

−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

• Define variables and user input


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 a = 0 , b = 0 , c = 0 , delta = 0;
5 f l o a t x1 = 0 , x2 = 0 , p = 0 , q = 0 ;
6 p r i n t f ( ” I n p u t a , b and c : \ n” ) ;
7 s c a n f ( ”%f%f%f ” , &a , &b , &c ) ;
8 return 0;
9 }
10

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

• In above example, we did not consider the case


• b 2 − 4ac < 0
• For which, we should output “no real solution”
• That means, we should check b 2 − 4ac
• For different case, we give different answer
• This is where if...else fits in

C Programming 24 / 59
Outline

1 Editors and IDE

2 Basic Ingradients of a C Program

3 Program structure

4 Program of Sequtial Control

5 Conditional Controls
if-else clause
Logical operators

6 Conditions

C Programming 25 / 59
Outline

1 Editors and IDE

2 Basic Ingradients of a C Program

3 Program structure

4 Program of Sequtial Control

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

• if statement makes a judgement


• If the logic/conditional expression is true
• The statment(s) inside {...} will be executed
• Otherwise, statment(s) will be ignored
C Programming 27 / 59
Logic/conditional expression (1)
• Let’s now focus on the conditional expression

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

• It is a expression that returns true or false


• For example, statement “you are undergraduate student”
• We can judge whether it is true or false
• Paradox, story shared
C Programming 28 / 59
Logic/conditional expression (2)
• In C, expression with relational operators is used as conditional
expressions
• They are
1 <, >, <=, >=
2 == for “equal to”
3 != for “not equal to”
• It returns 1 (true) or 0 (false)

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

• For the case b 2 − 4ac < 0


• We should output “no real solution”
• For the case, b 2 − 4ac ≥ 0
• We should output x1 and x2

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

• What is the answer??

C Programming 32 / 59
Outline

1 Editors and IDE

2 Basic Ingradients of a C Program

3 Program structure

4 Program of Sequtial Control

5 Conditional Controls
if-else clause
Logical operators

6 Conditions

C Programming 33 / 59
Logical Operators (1)

• In some cases, single conditional statement is not enough


• For example, we want to express following condition
• If a > b AND b > c, then ...
• We need a way to connect several statements
• Usually, we use AND, OR and NOT
• In C, they are &&, || and !
• AND (c1 && c2): means only when c1 and c2 both are true, it is true
• OR (c1 || c2): means when either c1 or c2 is true, it is true
• NOT (!c1): means reverse it, c1 is true, !c1 is false; c1 is false, !c1 is
true

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)

1 5>3 && 2 | | 8<4−!0

• “!” is higher than +, -, *, /


• “>”, “<”, “<=”, “>=” and “==” are lower than +, -, *, /
• “&&” and “∥” are the lowest
• Please tell me the result of this expression (3 minutes)

C Programming 37 / 59
Priority of Relational Operators (2)

1 5>3 && 2 || 8<4−!0


2 5>3 && 2 || 8<4−1
3 5>3 && 2 || 8<3
4 5>3 && 2 || 0
5 1 && 2 || 0
6 1 || 0
7 1

• “!” is higher than +, -, *, /


• “>”, “<”, “<=”, “>=” and “==” are lower than +, -, *, /
• “&&” and “∥” are the lowest

C Programming 38 / 59
Priority of Relational Operators (3)

1 (5 >3) && 2 | | (8 <(4 −!0) )

• Usually, we put ‘()’ to regularize the priority levels

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

1 Editors and IDE

2 Basic Ingradients of a C Program

3 Program structure

4 Program of Sequtial Control

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 ;

statement1 is only executed if the truth value of condition is true.


Otherwise statement2 is executed.
For multiple statements inside the if-else, use braces {}:
i f ( condition ) {
statement1 ;
statement2 ;
}

• The else part is OPTIONAL


C Programming 42 / 59
else if

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)

• Judge an input character is a digit, a character, space or something


else
• Steps outlined
1 Accept/take input character
2 Check whether in digit range (‘0’-‘9’)
3 Otherwise, check whether it is in character range (‘a’-‘z’)
4 Otherwise, check whether it is in (‘A’-‘Z’)
5 Otherwise, check whether it is space (‘ ’)
• Work it by yourself first...

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)

• Leap year should satisfy one of follow two conditions


1 It is dividable by 4, but not by 100
2 It is dividable by 400
[Steps]
1 Accept input number
2 Check whether it is dividable by 400
3 If yes, it is leap year
4 Otherwise, check whether it is dividable by 4 and NOT dividable by
100
1 If yes, it is leap year
2 Otherwise, it is not leap year
Give your solution first....

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)

• See the result of following code

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)

• See the result of following code

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

• Do not put statements and conditions on the same line

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

• Following codes produce the same results


1 i n t main ( )
2 {
3 int a = 3 , b = 4 , c = 1;
1 i n t main ( )
4 i f (a > b)
2 {
5 {
3 int a = 3 , b = 4 , c = 1;
6 a = c;
4 a = a > b? c : b ;
7 }else{
5 p r i n t f ( ” a = %d” , a ) ;
8 a = b;
6 }
9 }
10 p r i n t f ( ” a = %d” , a ) ;
11 }

• “a⋇b” is a logic expression


• If it is true, c is assigned to the left
• Otherwise, d is assigned to the left

C Programming 52 / 59
Application: Convert lower case char to upper case (1)

• Given a char of unknown case, convert it to uppercase


• ‘a’-‘z’ to ‘A’-‘Z’
• Solution:
1 Check whether ch is in the range of ‘a’-‘z’
2 If it is in this range, ch = ch - 32
3 Otherwise, do not do anything

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)

• If you have to check one variable for many constant values


• switch-case is your friend:)

switch ( variable )
{
case option1 : statement1 ; break ;
case option2 : statement2 ; break ;
case option3 : statement3 ; break ;
d e f a u l t : statement4 ; break ;
}

• case option defines a jump label


• More than one statement after it possible without braces
• All statements until the next break; will be executed

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy