You are on page 1of 20

CSE 191 – COMPUTER PROGRAMMING

Najia Manjur
Lecturer
Department of Computer Science & Engineering (CSE)
Military Institute of Science & Technology (MIST)
Mirpur, Dhaka – 1216, Bangladesh
Type Casting
a way to convert a variable from one data type to another data type.

main()
{
int i=7;
float f=8.5;
(i + f) % 4 // invalid; the first operand (i + f ) is floating-point

((int) (i + f)) % 4 // valid; forces the first operand to be an integer


}

2
Precedence
2 * ((i % 5) * (4 + (j - 3) / (k + 2)))
int i=8, j=15, k=4;

2 x ((8 % 5) x (4 + ( 1 5 - 3) / (4 + 2)))
= 2 x (3 x (4 + (12/6)))
= 2 x (3 x (4 + 2))
= 2 x (3 x 6)
= 2 x 18
= 36

3
Precedence (cont.)
w = 2 * ((i % 5) * (4 + (j - 3) / (k + 2)));

u=i%5;
v = 4 + (j - 3) / (k + 2);
w = 2 * (U * v);

4
Control Statement (if……else)
If(expression_statement)
{
statements
}
else
{
statements
}

5
Control Statement
If(expression_statement) true (1)

{
statements
}
else
{
statements
}

6
Control Statement
If(expression_statement) false(0)

{
statements
}
else
{
statements
}

7
Control Statement

8
Control Statement (flowchart to code)

9
Control Statement (code to flowchart)

#include<stdio.h>
main()
{
int a=100;
if(a<20)
{
printf(“a is less than 20”);
}
else
{
printf(“a is not less than 20”);
}

10
Control Statement (if…else if….else)

11
Control Statement (if…else if….else)

12
Control Statement (code to flowchart)
Program to find out whether a number is odd or even or zero

13
Control Statement (code to flowchart)
Program to find out whether a number is odd or even or zero

#include<stdio.h>

main()
{
int a=91;
if(a==0)
{ printf(“a is zero”); }
else if(a%2==0)
{ printf(“a is even); }
else
{ printf(“a is odd”); }
}

14
Control Statement (flowchart to code)
Ari

15
Switch Case
switch (expression)
{
case value-1 :
block-1
break;
case value-2 :
block-2
break;
…………………
…………………
default:
default-block
break;
}
Switch Case
 expression can be integer or character type.
 value-1,value-2,…. are constants (also known as case labels).
 block-1,block-2,…can contain zero or more statements.
 No need to put braces around the blocks.
 Case-labels end with a colon.
 break statement transfers the control out of the switch statement.
 default label is optional. If present, will be executed when the
expression does not find a matching case label.
 At most one default label.
 Permitted to nest switch statement.
Switch Case
 WHY Switch Case?
 switch statement tests the value of a given variable(or expression) against
a list of case values and when a match is found, a block of statements
associated with that case is executed
 Program complexity increases with number of alternatives, in case of if
statement.
 built-in multiway decision statement : switch case

 Switch Case (WITH NO break)


 CAUTION WHEN READING char WITH scanf()
 DEALING WITH EXPRESSION IN Switch Case
Reference
 Text Books
 Schaum’s Outlines Programming with C (2nd Edition) – Byron Gottfried

Chapter – 6 (6.1, 6.2) (including all examples)


End of Slides

You might also like