0% found this document useful (0 votes)
24 views21 pages

Unit-2 BCS-101 PPS Notes

Unit-2 of the Programming for Problem Solving course covers arithmetic expressions and conditional branching in C programming. It includes details on various operators (arithmetic, relational, bitwise, assignment, increment/decrement, and conditional operators), operator precedence, and control statements like if and switch. The unit aims to equip learners with the skills to translate algorithms into executable programs.

Uploaded by

dwivedialok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views21 pages

Unit-2 BCS-101 PPS Notes

Unit-2 of the Programming for Problem Solving course covers arithmetic expressions and conditional branching in C programming. It includes details on various operators (arithmetic, relational, bitwise, assignment, increment/decrement, and conditional operators), operator precedence, and control statements like if and switch. The unit aims to equip learners with the skills to translate algorithms into executable programs.

Uploaded by

dwivedialok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Programming for Problem solving (BCS-101)

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.

BCS101/201 Programming for Problem Solving UNIT-2 Page 1


Unit-2
Operators-
An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulations. Type’s are-

Arithmetic operator: Assume variable A holds 10 and variable B holds 20 then:

Operator
Description Example

+ Adds two operands A + B will give 30


- Subtracts second operand from the first A – B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after an B % A will give 0
integer division
++ Increments operator increases integer value by A++ will give 11
one
-- Decrements operator decreases integer value by A–will give 9
one

BCS101/201 Programming for Problem Solving UNIT-2 Page 2


Relational Operators:
Assume variable A holds 10 and variable B holds 20, then:
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

BCS101/201 Programming for Problem Solving UNIT-2 Page 3


Assume variable A holds 60 (00111100) and variable B holds 13 (00001101), then:

Operator Description Example

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

BCS101/201 Programming for Problem Solving UNIT-2 Page 4


Assignment Operators:
In C programs, values for the variables are assigned using assignment operators.There are
following assignment operators supported by C language:
Op Description Example
era
tor
C = A + B will
Assignment operator, Assigns values from right side assign value of A + B into C
= operands to left side operand
C += A is equivalent to
Add AND assignment operator, It adds right C = C+ A
+ operand to the left operand and assign the
= result to left operand
Subtract AND assignment operator, It C -= A is equivalent to
subtracts right C = C– A
- operand from the left operand and assign
= the result to left Operand
Multiply AND assignment operator, It C *= A is equivalent
multiplies right to C = C
* eft operand and assign the result to left Operand *A
=
C /= A is equivalent
Divide AND assignment operator, It to C = C
/ divides left operand with the right operand /A
= and assign the result to left operand
C %= A is equivalent
Modulus AND assignment operator, It takes to C = C
% modulus using two operands and assign the %A
= result to left operand
< C <<= 2 is same as C = C
<
= Left shift AND assignment operator << 2
> C >>= 2 is same as C =
> Right shift AND assignment operator C >> 2
=
& C &= 2 is same as C = C
= Bitwise AND assignment operator &2
^ C ^= 2 is same as C
= bitwise exclusive OR and assignment =C^2
operator
| C |= 2 is same as C
= bitwise inclusive OR and assignment =C|2
operator

BCS101/201 Programming for Problem Solving UNIT-2 Page 5


Increment/Decrement OPERATOR
In C, ++ and – are called increment and decrement operators respectively. Both of these
operators are unary operators, i.e, used on single operand. ++ adds 1 to operand and –
subtracts 1 to operand respectively. For example:
Let a=5 and b=10
a++; //a becomes 6
a--; //a becomes 5
++a; //a becomes 6
--a; //a becomes 5
When i++ is used as prefix(like: ++var), ++var will increment the value of var and then
return it but, if ++ is used as postfix(like: var++), operator will return the value of operand
first and then only increment it. This can be demonstrated by an example:

#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

It is a unary operator which is used in (a), where a is integer, will


Sizeof() finding the size of data type, constant, return 4.
arrays, structure etc.
& ; will give actual address of
Returns the address of a variable. the variable.
* Pointer to a variable. *a; will pointer to a
variable.

BCS101/201 Programming for Problem Solving UNIT-2 Page 6


Operators Precedence in C
Operator precedence determines the grouping of terms in an expression. This affects
how an expression is evaluated. Certain operators have higher precedence than others; for
example, the multiplication operator has higher precedence than the addition operator.
For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.For Example-

Operators Precedence Table


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.

BCS101/201 Programming for Problem Solving UNIT-2 Page 7


BCS101/201 Programming for Problem Solving UNIT-2 Page 8
Example 1: Let age = 10, height = 45 solve the
following expression for K- K= (age < 12 && height < 48) ||
(age > 65 && height > 72)

Solution: In this case, the order of evaluation of operators will be parentheses ( () ),


relational operators (<, >), AND ( && ) operator, OR ( || ) operator.

(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

Example 2: Let year = 2000 solve the following expression for K-


K= (year % 4 == 0 && year % 100! = 0 ) || (year % 400 == 0)

Solution: In this case, the order of evaluation of operators will be parentheses ( () ),


Modular division (%), Relational operators (==, !=) AND ( && ) operator, OR ( || )
operator.

(year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0)


1
=> (2000 % 4 == 0 && 2000 % 100 != 0 ) || (2000 % 400 ==
2
0)
3
=> (0 == 0 && 2000 % 100 != 0 ) || (2000 % 400 == 0)
4
=> (0 == 0 && 0 != 0 ) || (2000 % 400 == 0)
5
=> (1 && 0 != 0 ) || (2000 % 400 == 0)
6
=> (1 && 0 ) || (2000 % 400 == 0)
7
=> 0 || (2000 % 400 == 0)
8
=> 0 || (0 == 0)
9
=> 0 || 1
1
=> 1
0

BCS101/201 Programming for Problem Solving UNIT-2 Page 9


Control Statements-
Control statements enable us to specify the order in which the various instructions in the
program are to be executed. They define how the control is transferred to other parts of the
program. Control statements are classified in the following ways:

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:

BCS101/201 Programming for Problem Solving UNIT-2 Page 10


Example:
#include
<stdio.h
> int
main ()
{ int a = 10; if( a < 20 )
{ Printf("a is less than 20\n" );
}
Printf("value of a is : %d\n", a);
}
When the above code is compiled and executed, it produces
following result: a is less than 20;
value of a is : 10

BCS101/201 Programming for Problem Solving UNIT-2 Page 11


b) if –else statement
Syntax: The syntax of an if...else statement in C programming language is:

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:

#include <stdio.h> main ()


{ int a = 100; if( a < 20 )
{ Printf("a is less than 20\n" );
}
else
{ Printf("a is not less than 20\n" );
} Printf("value of a is : %d\n", a);}
When the above code is compiled and executed, it produces
following result: a is not less than 20;
value of a is : 100

BCS101/201 Programming for Problem Solving UNIT-2 Page 12


c) Nested if-else statement
The syntax for a nested if statement is as follows:

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

BCS101/201 Programming for Problem Solving UNIT-2 Page 13


d)

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 */

/* you can have any number of case


statements */ default : /* Optional */
statement(s);
}

f) The following rules apply to a switch statement:


• You can have any number of case statements within a switch. Each case is followed by the
value to be compared to and a colon.
• The constant-expression for a case must be the same data type as the variable in the switch,
and it must be a constant or a literal.
• When the variable being switched on is equal to a case, the statements following that case
will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow of control jumps to
the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of control will fall
through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which must appear at the end of the
switch. The default case can be used for performing a task when none of the cases is true. No
break is needed in the default case.

BCS101/201 Programming for Problem Solving UNIT-2 Page


14
Flow Diagram:

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

BCS101/201 Programming for Problem Solving UNIT-2 Page


15

You might also like