CS Unit - 2
CS Unit - 2
Faculty:
Darshana A Naik
Shilpa H
Sunitha R S
3
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Categories of Operators
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and Decrement operators
Conditional operators
Bitwise operators
Special operators
4
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
ARITHMETIC OPERATORS
The operators are
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulo division)
5
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Integer Arithmetic
When both the operands in a single arithmetic expression
are integers, the expression is called an integer expression
, and the operation is called integer arithmetic.
If a=14 and b=4, then
a-b=10
a+b=18
a*b=56
a/b=3
a%b=2
6
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Integer Arithmetic
What is the results of the following?
6/7=?
3/7=?
21/3=?
7
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Integer Arithmetic
During modulo division the sign of the result is
always the sign of the first operand.
-14 % 3 = -2
-14 % -3 = -2
14 % -3 = 2
8
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Real Arithmetic
An arithmetic operation involving only real
operands is called real arithmetic.
If x and y are floats then we will have
1) x = 6.0 / 7.0 = 0.857143
2) y = 1.0 / 3.0 = 0.333333
10
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
C Program for the arithmetic
operators
What is the output of the following?
11
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
RELATIONAL OPERATORS
Comparisons can be done with the help of
relational operators.
The expression containing a relational operator is
termed as a relational expression.
The value of a relational expression is either one
or zero.
12
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
RELATIONAL OPERATORS
1) < (is less than)
2) <= (is less than or equal to)
3) > (is greater than)
4) >= (is greater than or equal to)
5) = = (is equal to)
6) != (is not equal to)
13
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Relational Operators example in
C
14
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Relational Operators example in
C
15
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LOGICAL OPERATORS
Following are three logical operators.
&& (logical AND)
|| (logical OR)
! (logical NOT)
Eg:
1) if(age>55 && sal<1000)
2) if(number<0 || number>100)
16
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Truth Table
18
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LOGICAL OPERATORS-
Example
19
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LOGICAL OPERATORS-
Example- Output
Output
20
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
ASSIGNMENT OPERATORS
The usual assignment operator is ‘=’.
In addition, C has a set of ‘shorthand’ assignment
operators.
Eg: x += y+1;
This is same as the statement x=x+(y+1);
21
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
ASSIGNMENT OPERATORS
a + =1 a = a + 1
a - = 1 a = a – 1
a *= n + 1 a = a * (n+1)
a /= n + 1 a = a / (n+1)
a %= b a = a % b
22
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND
DECREMENT OPERATORS
These are the increment and decrement operator:
++ and --
The operator ++ adds 1 to the operands while -- subtracts
1.
It takes the following form:
++m; or m++
--m; or m—
23
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;
x=i++;
printf("x: %d",x);
printf("i: %d",i);
}
24
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;
x=i++;
Output:
printf("x: %d",x);
printf("i: %d",i);
10
}
11
25
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;
x=++i;
printf("x: %d",x);
printf("i: %d",i);
}
26
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;
x=++i;
Output:
printf("x: %d",x);
printf("i: %d",i);
11
}
11
27
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;
x=i--;
printf("x: %d",x);
printf("i: %d",i);
}
28
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;
x=i--;
Output:
printf("x: %d",x);
printf("i: %d",i);
10
}
9
29
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;
x=--i;
printf("x: %d",x);
printf("i: %d",i);
}
30
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,i; i=10;
x=--i;
Output:
printf("x: %d",x);
printf("i: %d",i);
9
}
9
31
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,a,b,c; a = 2;
b = 4;
c = 5;
x = a-- + b++ -++c;
printf("x: %d",x);
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
94
INCREMENT AND DECREMENT
OPERATORS- Example
void main()
{
int x,a,b,c; a = 2;
b = 4;
c = 5;
int x,a,b,c; a = 2;
b = 4;
c = 5;
a:7
O/p: a:7
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
38
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5;
printf("a: %d\n",a--);
printf("a: %d\n",--a);
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
39
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5;
printf("a: %d\n",a--); a:5
printf("a: %d\n",--a); a:3
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
40
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5,x;
x=a--;
printf("a: %d\n",x);
printf("a: %d\n",a);
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
41
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5,x;
x=a--;
printf("a: %d\n",x);
printf("a: %d\n",a); a:5
a:4
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
42
Exercise-What is the output?
#include <stdio.h> int
main()
{
int a=5,x;
x=a--;
printf("a: %d\n",a);
printf("a: %d\n",x);
x=--a;
printf("a: %d\n",a);
printf("a: %d\n",x);
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
43
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5,x;
x=a--;
a:4
printf("a: %d\n",a); a:5
printf("a: %d\n",x); a:3
x=--a; a:3
printf("a: %d\n",a);
printf("a: %d\n",x);
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
44
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5;
printf("%d",a--,a++,--a);
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
45
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5;
printf("%d",a--,a++,--a); return a:5
0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
46
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5;
printf("%d%d",a--,a++,--a);
return 0;
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
47
Exercise-What is the output?
#include <stdio.h>
int main()
{
int a=5;
printf("%d%d",a--,a++,--a);
return 0;
a:5, 4
}
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
48
Exercise-What are the values of n1,
n2, n3 and n4 at the end?
int n1,n2,n3,n4;
n1 = 1;
n2 = ++n1;
n3 = ++n1;
n4 = n1++;
type conversions.
Example
C = (float)9 / 5 * ( f – 32 )
float to int conversion causes truncation of fractional part
116
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Example on implicit type casting
#include<stdio.h> main ( )
{
int i=17;
char c=’c’; int sum; sum=i+c;
printf(“Value of sum:%d\n”,sum);
}
Output:
Value of mean: 3.400000 Output:
Value of mean: 3.000000
...
default: :
statement(s)
break ;
}
printf("End");
}
82
Department of Computer Science and Engineering
The while Statement
• A while statement has the following syntax:
while ( condition )
statement;
• If the condition is true, the statement is executed
• Then the condition is evaluated again, and if it is still true, the
statement is executed again
• The statement is executed repeatedly until the condition becomes
false
83
Department of Computer Science and Engineering
Logic of a while Loop
condition
evaluated
true false
statement
5
Department of Computer Science and Engineering
The while Statement
• An example of a while statement:
int count = 0; while (count < 2)
{
printf("Welcome to Java!");
count++;
}
13
Department of Computer Science and Engineering
Trace while Loop
Initialize count
7
Department of Computer Science and Engineering
Trace while Loop, cont.
(count < 2) is true
int count = 0;
while (count < 2)
{
printf("Welcome to C!"); count++;
}
8
Department of Computer Science and Engineering
Trace while Loop, cont.
Print Welcome to Java
int count = 0; while
(count < 2)
{
printf("Welcome to C!");
count++;
}
9
Department of Computer Science and Engineering
Trace while Loop, cont.
Increase count by 1
int count = 0; while (count < 2) count is 1 now
{
printf("Welcome to C!");
count++;
}
10
Department of Computer Science and Engineering
Trace while Loop, cont.
(count < 2) is still true since count
int count = 0; is 1
11
Department of Computer Science and Engineering
Trace while Loop, cont.
Print Welcome to Java
int count = 0; while
(count < 2)
{
printf("Welcome to C!");
count++;
}
12
Department of Computer Science and Engineering
Trace while Loop, cont.
Increase count by 1
int count = 0; while (count < 2) count is 2 now
{
printf("Welcome to C!");
count++;
}
13
Department of Computer Science and Engineering
animation
14
Department of Computer Science and Engineering
Trace while Loop
The loop exits. Execute the next
int count = 0; while (count < 2) statement after the loop.
{
printf("Welcome to C!"); count++;
}
15
Department of Computer Science and Engineering
The do-while Statement
• Syntax
do action
while (condition)
• How it works:
– execute action action
– if condition is true then execute action
again
– repeat this process until condition
evaluates to false.
• action is either a single statement or a true
group of statements within braces. condition
false
int count = 0; do
{
7
Department of Computer Science and Engineering
Trace do while Loop
int count = 0; do
{
Welcome to C!
printf("Welcome to C!");
count++;
} while (count < 2) ;
7
Department of Computer Science and Engineering
Trace do while Loop
int count = 0; do
{
printf("Welcome to C!");
count=1
count++;
} while (count < 2) ;
7
Department of Computer Science and Engineering
Trace do while Loop
int count = 0; do
{
printf("Welcome to C!"); count++;
1<2
} while (count < 2) ;
7
Department of Computer Science and Engineering
Trace do while Loop
int count = 0; do
{
Welcome to C!
printf("Welcome to C!"); count+
+;
} while (count < 2) ;
7
Department of Computer Science and Engineering
Trace do while Loop
int count = 0; do
{
printf("Welcome to C!");
Count=2
count++;
} while (count < 2) ;
7
Department of Computer Science and Engineering
Trace do while Loop
int count = 0; do
{
printf("Welcome to C!"); count++;
2<2
} while (count < 2) ;
7
Department of Computer Science and Engineering
Infinite Loops
• An example of an infinite loop:
int count = 1;
while (count <= 25)
{
printf(count);
count = count - 1;
}
103
Department of Computer Science and Engineering
Nested Loops
• How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 3)
{
count2 = 1;
while (count2 <= 3)
{
printf ("Here");
count2++;
}
count1++;
}
104
Department of Computer Science and Engineering
What is the output of the following code?
#include<stdio.h> void
main()
{
int val=1; do{
val++;
++val;
}while(val++>25);
printf("%d\n",val);
}
#include<stdio.h> void
main()
{
int val=1; do{
val++;
++val;
}while(val++>25);
printf("%d\n",val);
}
Answer: 4
#include<stdio.h> int
main()
{
int val=1; while(val<=5)
printf("Hi"); val++;
}
#include<stdio.h>
Hi
int main() Hi
{ Hi
Hi
int val=1; Hi
while(val<=5)
{
printf("Hi"); val++;
}
}
#include<stdio.h> int
main()
{
int val=1;
while(val<=5);
{
printf("Hi"); val++;
}
}
void main( )
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
}
Expected Output :
Number is : 1 and cube of the 1 is :1 Number is : 2 and cube of the 2 is :8
Number is : 3 and cube of the 3 is :27 Number is : 4 and cube of the 4
is :64 Number is : 5 and cube of the 5 is :125
10 9 8 6 5 4 3 2 1 0
return 0;
}
return 0;
}
}
printf("%d ", j);
j++;
}while(j<10); return 012345689
0;
}
return 0;
}
{
break; Value of variable num is 0 Value of
} variable num is 1 Value of variable
num++;
} num is 2 Out of while loop
printf("Out of while-loop");
return 0;
}
987654321
Department of Computer Science and Engineering
Interesting examples in for loop
main()
{
int i; for(i=0;i<5;i++);
printf("%d ",i);
}
6
Department of Computer Science and Engineering
Interesting examples in for loop
#include<stdio.h> main()
{
2 1
int x=1,y=1; 3 1
for(;y;printf("%d %d\n",x,y)) 4 1
5 1
y = x++ <= 5; 6 1
} 7 0