Pps Unit 2 Notes
Pps Unit 2 Notes
uiopasdfghjklzxcvbnmqwertyuiopasd
fghjklzxcvbnmqwertyuiopasdfghjklzx
cvbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyui
Programming for
opasdfghjklzxcvbnmqwertyuiopasdfg
Problem Solving
hjklzxcvbnmqwertyuiopasdfghjklzxc
NOTES
vbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyui
UNIT - 2
opasdfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmq
Rajesh Tripathi
wertyuiopasdfghjklzxcvbnmqwertyui
SIET
opasdfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmrtyuiopasdfghjklzxcvbn
mqwertyuiopasdfghjklzxcvbnmqwert
yuiopasdfghjklzxcvbnmqwertyuiopas
Sssss
1
Q. What is Operator?Explain types operatorused in C Language?
ANS: Simple answer can be given using expression 4 + 5 is equal to 9. Here 4 and 5 are called operands
and + is called operator. C language supports following type of operators.
Arithmetic Operators
Logical Operators
Relational Operators
Bitwise Operators
Assignment Operators
Misc Operators
Arithmetic Operators:
&& Called Logical AND operator. If both the operands are (A && B) is true.
non zero then then condition becomes true.
Relational operator
Operator Description Example
Checks if the value of two operands is equal or not,
== (A = = B) is not true.
if yes then condition becomes true.
2
Checks if the value of two operands is equal or not,
!= (A != B) is true.
if values are not equal then condition becomes true.
Checks if the value of left operand is greater than
> the value of right operand, if yes then condition (A > B) is not true.
becomes true.
Checks if the value of left operand is less than the
< value of right operand, if yes then condition (A < B) is true.
becomes true.
Checks if the value of left operand is greater than
>= or equal to the value of right operand, if yes then (A >= B) is not true.
condition becomes true.
Checks if the value of left operand is less than or
<= equal to the value of right operand, if yes then (A <= B) is true.
condition becomes true.
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ is as
follows –
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
& Binary AND Operator copies a bit to the result if it (A & B) = 12, i.e., 0000 1100
exists in both operands.
3
| Binary OR Operator copies a bit if it exists in either (A | B) = 61, i.e., 0011 1101
operand.
^ Binary XOR Operator copies the bit if it is set in one (A ^ B) = 49, i.e., 0011 0001
operand but not both.
~ Binary One's Complement Operator is unary and has (~A ) = ~(60), i.e,. -0111101
the effect of 'flipping' bits.
Assignment operators:
Assignment operators are used for assigning value to a variable. The left side operand of the
assignment operator is a variable and right side operand of the assignment operator is a value. The
value on the right side must be of the same data-type of the variable on the left side otherwise the
compiler will raise an error.
The following table lists the assignment operators supported by the C language –
= Simple assignment operator. Assigns values from right side C = A + B will assign the value of A
+= Add AND assignment operator. It adds the right operand to C += A is equivalent to C = C + A
-= Subtract AND assignment operator. It subtracts the right C -= A is equivalent to C = C - A
*= Multiply AND assignment operator. It multiplies the right C *= A is equivalent to C = C * A
/= Divide AND assignment operator. It divides the left operand C /= A is equivalent to C = C / A
%= Modulus AND assignment operator. It takes modulus using C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
& Returns the address of a variable. &a; returns the actual address of the variable.
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides how an expression is
evaluated. Certain operators have higher precedence than others; for example, the multiplication operator
has a higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than
+, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at
the bottom. Within an expression, higher precedence operators will be evaluated first.
5
Q. What is conditional control statement? Explain types of statement used in C language.
simple if statement : it tests the test expression and if the test expression is true , the statement block will
be executed otherwise the statement block will be skipped and the execution will be jumped to the next
statement.
The if statement has three basic forms:
Simple if ()
General Form(Syntax)
if (expression)
{
statements1;
}
Example:
if (a<b)
max = b;
else
max = a;
if else Statement :-Body of an ‘if’ statement contains another ‘if’ statement.
General Form:
if (expression)
statements1;
else
statements2;
6
Example
if (a>b)
printf(“A is greater than B”);
else
printf (“B is greater than A”);
3. Nested if: in nested if statements, another if statement is places either in if branch or in else branch or
in both the branches.
Syntax:
If(expression-1)
If (expression-2)
<statement-1>;
Else
<statement-2>;
Example:
Program to find the maximum of 3 numbers.
if (a>b)
if (a>c)
printf(“largest = %d”, a);
else
printf (“largest = %d”,c);
else
if (c>b)
printf (“largest = %d”,c);
else
printf (“largest = %d”,b);
7
Each condition is evaluated in order and if any condition is true the corresponding statement is executed
and the remainder of the chain is skipped. The final ‘else’ statement is executed only if none of the
previous conditions are satisfied. Final ‘else’ serves as a default case and is useful in detecting an
impossible or error condition.
Example 4.5
if (mark >= 75)
printf(“Honours\n”);
else if (mark >=60)
printf(“First Class\n”);
else if (mark >=50)
printf(“Second Class\n”);
else if (mark >=45)
printf(“Third Class\n”);
else
printf(“Fail\n”);
5) Switch statement in C :
Decision making are needed when, the program encounters the situation to choose a particular statement
among many statements. If a programmer has to choose one among many alternatives if...else can be used
but, this makes programming logic complex. This type of problem can be handled in C programming
using switch...case statement.
Syntax :
switch(variable)
{
case 1:
Valid C Statements;
break;
-
-
case n:
Valid C Statements;
break;
default:
Valid C Statements;
break;
}
8
Example
9
which is similar to the following if-else statement.
if (a>b)
max = a;
else
max = b;
10