Unit-2 BCS-101 PPS Notes
Unit-2 BCS-101 PPS Notes
Unit-2
Arithmetic expressions Conditional
Branching
CO2: To translate the algorithms to programs & execution
Table of Contents
Syllabus Unit Wise............................................................................................................................................... 1
Operators .............................................................................................................................................................. 2
Relational Operators............................................................................................................................................. 4
Bitwise Operators................................................................................................................................................. 4
Assignment Operators .......................................................................................................................................... 6
Increment/Decrement OPERATOR ..................................................................................................................... 7
Conditional Operators (? :)................................................................................................................................... 7
Misc Operators ..................................................................................................................................................... 7
Operators Precedence in C ................................................................................................................................... 8
Operators Precedence Table ................................................................................................................................. 8
Control Statements ............................................................................................................................................. 11
a) If statement ............................................................................................................................................... 11
b) if –else statement ...................................................................................................................................... 13
c) Nested if-else statement........................................................................................................................... 14
e) A switch statement ................................................................................................................................... 15
f) The following rules apply to a switch statement .................................................................................... 15
Syllabus
Unit – 2 : (Arithmetic expressions & Conditional Branching) || CO2: To translate the
algorithms to programs & execution
Arithmetic expressions and precedence: Operators and expression using numeric and
relational operators, mixed operands, type conversion, logical operators, bit operations,
assignment operator, operator precedence and associativity.
Conditional Branching: Applying if and switch statements, nesting if and else, use of
break and default with switch.
Operator
Description Example
== Checks if the values of two operands are equal or not, if yes then (A == B) is
condition
becomes true.
!= Checks if the values of two operands are equal or not, if values are not (A != B) is true.
equal
then condition becomes true.
> Checks if the value of left operand is greater than the value of right (A>B) is true.not
operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of right (A < B) is true.
operand, if
yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to the value of (A >= B) is not
right operand, if yes then condition becomes true. (A >=
B) is not
<= Checks if the value of left operand is less than or equal to the value of (A <= B) is true.
right
operand, if yes then condition becomes true.
Bitwise Operators
Bitwise operator works on bits and performs bit-by-bit operation. These operators can
operate upon int and char but not on float and double..Bit wise operators in C language are;
& (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^ (XOR), << (left shift) and >> (right
shift).The truth tables for &, |, and ^ are as follows:
P Q p&q p|q p^q
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 exists (A & B) will give
& in both operands. 12, which is 0000
1100
Binary OR Operator copies a bit if it exists in either (A | B) will give
| operand. 61, which is
0011 1101
y XOR Operator copies the bit if it is set in one operand (A ^ B) will give 49,
^ but not both. which is 0011 0001
(~A ) will give -61,
Binary Ones Complement Operator is unary and has the which is 1100 0011
~ effect of ‘flipping’ bits. in 2’s
Complement form.
Binary Left Shift Operator. The left operands value
is moved left by the number of bits specified by the A << 2 will give 240
<< right operand. which is 1111 0000
Binary Right Shift Operator. The left operands
value is moved right by the number of bits A >> 2 will give 15
>> specified by the right operand. which is 0000 1111
#include <stdio.h>
int main ()
{int c=2,d=2;
printf(“%d \n”,c++); //this statement displays 2 then, only c
incremented by 1 to 3. Printf(“%d”,++c); //this statement
increments 1 to c then, only c is displayed. Return 0;
}
Output:2 4
Conditional Operators (? :)
Conditional operators are used in decision making in C programming, i.e, executes different
statements according to test condition whether it is either true or false.
Syntax of conditional operators;
Conditional expression? expression1:expression2
If the test condition is true (that is, if its value is non-zero), expression1 is returned and if
false expression2 is returned.
y = (x> 5? 3: 4) ;➔this statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y.
Misc Operators:
There are few other operators supported by c language.
Operator Description Example
(age < 12 && height < 48) || (age > 65 && height > 72)
=> (10 < 12 && 45 < 48) || (10 > 65 && 45 > 72)
=> (1 && 1) || (10 > 65 && 45 > 72)
=> 1 || (10 > 65 && 45 > 72)
=> 1 || (0 && 0)
=> 1 || 0
=> 1
a) If statement
Syntax:
if(boolean_expression)
{ /* statement(s) will execute if the Boolean expression is true */
}
If the Boolean expression evaluates to true then the block of code inside the if statement
will be executed. If boolean expression evaluates to false then the first set of code after
the end of the if statement (after the closing curly brace) will be executed. 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. Flow Diagram:
if(boolean_expression)
{ /* statement(s) will execute if the boolean expression is true */
}
else
{ /* statement(s) will execute if the boolean expression is false */
}
If the Boolean expression evaluates to true then the if block of code will be executed
otherwise else block of code will be executed 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.
Flow Diagram:
Example:
if( boolean_expression 1)
{ /* Executes when the Boolean expression 1 is
true */ if(boolean_expression 2)
{ /* Executes when the Boolean expression 2 is true */
}
}
Example:
#include <stdio.h> main ()
{ int a = 100;
int b = 200; if( a == 100 )
{ if( b == 200 )
{ printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a
is : %d\n", a ); printf("Exact
value of b is : %d\n", b );
return 0;
}
When the above code is compiled and executed, it produces following
result: Value of a is 100 and b is 200
Exact value of
a is : 100
Exact value of
b is : 200
e) A switch statement
Allows a variable to be tested for equality against a list of values. Each value is called a case,
and the variable being switched on is checked for each switch case.
Syntax:
switch(expression)
{ case constant-
expression :
statement(s);
break; /* optional */ case constant-expression :
statement(s);
break; /* optional */
Example:
main ()
{ char grade = 'B'; switch(grade)
{ case 'A' : printf("Excellent!\n" ); break;
case 'B' :
case 'C' :
printf("Well done\n" ); break;
case 'D' :
printf("You passed\n" ); break;
case 'F' :
printf("Better try again\n" ); break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
}
When the above code is compiled and executed, it produces
following result: Well done
Your grade is B