Lecture 3
Lecture 3
Lecture: 3
Control Statements
Avishek Das
Department of CSE
CUET
Introduction
➢An operator is a symbol that tells the computer to perform certain
manipulations.
➢An expression is a sequence of operands and operators that
reduces to a single value.
➢C operators can be classified into a number of categories.
➢Arithmetic operators (+,-,*,/)
➢Relational operators (> , <, <=)
➢Logical operators (&&, ||, !)
➢Assignment operators (=)
➢Increment and decrement operators (++ , --)
➢Conditional operators (?..:)
➢Bitwise operators (&, |, ^, <<, >>)
16/09/2022 Department of CSE, Chittagong University of Engineering & Technology 2
3
Arithmetic operators
➢The arithmetic operators used for numeric calculations. They are two
types:
➢Unary Arithmetic Operators
+x, -y, ++, --, !, ~
➢Binary Arithmetic Operators
Operator meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% modulo division
Example
#include<stdio.h>
int main()
{
int months, days ;
printf("Enter days: \n”) ;
scanf("%d”, &days) ;
months = days / 30 ;
days = days % 30 ;
printf("Months =%d Days= %d\n", months, days);
}
16/09/2022 Department of CSE, Chittagong University of Engineering & Technology 4
5
Operator’s Precedence
Note:
➢The precedence of arithmetic operators
1. Brackets ()
2. Unary ++ or --
3. * / %
4. + -
36 / 12 * 4
10 + 23 % 4 * 5
(10 + 23) % 4 * 5
Arithmetic expressions
➢An arithmetic expression is a combination of variables, constants,
and operators.
➢For example,
a*b-c → a*b-c
(m+n)(x+y) → (m+n)*(x+y)
ax2+bx+c → a*x*x+b*x+c
ARITHMETIC EXPRESSIONS
➢ Integer Arithmetic: When both operands are integers
for example if a=17 and b=4
Expression Result
a+b 21
a-b 13
a*b 68
a/b 4
a%b 1
ARITHMETIC EXPRESSIONS
➢Real Arithmetic: When both operands are float types
for example if a=12.4 and b=3.1
Expression Result
a+b 15.5
a-b 9.3
a*b 38.44
a/b 4.0
a%b NA
Arithmetic expressions
➢Mixed-mode Arithmetic: When one operand is integers
and another is real .for example if a=12 and b=2.5
Expression Result
a+b 14.5
a-b 9.5
a*b 30.0
a/b 4.8
Operator Meaning
< less that
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
➢5 < 6 TRUE 1
➢4.5<10 TRUE 1
➢-34 + 8 > 23 - 5 FALSE 0
➢4.5<-10 FALSE 0
➢5==0 FALSE 0
➢if a=3, b=2, c =1; then a > b > c is ?
➢3 >= 2 = = -4 < 0
➢The logical expression given above is TRUE only if a>b is true and
x==10 is true. If either (or both) of them are false, the expression
is false.
a = a+1 a += 1
a = a-1 a -= 1
a = a * (n+1) a *= n+1
a = a / (n+1) a /= n+1
a=a%b a %= b
➢What appears on the left-hand side need not be repeated and therefore
it becomes easier to write.
➢In both cases, the effect is to increment n. But the expression ++n
increments n before its value is used, while n++ increments n after its
value has been used.
17/09/2022 Department of CSE, Chittagong University of Engineering & Technology 20
Increment and decrement operators
➢ Prefix Increment/Decrement:
The statement y = ++x means first increment the value of x by 1, then assign
the value to y. this statement belongs to the following two statement:
#include<stdio.h> Output
main()
{
int x=8;
printf(“x=%d\n”,x); x=8
printf(“x=%d\n”,++x); x=9
printf(“x=%d\n”,x); x=9
printf(“x=%d\n”,--x); x=8
printf(“x=%d”,x); x=8
}
#include<stdio.h> Output
main()
{
int x=8;
x=8
printf(“x=%d\n”,x);
printf(“x=%d\n”,x--);
x=8
printf(“x=%d\n”,x); x=7
printf(“x=%d\n”,x++); x=7
printf(“x=%d”,x); x=8
}
Syntax:
if (condition) True
Statement set-1
{ condition
Statement set-1;
} False
else
{ Statement set-2
Statement set-2;
}
Next Statement;
Next Statement
switch(i){
case 1:
printf(“ONE”);
break;
case 2: Output:
printf(“TWO”);
break; TWO
case 3:
printf(“THREE”);
break;
default:
printf(“INVALID”);
break;
}
Example
switch (departmentCode){
case 4 :
printf(“CSE”);
break;
case 2 :
printf(“EEE”);
break;
case 8 :
printf(“ETE”);
break;
case 7 :
printf(“PME”);
break;
case 11:
print(“BME”);
}
Example
int iNum = 2;
switch(iNum) {
case 1.5:
printf(“ONE AND HALF”); Case 1.5: this is invalid
because the values in
break; case statements must be
case 2: Integers or characters
printf(“TWO”);
break;
case ‘A’ :
printf(“A character”);
}
Example
#include<stdio.h>
int main()
{
char ch;
printf("Enter the vowel:");
scanf("%c", &ch);
switch(ch) {
case 'a' : printf("Vowel");
break;
case 'e' : printf("Vowel");
break;
case 'i' : printf("Vowel");
break;
case 'o' : printf("Vowel");
break;
case 'u' : printf ("Vowel");
break;
default : printf("Not a vowel");
}
return 0;
}