PPS USING C - UNIT - 2
PPS USING C - UNIT - 2
UNIT II
Bitwise Operators: Exact Size Integer Types, Logical Bitwise Operators, Shift Operators, Tips
Selection & Making Decisions: Logical Data and Operators, Two Way Selection,
Multiway Selection, More Standard Functions, Tips and Common Programming Errors,
Repetition: Concept of Loop, Pretest and Post-test Loops, Initialization and Updating, Event and
Counter Controlled Loops, Loops in C, Other Statements Related to Looping, Looping Applications,
Programming Example The Calculator Program, Tips and Common Programming Errors, Key
Bitwise Operators:-
These operators are used to perform bit operations. Decimal values are
converted into binary values which are the sequence of bits and bit
wise operators work on these bits.
Bit wise operators in C language are & (bitwise AND), | (bitwise OR),
~ (bitwise NOT), ^ (XOR), << (left shift) and >> (right shift).
TRUTH TABLE FOR BIT WISE OPERATION & BIT WISE OPERATORS
3. ~ – Bitwise NOT
4. ^–XOR
WWW.JNTUKNOTES.COM 1
Exact Size Integer Types:-
The following table provides the details of standard integer types with
their storage sizes and value ranges −
(x>=10)||(y>=10)
|| (logical OR) It returns true when at-least one of the condition is true
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
! (logical NOT) If “((x>5) && (y<5))” is true, logical NOT operator makes it false
Shift Operators:-
Bitwise Right Shift Operator in C :-
1.It is denoted by >>
WWW.JNTUKNOTES.COM 2
Bit Pattern of the data can be
2. shifted by specified number of
Positions to Right.
3. When Data is Shifted Right , leading zero’s are filled with
.
zero
4. Right shift Operator is Binary Operator [Bi – two]
Binary means ,
5. Operator that require two arguments
Bitwise Left Shift Operator in C :-
<< (left shift) Takes two numbers, left shifts the bits of the first
operand, the second operand decides the number of places to shift.
Or in other words left shifting an integer “x” with an integer “y”
(x<<y) is equivalent to multiplying x with 2^y (2 raise to power y).
Important Points:-
The left shift and right shift operators should not be used for negative
numbers. The result of is undefined behaviour if any of the operands
is a negative number. For example results of both -1 << 1 and 1 << -
1 is undefined.
If the number is shifted more than the size of integer, the behaviour
is undefined. For example, 1 << 33 is undefined if integers are stored
using 32 bits. See this for more details.
The left-shift by 1 and right-shift by 1 are equivalent to
multiplication and division by 2 respectively.
Selection & Making Decisions:-
Decision making structures require that the programmer specifies
one or more conditions to be evaluated or tested by the program,
along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other
statements to be executed if the condition is determined to be
false.
Show below is the general form of a typical decision making
structure found in most of the programming languages –
WWW.JNTUKNOTES.COM 3
C programming language assumes any non-zero and non-
null values as true, and if it is either zero or null, then it is
assumed as false value.
1. If Statement
“If statement” is the selection statement used to select course of action
depending on the conditions given. Therefore programmers can use this
statement to control the flow of their program.
The syntax of the if statement in C programming is:
if (test expression)
{
// statements to be executed if the test expression is true
}
2. C if...else Statement
The if statement may have an optional else block. The syntax
is:
of the if..else statement
if (test expression) {
// statements to be executed if the test expression is true
}
else {
// statements to be executed if the test expression is false
}
Nested if...else
WWW.JNTUKNOTES.COM 4
Logical Data and Operators:-
Following table shows all the logical operators supported by C
language. Assume variable A holds 1 and variable B holds 0, then
&& Called Logical AND operator. If both the operands are non-zero, (A && B)
then the condition becomes true. is false.
! Called Logical NOT Operator. It is used to reverse the logical !(A &&
state of its operand. If a condition is true, then Logical NOT B) is
operator will make it false. true.
Try the following example to understand all the logical operators available in C −
#include <stdio.h>
main() {
int a = 5;
int b = 20;
int c ;
if ( a && b ) {
printf("Line 1 - Condition is true\n" );
}
if ( a || b ) {
printf("Line 2 - Condition is true\n" );
}
if ( a && b ) {
printf("Line 3 - Condition is true\n" );
} else {
printf("Line 3 - Condition is not true\n" );
}
if ( !(a && b) ) {
printf("Line 4 - Condition is true\n" );
}
}
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true
WWW.JNTUKNOTES.COM 5
When you compile and execute the above program, it produces the following result −
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true
Two Way Selection:-
The two-way selection is the basic decision statement
for computers.
The decision is based on resolving a binary expression, and then
executing a set of commands depending on whether the
response was true or false.
C, like most contemporary programming languages,
implements two-way selection with the if…else statement.
Multi-Way Selection:-
A multi-way selection statement is used to execute at most ONE of the
choices of a set of statements presented.
Syntax of the multi-way select statement:
Switch (Expression)
{
Case Constant1, one or more statements; break;
Case constant2, one or more statements; break;
........
[ default : one or more statements;]
}
More Standard Functions:-
The standard functions are built-in functions. In C
programming language, the standard functions are
declared in header files and defined in .dll files.
In simple words, the standard functions can be defined as
"the ready made functions defined by the system to make
coding more easy".
The standard functions are also called as library
or .
functions pre-defined functions
in C when we use standard functions, we must include the
respective header file using #include statement.
WWW.JNTUKNOTES.COM 6
For example, the function printf() is defined in header
file stdio.h (Standard Input Output header file).
When we use printf() in our program, we must
include stdio.h header file using #include<stdio.h> statement
C Programming Language provides the following header
files with standard functions.
<ctype.h> character testing and conversion functions.
<math.h> Mathematical functions
<stdio.h> standard I/O library functions
<stdlib.h> Utility functions such as string conversion routines memory
allocation routines , random number generator,etc.
<string.h> string Manipulation functions
<time.h> Time Manipulation functions
MATH.H
abs : returns the absolute value of an integer x
cos : returns the cosine of x, where x is in radians
exp: returns "e" raised to a given power
fabs: returns the absolute value of a float x
log: returns the logarithm to base e
log10: returns the logarithm to base 10
pow : returns a given number raised to another number
sin : returns the sine of x, where x is in radians
sqrt : returns the square root of x
Repetition:-
Concept of Loop:-
In looping, a program executes the sequence of statements
many times until the stated condition becomes false. A loop
consists of two parts, a body of a loop and a control statement.
The control statement is a combination of some conditions
that direct the body of the loop to execute until the specified
condition
Types of Loops
WWW.JNTUKNOTES.COM 7
In an entry controlled loop, a condition is checked before executing
the body of a loop. It is also called as a pre-checking loop.
While Loop
While (condition )
{
Statements;
}
WWW.JNTUKNOTES.COM 8
#include<conio.h>
int main()
{
int num=1; //initializing the variable
while(num<=10) //while loop with condition
{
printf("%d\n",num);
num++; //incrementing operation
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
Do-While loop
A do-while loop is similar to the while loop except that the condition is always
executed after the body of a loop. It is also called an exit-controlled loop.
statements
} while (expression);
WWW.JNTUKNOTES.COM 9
The following program illustrates the working of a do-while loop:
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
do //do-while loop
{
printf("%d\n",2*num);
num++; //incrementing operation
}while(num<=10);
return 0;
}
Output:
2
4
6
8
10
12
14
16
18
20
For loop
#include<stdio.h>
int main()
{
int number;
for(number=1;number<=10;number++) //for loop to print 1-10 numbers
{
printf("%d\n",number); //to print the number
}
return 0;
WWW.JNTUKNOTES.COM 10
}
Output:
1
2
3
4
5
6
7
8
9
10
Initialization:-
Initialization is the process of locating and using the defined
values for variable data that is used by a computer program. For
example, an operating system or application program is installed
with default or user-specified values that determine certain
aspects of how the system or program is to function.
The process of the user specifying initialization values
is sometimes called configuration.
Event and Counter Controlled Loops:-
The Event-Controlled while loop
In this while loop an action is repeated until a certain event occurs. This
is by far the most frequently used form of the while loop.
There are three different types of Event-Controlled while loops
WWW.JNTUKNOTES.COM 11
1. Sentinel-controlled loops - A sentinel variable is initialized to a specific
value. The while loop continues until, through some action inside the
loop, the sentinel variable is set to a predefined termination value.
Count-Controlled Repetition:-
WWW.JNTUKNOTES.COM 12
In the case of for loops, the incrementation step will be executed
next, followed by the condition test to start the next loop iteration.
In the case of while and do-while loops, execution jumps to the
next loop condition test.
Infinite Loops:-
Infinite loops are loops that repeat forever without stopping.
Usually they are caused by some sort of error, such as the
following example in which the wrong variable is incremented:
int i, j;
for( i = 0; i < 5; j++ )
printf( "i = %d\n", i );
printf( "This line will never execute\n" );
Nested Loops
The code inside a loop can be any valid C code, including other loops.
Any kind of loop can be nested inside of any other kind of loop.
Looping Applications:-
Why do we use looping in programming?
Because we want to repeat something:
#include <stdio.h>
int main()
{
int num1,num2;
float result;
char ch; //to store operator choice
WWW.JNTUKNOTES.COM 13
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("Choose operation to perform (+,-,*,/,%):
"); scanf(" %c",&ch);
result=0;
switch(ch)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=(float)num1/(float)num2;
break;
case '%':
result=num1%num2;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
return 0;
}
Output
Enter first number: 10
Enter second number: 20
Choose operation to perform (+,-,*,/,%): +
Result: 10 + 20 = 30.000000
WWW.JNTUKNOTES.COM 14
Enter second number: 3
Choose operation to perform (+,-,*,/,%): >
Invalid operation.
Result: 10 > 3 = 0.000000
The End
WWW.JNTUKNOTES.COM 15