Unit 2
Unit 2
Unit 2
LECTURE NOTES ON
PROGRAMMING FOR PROBLEM SOLVING USING C
UNIT II
Bitwise Operators: Exact Size Integer Types, Logical Bitwise Operators, Shift Operators
Selection & Making Decisions: Logical Data and Operators, Two Way Selection, Multiway
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
IMPORTANT QUESTIONS:
1.Define operator?explain operators in c language?
3. ~ – Bitwise NOT
4. ^ – XOR
Operators Example/Description
(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 >>
2. Bit Pattern of the data can be 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]
5.Binary means , Operator that require two arguments
<< (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.
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.
2. C if...else Statement
The if statement may have an optional else block. The syntax of
the if..else statement is:
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
It is possible to include an if..else statement inside the body of
another if...else statement.
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.
Example
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" );
}
}
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
Multi-Way Selection:-
A multi-way selection statement is used to execute at most ONE of the choices
of a set of statements presented.
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
Sample loop
The control conditions must be well defined and specified otherwise the
loop will execute an infinite number of times.
While Loop
While (condition )
{
Statements;
}
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.
do {
statements
} while (expression);
In the do-while loop, the body of a loop is always executed at least once.
After the body is executed, then it checks the condition. If the condition
is true, then it will again execute the body of a loop otherwise control is
transferred out of the loop.
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;
}
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.
2. End-of-file controlled loops - This type of while loop is usually used when
reading from a file. The loop terminates when the end of the file is
detected. This can easily be determined by checking the EOF() function
after each read from the file. This function will return true when the end-
of-file is reached.
Count-Controlled Repetition:-
break and continue are two C/C++ statements that allow us to further
control flow within and out of loops.
break causes execution to immediately jump out of the current loop, and
proceed with the code following the loop.
continue causes the remainder of the current iteration of the loop to be
skipped, and for execution to recommence with the next iteration.
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:-
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:
Count from 1 to 10.
Go through all the words in a dictionary to see whether they’re
palindromes.
For each customer that has an outstanding balance, send out an
email reminder that payment is due.
For each directory under this one, find music files and add then
to the list of known music.
#include <stdio.h>
int main()
{
int num1,num2;
float result;
char ch; //to store operator choice
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
The End