0% found this document useful (0 votes)
7 views

POP Module2

Bpops103 or bpops203 module 2 notes vtu, sem exam

Uploaded by

darshangowda0525
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

POP Module2

Bpops103 or bpops203 module 2 notes vtu, sem exam

Uploaded by

darshangowda0525
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Principles of Programming using C BPOPS203

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

1. Arithmetic Operators: The different arithmetic operators are:

Operator Name Result Syntax Example (b=5, c=2)


+ Addition Sum a=b+c a=7
- Subtraction Difference a=b-c a=3
* Multiplication Product a=b*c a = 10
/ Division Quotient a=b/c a=2
% Modulus Remainder a=b%c a=1

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:

Operator Name Syntax Example (b=5, c=2)


< Lesser than a=b<c a = 0 (False)
> Greater than a=b>c a = 1 (True)
<= Lesser than or Equal to a = b <= c a = 0 (False)
>= Greater than or Equal to a = b >= c a = 1 (True)
== Equal to a = b == c a = 0 (False)
!= Not equal to a = b != c a = 1 (True)

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.

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

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 (>>)

✓ Bitwise NOT (~)

X ~X

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

0 1
1 0

✓ Bitwise AND (&), Bitwise OR (|),Bitwise XOR (^)

X Y X&Y X|Y X^Y


0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 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

6. Unary Operators: There are 4 types:

✓ Unary Plus Operator


✓ Unary Minus Operator
✓ Increment (++)
✓ Decrement (--)

✓ Unary plus and Unary minus Operator determines the signs

✓ Increment (++): It adds one to the


operand.

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++

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

✓ Decrement (--): It subtracts one from the operand.

Pre-decrement Post- decrement


First value of the operand is decremented First value of the operand is used for evaluation
(subtracted) by 1 then, it is used for then, it is decremented (subtracted) by 1.
evaluation.
Ex: - -a Ex: a- -

7. Conditional Operator/ Ternary Operator (?:)

✓ It takes three arguments.


Expression1 ? Expression2 : Expresseion3
Where,
Expression1 Condition
Expression2 Statement followed if condition is true
Expression3 Statement followed if condition is false
✓ Ex: large = (4 > 2) ? 4: 2 large = 4

8. Special Operator/ Special Symbols

✓ Comma Operator: It can be used as operator in expression and as separator in declaring


variables.
✓ Sizeof Operator: It is used to determine the size of variable or value in bytes.
✓ Address Operator: It is used to find the address of the operators.

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

Operator Description Associativity Precedence(Rank)


() Function call
Left to right 1
[] Array element reference
+ Unary plus
- Unary minus
++ Increment
-- Decrement
! Logical negation
Right to left 2
~ Ones complement
* Pointer to reference
& Address
Sizeof Size of an object
(type) Type cast (conversion)
* Multiplication
/ Division Left to right 3
% Modulus
+ Addition
Left to right 4
- Subtraction
<< Left shift
Left to right 5
>> Right Shift
< Less than
<= Less than or equal to
Left to right 6
> Greater than
>= Greater than or equal to
== Equality
Left to right 7
|= Inequality
& Bitwise AND Left to right 8
^ Bitwise XOR Left to right 9
| Bitwise OR Left to right 10
&& Logical AND Left to right 11
|| Logical OR Left to right 12
?: Conditional expression Right to left 13

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

=
*= /= %=
+= -= &= 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:

i. Automatic Type Conversion (Implicit)

✓ 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);

ii. Manual Type Conversion (Explicit)

✓ 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.

Example : float salary = 10000.00; int a = 500, b = 70;


int sal; float res;
sal = (int) salary;
res = (float) a*b;

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

✓ Decision Statements and Looping Statements :


Decision / Conditional Branching Statements:
C program is a set of statements which are normally executed sequentially in the order which they
appear. If there occours a situation where we have to change the order of execution of the statements we
make use of conditional statements.
C provides 5 types of conditional branching statements.

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

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

2. if – else Statement :

The if..else statement is an extension of if statement.


✓ If the Expression is true (or non-zero) then Statement1 will be executed; otherwise if it is false (or zero),
then Statement2 will be executed.
✓ In this case either true block or false block will be executed, but not both.
if (conditional_expression) Example :
{
Statement1; → true-block #include<stdio.h>
}
else Void main()
{
Statement2; →false-block {
}
int a =10, b=11;
Statement3;
if(a>b)
Flowchart:
{

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 :

✓ This construct is known as the else if ladder.


✓ The conditions are evaluated from the top (of the ladder), downwards. As soon as true condition is
found, the statement associated with it is executed and control transferred to the Next statement
skipping rest of the ladder.

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

✓ 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:

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203
Example :

#include<stdio.h>

Void main()

int a =20, b=5,c=3;

if((a>b) && (a>c))

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

✓ The choice is an integer expression or characters.


✓ The label1, label2, label3,…. are constants or constant expression evaluate to integer constants.
✓ Each of these case labels should be unique within the switch statement. block1, block2, block3, …
are statement lists and may contain zero or more statements.
✓ There is no need to put braces around these blocks. Note that case labels end with colon(:).
✓ Break statement at the end of each block signals end of a particular case and causes an exit from
switch statement.
✓ The default is an optional case when present, it will execute if the value of the choice does not match
with any of the case labels.

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

Example : Label = Number Example : Label = Character


#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<stdlib.h>
void main( ) void main( )
{ {
int ch,a,b,res; int a,b,res;
float div; char ch;
printf(“Enter two numbers:\n”); float div;
scanf(“%d%d”,&a,&b); printf(“Enter two numbers:\n”);
printf(“1.Addition\n 2.Subtraction\n scanf(“%d%d”,&a,&b);
3.Multiplication\n 4.Division\n 5.Remainder\n”); printf(“a.Addition\n b.Subtraction\n
printf(“Enter your choice:\n”); c.Multiplication\n d.Division\n e.Remainder\n”);
scanf(“%d”,&ch); printf(“Enter your choice:\n”);
switch(ch) scanf(“%c”,&ch);
{ switch(ch)
case 1: res=a+b; {
break; case ‘a’: res=a+b;
case 2: res=a-b; break;
break; case ‘b’: res=a-b;
case 3: res=a*b; break;
break; case ‘c’: res=a*b;
case 4: div=(float)a/b; break;
break; case ‘d’: div=(float)a/b;
case 5: res=a%b; break;
break; case ‘e’ : res=a%b;
default: printf(“Wrong choice!!\n”); break;
} default: printf(“Wrong choice!!\n”);
printf(“Result=%d\n”,res); }
} printf(“Result=%d\n”,res);
}

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

Looping statements

Definition of Loop: It is a programming structure used to repeatedly carry out a particular


instruction/statement until a condition is true. Each single repetition of the loop is known as an iteration of the
loop.

Three important components of any loop are:


1. Initialization (example: ctr=1, i=0 etc)
2. Test Condition (example: ctr<=500, i != 0 etc)
3. Updating loop control values (example: ctr=ctr+1, i =i-1)

✓ 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.

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

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.

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

Example: Write a program to find sum of 1 to 5 using do… while


#include<stdio.h>
void main()
{
int i=1, sum=0;
do
{
sum=sum+i;
i=i++;
} while (i<=5);
printf(“%d”, sum);
}

3. for loop:

✓ It is a pre test loop and also known as entry controlled loop.


✓ “for” keyword is used here.
✓ Here, the head of the for loop contains all the three components that is initialization, condition and
Updation.
syntax:

for( initialization; test-condition; updation)


{
Statements;
}
Statement x;

The execution of for statement is as follows.


i. First the control variable will be initialized. (Example i=1)
ii. Next the value of control variable is tested using test condition. (i<=n)
iii. If the condition is true the body of the loop is executed else terminated.
iv. If loop is executed then control variable will be updated (i++) then, the condition is checked
again.

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

Example: Write a Program to find the sum of 10 numbers


using for loop.#include<stdio.h>
void main( )
{
int i, sum=0;
for (i=1; i<=10; i++)
{
sum=sum+i;
}
printf(“%d”, sum);
}

Note: In for loops whether both i++ or ++i operations will be treated as pre-increment only.

Nested for loops:


✓ A for loop inside a for loop is called Nested for loop.

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

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.

while do… while


It is a pre test loop. It is a post test loop.
It is an entry controlled loop. It is an exit controlled loop.
The condition is at top. The condition is at bottom.
There is no semi colon at the end of while. The semi colon is compulsory at the end of while.
It is used when condition is important. It is used when process is important.
Here, the body of loop gets executed if and only if Here, the body of loop gets executed atleast once
condition is true. even if condition is false.
SYNTAX, FLOWCHART, EXAMPLE (Same as in SYNTAX, FLOWCHART, EXAMPLE (Same as in
explanation) explanation)

JUMPS IN LOOPS :

✓ Break and continue: break and continue are unconditional control construct.

1. Break :

✓ It terminates the execution of remaining iteration of loop.


✓ A break can appear in both switch and looping statements.

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

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);
}

Information Science and Engineering, JIT


Principles of Programming using C BPOPS203

Syntax:

✓ A label is a valid variable name.


✓ Label need not be declared and must be followed by colon.
✓ Label should be used along with a statement to which control is transferred.
✓ Label can be anywhere in the program either before or after the goto label.
✓ Here control jumps to label skipping statement1 and statement2 without verifying any condition that
is the reason we call it unconditional jumping statement.

Information Science and Engineering, JIT

You might also like