Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
90 views10 pages

United International University: Lab Sheet 3 Selection Statements in C

The document describes selection statements in C programming, including if, if-else, if-else if-else, nested if, and switch statements. It provides the syntax and flow diagrams of each statement, along with examples of how to write simple decision making code using each statement type. The purpose is to help students learn how to use selection statements to selectively execute code based on conditions.

Uploaded by

Shah wafiq
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)
90 views10 pages

United International University: Lab Sheet 3 Selection Statements in C

The document describes selection statements in C programming, including if, if-else, if-else if-else, nested if, and switch statements. It provides the syntax and flow diagrams of each statement, along with examples of how to write simple decision making code using each statement type. The purpose is to help students learn how to use selection statements to selectively execute code based on conditions.

Uploaded by

Shah wafiq
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/ 10

United International University

Dept. of Electrical and Electronic Engineering (EEE)

Course No. : EEE 122


Course Title: Structured Programming Laboratory

Lab Sheet 3
Selection Statements in C
Outcomes
After finishing this lab students should be able to ...

1. write simple decision making statements.

2. use the if selection statement and the if...else selection statement to select actions.

3. use switch Statement understand multiple selection using the switch selection statement.

Contents
1 C - Decision Making 1
1.1 if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 if...else statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 If...else if...else statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 C - nested if statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 C -switch statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2 Programming Examples 7

3 Practice session 9

4 Lab Assignments 9

1 C - Decision Making
Decision making structures require that the programmer specifies one or more conditions to be evalu-
ated or tested by the program, along with a statement or statements to be executed if the condition is
determined to be true, and optionally, other statements to be executed if the condition is determined
to be false.

C programming language assumes any non-zero and non-null values as true, and if it is either zero
or null, then it is assumed as false value.

C programming language provides the following types of decision making statements.

1.1 if statement
An if statement consists of a Boolean expression followed by one or more statements.
EEE 122: Structured Programming Laboratory 2

Syntax
i f ( condition ) {
/ / statement ( s )
}

Flow Diagram

Start

false
condition

true

Conditional Code

end

Fig: if condition flow chart

Example
# i n c l u d e < s t d i o . h>

i n t main ( void ) {
int a ;
printf ( ” \ nEnter a value ” ) ;
scanf ( ”%d” ,&a ) ;
i f ( a < 20 ) {
/ * i f c o n d i t i o n i s t r u e then p r i n t t h e f o l l o w i n g * /
printf ( ” a i s l e s s than 20\n” ) ;
}
printf ( ” value o f a i s : %d\n” , a ) ;
return 0;
}

1.2 if...else statement


An if statement can be followed by an optional else statement, which executes when the Boolean
expression is false.
Syntax
i f ( condition ) {
/ / statement ( s )
}
else {
/ / statement ( s )
}
EEE 122: Structured Programming Laboratory 3

Flow Diagram

Start

false
condition

true

if code if code

end

Fig: if-else condition flow chart

Example
# i n c l u d e < s t d i o . h>

i n t main ( void ) {
int a ;
printf ( ” \ nEnter a value ” ) ;
scanf ( ”%d” ,&a ) ;
i f ( a < 20 ) {
/ * i f c o n d i t i o n i s t r u e then p r i n t t h e f o l l o w i n g * /
printf ( ” a i s l e s s than 20\n” ) ;
}
else {
/ * i f c o n d i t i o n i s f a l s e then p r i n t t h e f o l l o w i n g * /
printf ( ” a i s g r e a t e r than 20\n” ) ;
}
printf ( ” value o f a i s : %d\n” , a ) ;
return 0;
}

1.3 If...else if...else statement


An if statement can be followed by an optional else if...else statement, which is very useful to test
various conditions using single if...else if statement. Once an else if succeeds, none of the remaining
else if’s or else’s will be tested.
Syntax
i f ( condition 1 ) {
/ * E x e c u t e s when t h e c o n d i t i o n 1 i s t r u e * /
}
e l s e i f ( condition 2 ) {
/ * E x e c u t e s when c o n d i t i o n 2 i s t r u e * /
}
e l s e i f ( condition 3 ) {
/ * E x e c u t e s when t h e c o n d i t i o n 3 i s t r u e * /
}
...
...
...
else {
/ * e x e c u t e s when t h e none o f t h e above c o n d i t i o n i s t r u e * /
}
EEE 122: Structured Programming Laboratory 4

Flow Diagram

Start

true
cond. 1 statement 1

false

true
cond. 2 statement 2

false

true
cond. 3 statement 3

false

statement 4
false

end

Fig: if-else condition flow chart

Example
# i n c l u d e < s t d i o . h>

i n t main ( void ) {
int a ;
printf ( ” \ nEnter a value ” ) ;
scanf ( ”%d” ,&a ) ;
i f ( a < 20 ) {
/ * i f c o n d i t i o n 1 i s t r u e then p r i n t t h e f o l l o w i n g * /
printf ( ” a i s l e s s than 20\n” ) ;
}
e l s e i f ( a < 30 ) {
/ * i f c o n d i t i o n 2 i s t r u e then p r i n t t h e f o l l o w i n g * /
printf ( ” a i s l e s s than 30\n” ) ;
}
e l s e i f ( a < 40 ) {
/ * i f c o n d i t i o n 3 i s t r u e then p r i n t t h e f o l l o w i n g * /
printf ( ” a i s l e s s than 40\n” ) ;
}
else {
/ * i f a l l c o n d i t i o n i s f a l s e then p r i n t t h e f o l l o w i n g * /
printf ( ” a i s g r e a t e r than 40\n” ) ;
}
printf ( ” value o f a i s : %d\n” , a ) ;
return 0;
}

1.4 C - nested if statements


It is always legal in C programming to nest if-else statements, which means you can use one if or else
if statement inside another if or else if statement(s).
EEE 122: Structured Programming Laboratory 5

Syntax
i f ( condition1 ) {
/ * E x e c u t e s when condtion1 i s t r u e * /
i f ( condition2 ) {
/ * E x e c u t e s when c o n d i t i o n 2 i s t r u e * /
}
}

Example
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
/* local variable definition */
int a = 100;
int b = 200;
/ * check t h e boolean c o n d i t i o n * /
i f ( a == 100 ) {
/ * i f c o n d i t i o n i s t r u e then check t h e f o l l o w i n g * /
i f ( b == 200 ) {
/ * i f c o n d i t i o n i s t r u e then p r i n t t h e f o l l o w i n g * /
printf ( ” Value o f a i s 100 and b i s 200\n” ) ;
}
}
printf ( ” E x a c t value o f a i s : %d\n” , a ) ;
printf ( ” E x a c t value o f b i s : %d\n” , b ) ;
return 0;
}

1.5 C -switch statements


A switch statement allows a variable to be tested for equality against a list of values. Each value is
called a case, and the variable being switched on is checked for each switch case.
Syntax
s w i tc h ( expression ) {
c a s e constant−expression :
statement ( s ) ;
break ; / * o p t i o n a l * /

c a s e constant−expression :
statement ( s ) ;
break ; / * o p t i o n a l * /

/ * you can have any number o f c a s e s t a t e m e n t s * /

default : / * optional * /
statement ( s ) ;
}
EEE 122: Structured Programming Laboratory 6

Flow Diagram

Start

expression case1 statement 1

case2 statement 2

case3 statement 3

case4 statement 4

Fig: switch statement flow chart

Example
# i n c l u d e < s t d i o . h>

i n t main ( void ) {
char grade = ' B ' ;

s w i tc h ( grade ) {
c a s e 'A ' :
printf ( ” E x c e l l e n t ! \ n” ) ;
break ;
case 'B ' :
c a s e 'C ' :
printf ( ” Well done \n” ) ;
break ;
c a s e 'D ' :
printf ( ”You passed \n” ) ;
break ;
case ' F ' :
printf ( ” B e t t e r t r y again \n” ) ;
break ;
default :
printf ( ” I n v a l i d grade \n” ) ;
}

printf ( ”Your grade i s %c \n” , grade ) ;


return 0;
}
EEE 122: Structured Programming Laboratory 7

2 Programming Examples
Example: 1
Description: Write a C program that check the condition in if. If it is true, it’s print a message
Source Code Output
# include <stdio . h> The entered number is 5
i n t main ( void ) {
int a=5;;
i f ( a==5)
printf ( ”The e n t e r e d number i s %d . \ n” , a ) ;
return 0;
}

Example: 2
Description: Write a C program that finds and displays maximum of two numbers.
Source Code Output
# include <stdio . h>
i n t main ( void ) {
int a , b ; Enter the first number:10
printf ( ” E n t e r t h e f i r s t number : ” ) ;
scanf ( ”%d” ,&a ) ; Enter the second number:20
printf ( ” E n t e r t h e second number : ” ) ; 20 is larger than 10
scanf ( ”%d” ,&b ) ;
i f ( a>b )
printf ( ”%d i s l a r g e r than %d” , a , b ) ;
else
printf ( ”%d i s l a r g e r than %d” , b , a ) ;
return 0;
}

Example: 3
Description: Write a C program that finds and displays maximum of three numbers.
Source Code Output
# include <stdio . h>
i n t main ( void ) {
i n t a , b , c , large ; Enter three numbers 10 20 8
printf ( ” E n t e r t h r e e numbers ” ) ;
scanf ( ”%d%d%d” ,&a ,&b ,&c ) ; Largest number is 20
i f ( a>b ) {
i f ( a>c )
large=a ;
else
large=c ;
}
else {
i f ( b>c )
large=b ;
else
large=c ;
}
printf ( ” L a r g e s t number i s %d\n” , large ) ;
return 0;
}
EEE 122: Structured Programming Laboratory 8

Example: 4
Description: Write a C program to print equivalent letter grade from numeric value. Compute the
grades as given in the following table.

Marks(n) Grade
n >=85 A
70 <= n <85 B
55 <= n <70 C
40 <= n <54 D
n <40 F
Source Code Output

# include <stdio . h>


i n t main ( void ) {
f l o a t num ; Enter marks of the subject: 45
char grade ; Mark is 45.000000
printf ( ” E n t e r marks o f t h e s u b j e c t : ” ) ;
scanf ( ”%f ” ,&num ) ;
Grade is D
i f ( num >=85)
grade= 'A ' ;
e l s e i f ( num >=70)
grade= ' B ' ;
e l s e i f ( num >=55)
grade= 'C ' ;
e l s e i f ( num >=40)
grade= 'D ' ;
else
grade= ' F ' ;
printf ( ”Mark i s %f \ nGrade i s %c \n” , num , grade ) ;
return 0;
}

Example: 5
Description: Write a C program to perform arithmetic calculation on integers using switch operator.
Source Code Output

# include <stdio . h>


i n t main ( void ) { Run1:
char op ;
int a , b ; Enter number operator and another
printf ( ” E n t e r number o p e r a t o r and anothernumber : ” ) ; number:
scanf ( ”%d %c %d” , &a , &op , &b ) ;
s w i tc h ( op ) {
100+50
c a s e '+ ' : Sum=150
printf ( ”Sum=%d\n” , a+b ) ;
break ;
c a s e '− ' : Run2:
printf ( ” D i f f e r e n c e = %d\n” , a−b ) ; Enter number operator and another
break ;
case ' * ' : number:
printf ( ” Product= %d\n” , a * b ) ; 100/50
break ;
case ' / ' :
Quotient= 2
printf ( ” Quotient= %d\n” , a / b ) ;
break ;
c a s e '% ' :
printf ( ” Quotient= %d\n” , a%b ) ;
default :
printf ( ” \ n I n v a l i d c h o i c e . ” ) ;
}
return 0;
}
EEE 122: Structured Programming Laboratory 9

3 Practice session
Sl Source Code
Practice 1
# include <stdio . h>
i n t main ( void ) {
i n t a =1 , b =3;
i f ( a==2)
i f ( b==3)
printf ( ” P r i n c e ” ) ;
printf ( ”Queen” ) ;
return 0;
}

Practice 2
# include <stdio . h>
i n t main ( void ) {
i n t a =1 , b =3;
i f ( a==2)
i f ( b==3)
printf ( ” P r i n c e ” ) ;
else
printf ( ”Queen” ) ;
else
printf ( ”King” ) ;
return 0;
}

Practice 3
# include <stdio . h>
i n t main ( void ) {
i n t temp =32;
i f ( temp < 80 )
i f ( temp > 60 )
printf ( ” Nice day ! ” ) ;
else
printf ( ” Sure IT i s hot ! ” ) ;
return 0;
}

Practice 4
# include <stdio . h>
i n t main ( void ) {
i n t a =2 , x =10;
i f ( a==2)
i f ( x==8)
printf ( ” a i s equal t o 2 and x i s equal t o 8 n” ) ;
else
printf ( ” a i s not equal t o 2 ” ) ;
return 0;
}
EEE 122: Structured Programming Laboratory 10

4 Lab Assignments
1. An Electric power distribution company charges its domestic consumers as follows :

Consumption units Rate of charge


0-100 4.5 taka per unit
101-200 100 taka plus 5 taka per unit excess of 100
201-400 250 taka plus 6 taka per unit excess of 200
401 and above 400 taka plus 7 taka per unit excess of 400

Write a C program which reads the customers amount of power consumed and prints the amount
to be paid by the customer.

2. Write a program in C which gives the solution of a quadratic equation ,using the formula

−b± b2 −4ac
x = 2a . You must find any kind of roots (either real or maginary).In case of
imaginary roots ,the roots should be of the form c+id and c- id. Keep the option that when one
enter a = 0 as input, it prints: This is not a quadratic equation.

3. Write a program in C asking the user to enter 2 digit number, then prints the English word for it.
Suppose you enter ’41’ the printf function prints out ’forty one’. Use switch statement for this
purpose.

Acknowledgment
First OBE version, prepared by: Second Update , prepared by:
B.K.M. Mizanur Rahman, Nazmul Alam,
Assistant Professor, Part Time Faculty,
Department of EEE, UIU Department of EEE, UIU

You might also like