Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

COMPUTER PROGRAMMING

Engr. Anees Ahmed Soomro


Assistant Professor
CSE QUEST Nawabshah
INTRODUCTION TO COMPUTER PROGRAMMING

1) Operators, Arithmetic and Arithmetic assignment operators


2) Increment Operator, Decrement Operator
3) Relational Operator
Operators
• Operators are words or symbols that cause a program to do
something to variables.
• For example, the arithmetic operators (+) and (-) cause a
program to add or subtract two numbers.
Many different kind of operators
• + Addition
• - Subtraction
• * Multiplication
• / Division
• % Remainder
Fahrenheit to Celsius Temperature conversion

#include<stdio.h>
void main(void)
{
int ftemp,ctemp;
pritnf((“Type temperature in degree fahrenheit”);
scanf((“%d”,&ftemp);
ctemp=(ftemp-32)*5/9;
printf(“Temperature in degree Celsius is %d”,ctemp);

}
Celsius to Fahrenheit Temperature conversion

#include<stdio.h>
void main(void)
{
int ftemp,ctemp;
pritnf((“Type temperature in degree fahrenheit”);

printf(“Temperature in degree Celsius is %d”,ctemp);

}
Operator Precedence
• We have parenthesis around f(temp-32) known as precedence.
• The fact (*) abd (/) are evaluated before(+) and(-).
• We say that (*) and (/) has higher precedence than (+) and (-).

Remainder Operator
• Remainder operator(sometimes called modulor operator)
may be unfamiliar to you.
• It is used to find remainder when one number is divided by
another
• For example answer=13%5
value is 3
Expressions versus Variables

Days=years*365;

void main(void)
{
int num=2;
printf(“Number plus four is”,num+4);

}
Arithmetic Assignment Operator
Syntax:
Arithmetic op=
• += (Addition Assignment Operator)
• -= (Subtraction Assignment Operator)
• *= (Multiplication Assignment Operator)
• /= (Division Assignment Operator)
• %= (Remainder Assignment Operator)

• a+=1 is same as=> a=a+1


• a-=1 is same as=> a=a-1
• a*=1 is same as=> a=a*1
• a/=1 is same as=> a=a/1
• a%=1 is same as a=a%1
Increment Operator
+=,-=,++,--

+=(Addition Assignment Operator)


It is also known as increment operator
Syntax:
variable+=value;
variable=variable+value
-=(Subtraction Assignment Operator)
It is also known as increment operator
Syntax:
Variable-=value;
variable=variable-value
• Postfix increment and Postfix decrement Operator
• Prefix increment and Prefix decrement Operator

Op++
• It is postfix increment operator
++Op
• It is prefix increment operator
Op--
• It is postfix decrement operator
--Op
• It is prefix decrement operator
variable++ as postfix increment operator

#include<stdio.h>
void main(void)
{
int num=0;
printf(“Number=%d\n”,num);
printf(“Number=%d\n”,num++);
printf(“Number=%d\n”,num);

}
o/p is 0,0,1
++variable as prefix increment operator

#include<stdio.h>
void main(void)
{
int num=0;
printf(“Number=%d\n”,num);
printf(“Number=%d\n”,++num);
printf(“Number=%d\n”,num);

}
o/p is 0,1,1
variable-- as Postfix decrement operator

#include<stdio.h>
void main(void)
{
int num=0;
printf(“Number=%d\n”,num);
printf(“Number=%d\n”,num--);
printf(“Number=%d\n”,num);

}
o/p is 0,0,-1
--variable as Prefix decrement operator

#include<stdio.h>
void main(void)
{
int num=0;
printf(“Number=%d\n”,num);
printf(“Number=%d\n”,--num);
printf(“Number=%d\n”,num);

}
o/p is 0,-1,-1
Relational Operators

< Less than


> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
&& Logical AND or short circuit AND
|| Logical OR or short circuit OR

You might also like