POP Module 2
POP Module 2
EXPRESSION
Expressions:An expression is a sequence of Operands and Operators that
reduces to single value.An expression can be simple or complex.
An operator is a syntactical token that requires an action to be taken
An operand is an object on which an operation is performed. It receives an
operation action.
Simple expression contains only one operator. Ex 1+6
A complex expression contains more than one operator. Ex 3*5+7
DIFFERENT TYPES OF EXPRESSIONS
BASED ON OPERATORS
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. AssignmentOperators
5. Increment / Decrement
operators
6. BitwiseOperators
7. Conditional operator
8. Special operator
1. Arithmetic
Operators
2. Relational
Operators
3. Logical Operators
4. Assignment
Operators
1. Simple assignment. Ex
a=5;
2. Shorthand assignment. Ex a+=5;
#include<stdio.h>
void main()
{
int a=1,b=2;int k; k = (a,b);
printf("%d",k);
}
Output: 2
OPERATOR PRECEDENCE AND
ASSOCIATIVITY
Precedence - The order in which the operators in a complex expression are evaluated is
determined by set of priorities known as precedence.
Associativity - If two operators with same precedence occur in a complex expression, another
attribute of an operator called associativity takes control. Associativity is the parsing
direction used to evaluate an expression. It can be either left to right or right to left .
Precedence: The order in which the operators in a complex expression are evaluated is determined by set of
priorities known as precedence. If an expression contains Arithmetic operators ‘+’ , ‘*’ and ‘/ ‘operators as
shown below:
A=B+C*D/F
In this expression RHS of expression contains B+C*D/F, here first preference is given to ‘*’ and ‘/ ‘and
then to ‘+’. This is decided by operator precedence given in C Language.
Associativity: If two operators with same precedence occur in a complex expression,
another attribute of an operator called associativity takes control. Associativity is the
parsing direction used to evaluate an expression. It can be either left to right or right to left .
For example: X= Y/Z*P%Q
Here ‘/’, ‘*’, and ‘%’ are operators at same level. But we evaluate this expression from
LEFT to RIGHT (i.e. Associativity is from Left to Right).
Evaluation of Expressions
Example-1 Example-2
Example-3 Example-4
?
TYPE CONVERSION AND TYPE CASTING
● Type conversion and type casting of variables refers to changing a variable of one data type
into another.
●
While type conversion is done implicitly, casting has to be done explicitly by the
programmer.
We will discuss both of them here.
●
Type conversion is done when the expression has variables of different data types. So
to evaluate the expression, the data type is promoted from lower to higher level where
the hierarchy of data types can be given as: double, float, long, int, short and char.
● For example, type conversion is automatically done when we assign an integer value to
a
floating point variable. For ex,
float x;
int y = 3;
x = y;
●
Type casting is also known as forced conversion. It is done when
the value of a higher data type has to be converted in to the value
of a lower data type. For example, we need to explicitly type cast
an integer variable into a floating point variable.
float salary = 10000.00;
int sal;
sal = (int) salary;
●
Typecasting can be done by placing the destination data type in
parentheses followed by the variable name that has to be
converted.
Bangalore -560024
INFORMATION SCIENCE & ENGINEERING
Chapter-10
Ranjitha J
Assistant Professor
Dept. of ISE
Bangalore.
INTRODUCTION TO DECISION CONTROL
STATEMENTS
DECISION MAKINGG (TWO-WAY
SELECTION STATEMENTS)
The basic decision statement in the computer is the two way selection.
The decision is described to the computer as conditional statement that can be answeredTRUE
or FALSE.
If the answer isTRUE, one or more action statements are executed.
If answer is FALSE, the different action or set of actions are executed.
Regardless of which set of actions is executed, the program continues with next statement.
C language provides following two-way selection statements:
1. if statement
2. if – else statement
3. Nested if else statement
4. Cascaded if else (also called else-if ladder)
1. if statement:The general form of simple if statements is shown below.
The Expression is evaluated first, if the value of Expression is true (or non zero)
then Statement1 will be executed; otherwise if it is false (or zero), then
Statement1 will be skipped and the execution will jump to the Statement2.
Remember when condition is true, both the Statement1 and Statement2 are
executed in sequence.This is illustrated in Figure1. Note: Statement1 can be single
statement or group of statements.
2. if..else statement:The if..else statement is an extension of simple if statement.General
syntax:
Here switch, case, break and default are built-in C language words.
If the choice matches to label1 then block1 will be executed else if it evaluates to label2
then block2 will be executed and so on.
If choice does not matches with any case labels, then default block will be executed.
.
Iterative
Loops
The while Statement
The do Statement
The for Statement
Nested for Loop
THE while STATEMENT
while loop: It is a pre-test loop (also known as entry controlled loop).This loop has
following syntax:
do…. while loop: It is a post-test loop (also called exit controlled loop) it has
two
keywords do and while.The General syntax:
In this loop the body of the loop is executed first and then test condition is
evaluated.
If the condition is true, then the body of loop will be executed once again.This
process continues as long as condition is true.
When condition becomes false, the loop will be terminated and control
comes out
of the loop.
THE for STATEMENT
In some programming situations within one for loop we have to write another
for loop. Such embedded for loops are called nested for loops.
Following diagram depicts a nested for loop flowchart.
INITIALIZING, TEST CONDITION AND
UPDATING: THREE COMPONENTS OF A LOOP
Normally all loops should have three components:
1. Initialization (example: i=0; j=1 etc)
2. Test condition (example: ctr<=10, i<=20 etc)
3. Updating (example: ctr=ctr+1, i=i+2 etc)
If any of these three components are missing program behaves indifferently. For
instance if updating statement is missing loop goes into infinite mode as shown
in following example:
JUMPS IN LOOPS