You are on page 1of 24

CSI 124

Structured Programming Language


Lecture 02
Operators – if else
What is Operator
• An Operator is a symbol that use to perform certain
mathematic or logical operations.
• Operator is used to manipulate data and variables 
Types of Operators
• In C Programming operator classified into
various categories.
– Arithmetic operators.
– Relational operators.
– Logical operators.
– Assignment operators.
Arithmetic operators
• Arithmetic operators used to perform arithmetic
operations.
• There are following arithmetic operators in C
language.
Operato Meaning
r
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo(Return remainder of division) 
Relational Operators
• Relational operators compare between two
operands and return in terms of true or false.
• There are following relational operators.
Operator Meaning
> Greater then
< Less then
>= Greater then Equal to
<= Less then Equal to
== Equal to
!= Not Equal to 
Relational Operators
• true will return 1 and false will return 0.
• Example:
void main()
{
int a = 5, b= 3;
printf(“%d%d”,a>b,a==b);
}
Logical operator
• A logical operator is used to compare or
evaluate logical and relational expressions.
• There are following Logical operators.
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Logical operator
• Example
void main()
{
int a = 5, b= 3;
printf(“%d%d”,(a>b)&&(a!=b),!(b>a)||(b==a));
}
Assignment operator
• An assignment operator is used to assign a
constant or a value of one variable to another.
• = is a assignment operator.
• You can use the assignment for multiple
assignments as follows:
• x = y = z = 20
• x+=10 // x=x+10
Assignment operator
• Example:
void main()
{
int a=5,b=3;
a+=b;
b*=a;
printf(“%d%d”,a,b);
}
C PROGRAMMING OPERATORS

An operator is a symbol that operates on a value or a variable.


For example: + is an operator to perform addition.
C has a wide range of operators to perform various operations.
C Arithmetic Operators
Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

remainder after division


%
(modulo division)
C PROGRAMMING OPERATORS
Example : 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;
}
C PROGRAMMING OPERATORS

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;
}
C PROGRAMMING OPERATORS

Assignment Operators
Operator Example Same as

= 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
C PROGRAMMING OPERATORS
Example : Assignment Operators
#include <stdio.h> c *= a; // c is 25
int main() printf("c = %d\n", c);
{ c /= a; // c is 5
int a = 5, c; printf("c = %d\n", c);
c %= a; // c = 0
c = a; // c is 5 printf("c = %d\n", c);
printf("c = %d\n", c);
c += a; // c is 10 return 0;
printf("c = %d\n", c); }
c -= a; // c is 5
printf("c = %d\n", c);
C PROGRAMMING OPERATORS
Relational Operators
Meaning of
Operator Operator Example

== Equal to 5 == 3 is evaluated to 0

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5 != 3 is evaluated to 1

>= Greater than or equal to 5 >= 3 is evaluated to 1

Less than or equal to 5 <= 3 is evaluated to


<=
0
C PROGRAMMING OPERATORS
Example 4: 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;
}
C PROGRAMMING OPERATORS
Logical Operators
Operator Meaning Example

If c = 5 and d = 2 then,
&& Logical AND. True only if expression ((c==5) &&
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 true expression ((c==5) ||
(d>5)) equals to 1.

If c = 5 then,
Logical NOT. True only if
! the operand is 0 expression !(c==5)
equals to 0.
C PROGRAMMING OPERATORS

The sizeof operator


The sizeof is a unary operator that returns the size of data
(constants, variables, array, structure, etc).

#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;
}
Conditional Statement
Conditional Statement
• Conditional Statements Are Used To Execute A
Set Of Statements On Some Conditions.
• It Provides A Unit Of Block In Which We Can
Either One Statement Or More Than One
Statements.
• If The Given Condition Is True Then The Set Of
Statements Are Executed Otherwise Body Is
Skipped.
if…else syntax
• if … else statement is used to express decisions.
Formally the syntax is :
if (expression)
statement1
else
statement2
• The expression inside if … else statement expects a
value. If the value is nonzero then it is true. If the
value is 0 then it is false.
if…else syntax
• Example:

void main()
{
int x ,y=0, a=2, b=3 ;
if(y>0)
x = a;
else
x = b;
printf(“%d“,x);
}
Thanks for your time and attention.

You might also like