Department of
Computer Science and Engineering
IPL - OUTREACH 2024
C PROGRAMMING
Date: 11.05.2024 - 15.05.2024
Dr.P.Durgadevi,Assistant Professor(S.G)
Agenda
Operators in C
Control Statements
Conditional Statements (if, else if, else)
Looping Constructs (for, while, do-while)
Dr.P.Durgadevi,Assistant Professor(S.G)
Operators
• Need of Operators
Operators in programming are symbols or keywords that represent
computations or actions performed on operands.
Operands can be variables, constants, or values, and the combination of
operators and operands form expressions.
Operators play a crucial role in performing various tasks, such as
arithmetic calculations, logical comparisons, bitwise operations, etc.
Basic classification of operators
Arithmetic Operators
Topics
• Arithmetic Operators
• Operator Precedence
• Evaluating Arithmetic Expressions
• In-class Project
• Incremental Programming
Dr.P.Durgadevi,Assistant Professor(S.G)
Arithmetic Operators in C
Name Operator Example
Addition + num1 +
num2
Subtraction - initial - spent
Multiplication * fathoms * 6
Division / sum / count
Modulus % m%n
Dr.P.Durgadevi,Assistant Professor(S.G)
Division
• If both operands of a division
expression are integers, you will get an
integer answer. The fractional portion
is thrown away.
• Examples : 17 / 5 = 3
4 / 3 = 1
35 / 9 = 3
Dr.P.Durgadevi,Assistant Professor(S.G)
Division (con’t)
• Division where at least one operand is a
floating point number will produce a
floating point answer.
• Examples : 17.0 / 5 = 3.4
4 / 3.2 = 1.25
35.2 / 9.1 = 3.86813
• What happens? The integer operand is
temporarily converted to a floating
point, then the division is performed.
Dr.P.Durgadevi,Assistant Professor(S.G)
Division By Zero
• Division by zero is mathematically
undefined.
• If you allow division by zero in a program, it
will cause a fatal error. Your program will
terminate execution and give an error
message.
• Non-fatal errors do not cause program
termination, just produce incorrect results.
Common examples of fatal errors include segmentation faults, invalid
memory accesses, and divide-by-zero errors.
Examples of non-fatal errors include input validation errors, file-not-found
errors, and network connection failures.
Dr.P.Durgadevi,Assistant Professor(S.G)
Modulus
• The expression m % n yields the integer
remainder after m is divided by n.
• Modulus is an integer operation -- both
operands MUST be integers.
• Examples : 17 % 5 = 2
6%3 = 0
9%2 = 1
5%8 = 5
Dr.P.Durgadevi,Assistant Professor(S.G)
Uses for Modulus
• Used to determine if an integer value is even or odd
5 % 2 = 1 odd 4 % 2 = 0 even
If you take the modulus by 2 of an integer, a result of
1 means the number is odd and a result of 0 means
the number is even.
Dr.P.Durgadevi,Assistant Professor(S.G)
Arithmetic Operators
Rules of Operator Precedence
Operator(s) Precedence & Associativity
Evaluated first. If nested (embedded),
() innermost first. If on same level, left to right.
* / % Evaluated second. If there are several,
evaluated left to right
Evaluated third. If there are several, evaluated
+ - left to right.
= Evaluated last, right to left.
Dr.P.Durgadevi,Assistant Professor(S.G)
Using Parentheses
• Use parentheses to change the order in which an
expression is evaluated.
• a+b*c Would multiply b * c first, then add a to
the result.
• If you really want the sum of a and b to be multiplied
by c, use parentheses to force the evaluation to be
done in the order you want.
(a + b) * c
• Also use parentheses to clarify a complex expression.
Dr.P.Durgadevi,Assistant Professor(S.G)
Practice With Evaluating
Expressions
Given integer variables a, b, c, d, and e,
where a = 1, b = 2, c = 3, d = 4,
Evaluate the following expressions:
a+b-c+d
1. a + ba -*c b
+ d/=c4
2. a * b1 /+c a= 0* b % c
3. 1 + a * b % c = 3
4. a + da %+ bd- %c =b
-2- c
5. e = eb ==db+ =c /db +- a,ce/=b4 - a
Dr.P.Durgadevi,Assistant Professor(S.G)
A Sample Project
• Let’s write a program that computes and
displays the volume and surface area of a
cube.
• Procedure:
– Use the pseudocode/algorithm
– Convert the algorithm to code
– Clean up the code (spacing, indentation,
commenting)
Dr.P.Durgadevi,Assistant Professor(S.G)
The Box - Pseudocode
Display “Enter the height: “
Read <height>
While (<height> <= 0 )
Display “The height must be > 0”
Display “Enter the height: “
Read <height>
End_while
Dr.P.Durgadevi,Assistant Professor(S.G)
The Box - Pseudocode (con’t)
Display “Enter the width: “
Read <width>
While (<width> <= 0 )
Display “The width must be > 0”
Display “Enter the width: “
Read <width>
End_while
Dr.P.Durgadevi,Assistant Professor(S.G)
The Box - Pseudocode (con’t)
Display “Enter the depth: “
Read <depth>
While (<depth> <= 0 )
Display “The depth must be > 0”
Display “Enter the depth: “
Read <depth>
End_while
Dr.P.Durgadevi,Assistant Professor(S.G)
The Box - Pseudocode (con’t)
<volume> = <height> X <width> X <depth>
<surface1> = <height> X <width>
<surface2> = <width> X <depth>
<surface3> = <height> X <depth>
<surface area> = 2 X (<surface1> + <surface2> + <surface3>)
Dr.P.Durgadevi,Assistant Professor(S.G)
The Box - Pseudocode (con’t)
Display “Height = “, <height>
Display “Width = “, <width>
Display “Depth = “, <depth>
Display “Volume = “, <volume>
Display “Surface Area = “, <surface area>
Dr.P.Durgadevi,Assistant Professor(S.G)
Good Programming Practice
• It is best not to take the “big bang” approach to
coding.
• Use an incremental approach by writing your code in
incomplete, yet working, pieces.
• For example, for your projects,
– Don’t write the whole program at once.
– Just write enough to display the user prompt on the
screen.
– Get that part working first (compile and run).
– Next, write the part that gets the value from the
user, and then just print it out.
Good Programming Practice
– Get that working (compile and run).
– Next, change the code so that you use the value in a
calculation and print out the answer.
– Get that working (compile and run).
– Continue this process until you have the final
version.
– Get the final version working.
Always have a working
version of your program!
Dr.P.Durgadevi,Assistant Professor(S.G)
Using the Incremental
Approach
• Let’s think about how we could have
developed the volume and surface area
program incrementally.
Dr.P.Durgadevi,Assistant Professor(S.G)
Operator
Definition
An operator is a symbol that operates on a value or a
variable
Operator Types
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Assignment operators
Conditional operators
Special operators
16-07-2020 Department of Computer Science and Engineering
Operator-Types
Arithmetic operators
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
remainder after division
%
(modulo division)
16-07-2020 Department of Computer Science and Engineering
Operator-Types
Arithmetic operators
/ Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
16-07-2020 Department of Computer Science and Engineering
Operator-Types
Relational operators
Operator Meaning of Operator Example
5 == 3 is evaluated to
== Equal to
0
5 > 3 is evaluated to
> Greater than
1
5 < 3 is evaluated to
< Less than
0
5 != 3 is evaluated to
!= Not equal to
1
Greater than or equal 5 >= 3 is evaluated to
>=
16-07-2020 to Department of Computer Science and Engineering
1
Operator-Types
Relational operators
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
16-07-2020 Department of Computer Science and Engineering
Operator-Types
Logical operators
Operato
Meaning of Operator Example
r
If c = 5 and d = 2 then,
Logical AND. True only
& expression ((c==5) &&
if all operands are true
(d>5)) equals to 0.
Logical OR. True only if If c = 5 and d = 2 then,
|| either one operand is expression ((c==5) ||
true (d>5)) equals to 1.
!
Logical NOT. True only if If c = 5 then, expression !
the operand is 0 (c==5) equals to 0.
16-07-2020 Department of Computer Science and Engineering
Operator-Types
Logical operators
/ Working of logical operators
#include <stdio.h>
int main()
{ int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a == b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;}
16-07-2020 Department of Computer Science and Engineering
Operator-Types
Bitwise operators
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
16-07-2020 Department of Computer Science and Engineering
Operator-Types
Assignment operators
Operat Exampl
Same as
or e
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
16-07-2020 Department of Computer Science and Engineering
Operator-Types
Assignment operators
// Working of assignment operators
#include <stdio.h>
int main()
{ int a = 5, c; c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c); c -= a;
// c is 5 printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c); c /= a; // c is 5
printf("c = %d\n", c); c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
16-07-2020 Department of Computer Science and Engineering
Special Operator
Conditional operators
Operator Meaning of operators
?: Conditional Expression
Comma operators
Operator Meaning of operators
, used to link related expressions
together
16-07-2020 Department of Computer Science and Engineering
Special Operator
The sizeof operators
Operato
Meaning of operators
r
sizeof a unary operator that returns the size of data (constants, variables,
array, structure, etc).
16-07-2020 Department of Computer Science and Engineering
Special Operator
The sizeof operators
#include <stdio.h>
int main()
{
int a; float b; double c; char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\
n",sizeof(c)); printf("Size of char=%lu
byte\n",sizeof(d)); return 0;
}
16-07-2020 Department of Computer Science and Engineering
Special Operator
Increment / Decrement operators
Operator Meaning of operators
++ Increment Operator
-- Decrement Operator
Note:
Increment ++ increases the value by 1 whereas decrement -- decreases the value
by 1. These two operators are unary operators, meaning they only operate on a
single operand
16-07-2020 Department of Computer Science and Engineering
Special Operator
Increment / Decrement operators
/ Working of increment and decrement operators
#include <stdio.h>
int main()
{ int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
16-07-2020 Department of Computer Science and Engineering
Expressions
An expression is a formula in which operands are linked to each other by
the use of operators to compute a value.
16-07-2020 Department of Computer Science and Engineering
Types of Expressions
16-07-2020 Department of Computer Science and Engineering
Expressions Examples
Constant expressions: Constant Expressions consists of only constant
values. A constant value is one that doesn’t change.
Examples: 5, 10 + 5 / 6.0, 'x’
Integral expressions: Integral Expressions are those which produce integer
results after implementing all the automatic and explicit type conversions.
Examples: x, x * y, x + int( 5.0)
Floating expressions: Float Expressions are which produce floating point
results after implementing all the automatic and explicit type conversions.
Examples: x + y, 10.75 where x and y are floating point variables
16-07-2020 Department of Computer Science and Engineering
Expressions Examples
Relational expressions: Relational Expressions yield results of type which
takes a value true or false.
Examples: x <= y, x + y > 2
Logical expressions: Logical Expressions combine two or more relational
expressions and produces bool type results.
Examples: x > y && x == 10, x == 10 || y == 5
Bitwise expressions: Bitwise Expressions are used to manipulate data at bit
level. They are basically used for testing or shifting bits.
Examples: x << 3 - shifts three bit position to left
y >> 1 - shifts three bit position to left
16-07-2020 Department of Computer Science and Engineering
Real time Uscases for Operators
Question: In a banking application, you need to calculate compound
interest for various account types. Which C operators would be useful for
performing arithmetic calculations on principal, interest rate, and time?
CLICK
Department of Computer Science and Engineering
Real time Uscases for Operators
Question: A manufacturing company needs to determine whether a
machine is operating efficiently based on its temperature readings. Which C
operators would be appropriate for comparing temperature values?
CLICK
Department of Computer Science and Engineering
Real time Uscases for Operators
Question: You are developing a game where the player's score increases
based on their performance. Which operators in C would be handy to
increment and decrement the player's score?
CLICK
Department of Computer Science and Engineering
Thank You
Department of Computer Science and Engineering