POP Module2
POP Module2
Module – 2 :
✓ Operators in C :
An operator is a symbol that tells the compiler to perform specific mathematical and logical functions.
The different operators supported in ‘C’ are:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operators
6. Unary Operators Increment and Decrement
7. Ternary/ Conditional Operator
8. Special Operators
2. Relational Operators: These are used to compare two quantities. The output will be either 0 (False)
or 1 (True). The different relational operators are:
3. Logical Operators: These are used to test more than one condition and make decision. The different
logical operators are: NOT, AND, OR
✓ Logical NOT (!) The output is true when input is false and vice versa. It accepts only one
input.
Input Output
X !X
0 1
1 0
✓ Logical AND (&&) The output is true only if both inputs are true. It accepts two or more
inputs.
Input Output
X Y X && Y
0 0 0
0 1 0
1 0 0
1 1 1
✓ Logical OR (||) The output is true only if any of its input is true. It accepts two or more inputs.
Input Output
X Y X || Y
0 0 0
0 1 1
1 0 1
1 1 1
4. Assignment Operators: These are used to assign the result or values to a variable. The different types
of assignment operators are:
Simple Assignment a = 10
Shorthand Assignment a += 10 a = a + 10
Multiple Assignment a = b = c = 10
5. Bitwise Operators: These works on bits and performs bit by bit operations. The different types of
bitwise operators are:
i. Bitwise NOT (~)
ii. Bitwise AND (&)
iii. Bitwise OR (|)
iv. Bitwise XOR (^) Output is True when odd number of 1’s are present.
v. Bitwise left shift (<<)
vi. Bitwise right shift (>>)
X ~X
0 1
1 0
✓ Bitwise Left Shift (<<) Shift specified number of bits to left side.
X 0 1 0 0 0 1 1 0
X<<2 0 0 0 1 1 0 0 0
✓ Bitwise Right Shift (>>) Shift specified number of bits to right side.
X 0 1 0 0 0 1 1 0
X>>2 0 0 0 1 0 0 0 1
Pre-increment Post-increment
First value of the operand is incremented First value of the operand is used for evaluation
(added) by 1 then, it is used for evaluation. then, it is incremented (added) by 1.
Ex: ++a Ex: a++
=
*= /= %=
+= -= &= Assignment operators Right to left 14
^= |=
<<= >>=
, Comma operator Left to right 15
Type Conversion:
✓ Converting the value of one data type to another type is called as Type Conversion.
✓ It occurs when mixed data occurs.
✓ There are two types:
✓ Here, the operand/ variables of smaller data type is automatically converted to data type of larger
size.
✓ char int long int float double long double
✓ Ex: int a = 5;
float b = 25, c; a b c
c = a / b; 5 25.0 0.25
printf(“%f” , c);
✓ It is a forced conversion used to convert operand/ variables of larger data type to smaller size or
vice versa.
✓ Ex: int a = 7, c; a b c
float b = 4.0; 7 4.0 3
b = a % (int) b; b → Converted into int & evaluated
printf(“%d” , c);
Type Casting:
Type casting is also known as forced type conversion. Type casting arithmetic expression tells the compiler to
represent the value of the expression in a certain way. It is done when the value of the higher data type has to be
converted in to the value of a lower data type.
1. if statement
2. if – else statement
3. if-else-if statement
4. Switch statement
1. if Statement :
This is a one way selection statement which helps the programmer to execute or skip certain block of
statements based on the particular condition.
✓ The Expression is evaluated first, if the value of Expression is true (or non zero) then Statement1 will be
executed; otherwise if it is false (or zero), then Statement1 will be skipped and the execution will jump
to the Statement2.
✓ Remember when condition is true, both the Statement1 and Statement2 are executed in sequence. This is
illustrated in Figure1.
Syntax:
Example :
if(conditional_expression)
#include<stdio.h>
{
Void main()
Statements 1 ;
{
}
int a =20, b=11;
Statements 2 ;
if(a>b)
printf(“A is greater\n”);
}
}
Output: A is greater
2. if – else Statement :
printf(“A is greater\n”);
False True }
Expression else
{
Statement2 Statement1 printf(“B is greater\n”);
}
Statement3
}
Output: B is greater
3. if – else – if Statement :
✓ When all conditions are false then the final else containing the default Statement4 will be executed.
if (Expression1)
{
Statement1;
}
else if(Expression2)
{
Statement2;
}
else if(Expression3)
{
Statement3;
}
else
{
Statement4;
}
Next Statement;
FlowChart:
#include<stdio.h>
Void main()
printf(“A is greater\n”);
}
else if ((b>a) && (b>c))
{
printf(“B is greater\n”);
}
else
{
printf(“C is greater\n”);
}
}
Output : A is greater
4. Switch statement:
✓ C language provides a multi-way decision statement so that complex else-if statements can be easily
replaced by it. C language’s multi-way decision statement is called switch.
General syntax of switch statement is as follows:
switch(choice)
{
case label1: block1;
break;
case label2: block2;
break;
case label3: block-3;
break;
default: default-block;
break;
}
✓ Here switch, case, break and default are built-in C language words.
✓ If the choice matches to label1 then block1 will be executed else if it evaluates to label2 then block2
will be executed and so on.
✓ If choice does not matches with any case labels, then default block will be executed.
Information Science and Engineering, JIT
Principles of Programming using C BPOPS203
Looping statements
✓ Loops can be classified into two types based on the placement of test-condition.
✓ If the test-condition is given in the beginning such loops are called pre-test loops (also known as entry-
controlled loops).
✓ Otherwise if test condition is given at the end of the loop such loops are termed as post-test loops (or exit
controlled loops).
Loops in C:
1. while loop
2. do while loop
3. for loop
1. while loop:
✓ It is a pre-test loop (also known as entry controlled loop). This loop has following
✓ syntax:
while (condition)
{
statement-block;
}
statement x;
✓ In the syntax given above ‘while’ is a key word and condition is at beginning of the loop.
✓ If the test condition is true the body of while loop will be executed.
✓ After execution of the body, the test condition is once again evaluated and if it is true, the body is
executed once again.
✓ This process is repeated until condition finally becomes false and control comes out of the body of the
loop.
FlowChart:
✓ Here is an example program using while loop for finding sum of 1 to 10.
Example: Write a Program to find sum of 1 to 5
using while.#include<stdio.h>
void main()
{
int i=1, sum=0;
while (i<=5)
{
sum=sum+i;
i=i++;
}
printf(“%d”, sum);
}
2. do while loop:
It is a post-test loop (also called exit controlled loop) it has two keywords do and while. The General
syntax:
do
{
statement-block;
} while (condition);
✓ In this loop the body of the loop is executed first and then test condition is evaluated.
✓ If the condition is true, then the body of loop will be executed once again. This process continues as
long as condition is true.
✓ When condition becomes false, the loop will be terminated and control comes out of the loop.
3. for loop:
Note: In for loops whether both i++ or ++i operations will be treated as pre-increment only.
Example:
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf(“%d”, &a[i][j])
}
}
Note: If updation is not present in loops then, it will execute infinite times.
If initialization is not given then, program prints nothing.
JUMPS IN LOOPS :
✓ Break and continue: break and continue are unconditional control construct.
1. Break :
Syntax Flowchart
#include<stdio.h>
while(condition) void main( )
{ {
Statements; int i;
if(condition) for(i=1; i<=5; i++)
break; {
Statements; if(i==3)
} break;
printf(“%d”, i)
}
}
OUTPUT 1 2
2. Continue :
✓ It terminates only the current iteration of the loop.
✓ Continue can appear in looping statements.
Syntax Flowchart
#include<stdio.h>
while(condition) void main( )
{ {
Statements; int i;
if(condition) for(i=1; i<=5; i++)
continue; {
Statements; if(i==3)
} continue;
printf(“%d”, i);
}
}
OUTPUT 1 2 4 5
GOTO STATEMENT :
✓ goto is an unconditional branching statement. The syntax of goto is as follows:
Syntax Example
goto label; void main( )
{
statement1; int a=5, b=7;
goto end;
statement2; a=a+1;
b=b+1;
label: end: printf(“a=%d b=%d”, a,b);
}
Syntax: