0% found this document useful (0 votes)
87 views66 pages

POP Module 2

The document discusses different types of expressions and operators in C language. It defines expressions as sequences of operands and operators that evaluate to a single value. Operators perform operations on operands. Expressions can be simple, with one operator, or complex, with multiple operators. The document describes different types of operators like arithmetic, relational, logical, assignment, increment/decrement, bitwise, conditional and special operators. It also discusses operator precedence and associativity which determine the order of evaluation in complex expressions. Finally, it covers various decision making and iterative statements in C like if-else, switch, while, do-while and for loops.

Uploaded by

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

POP Module 2

The document discusses different types of expressions and operators in C language. It defines expressions as sequences of operands and operators that evaluate to a single value. Operators perform operations on operands. Expressions can be simple, with one operator, or complex, with multiple operators. The document describes different types of operators like arithmetic, relational, logical, assignment, increment/decrement, bitwise, conditional and special operators. It also discusses operator precedence and associativity which determine the order of evaluation in complex expressions. Finally, it covers various decision making and iterative statements in C like if-else, switch, while, do-while and for loops.

Uploaded by

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

OPERATORS &

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;

3. Multiple assignments Ex a=b=c=100;


5. Increment / Decrement
operators
6. Bitwise
Operators
7. Conditional Operators [ ?: ] Or Ternary Operator
8. Special operator: a) Comma Operator b) sizeof operator
Comma
operator:
1. Comma operator has lowest precedence. [Priority] i.e., evaluated at
last.
2. Comma operator returns the value of the rightmost operand.
3. Comma operator can acts as

#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:

 If the Expression is true (or non-zero)thenStatement1 will be executed; otherwise if it is false


(or zero), then Statement2 will be executed.
 In this case either true block or false block will be executed, but not both.
 This is illustrated in Figure 2. In both the cases, the control is transferred subsequently to the
Statement3.
3. Nested if .. else statement: When a series of decisions are involved, we have
use
to more than one if..else statement in nested form as shown below in the general
syntax.

• If Expression1 is true, check for Expression2, if it is also true then Statement1


is executed.
• If Expression1 is true, check for Expression2, if it is false then Statement2 is
executed.
• If Expression1 is false, then Statement3 is executed.
• Once we start nesting if .. else statements, we may encounter a classic
problem known as dangling else.
• This problem is created when no matching else for every if.
• C solution to this problem is a simple rule “always pair an else to most
recent unpaired if in the current block”.
• Solution to the dangling else problem, a compound statement.
• In compound statement, we simply enclose true actions in braces to make
the second if a compound statement.
4. else-if ladder or cascaded if else: There is another way of putting ifs together
when multipath decisions are involved. A multi path decision is a chain of ifs in
which the statement associated with each else is an if. It takes the following
form.

This construct is known as the else if ladder.


The conditions are evaluated from the top (of the ladder), downwards.As soon
as true condition is found, the statement associated with it is executed and
control transferred to the Next statement skipping the rest of the ladder.
When all conditions are false then the final else containing the default
Statement4
will be executed.
THE SWITCH STATEMENT

 C language provides a multi-way decision statement so that complex else-if statements


can be easily replaced by it. C language’s multi-way decision statement is called switch.
General syntax of switch statement is as follows:

 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:

 In the syntax given above ‘while’ is a keyword and condition is at beginning of


the loop.
 If the test condition is true the body of while loop will be executed.
 After execution of the body, the test condition is once again evaluated and if it is
true, the body is executed once again.
 This process is repeated until condition finally becomes false and control comes
out of the body of the loop.
THE do STATEMENT

 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

 It is another pre-test loop (also called entry controlled loop)that provides


concise loop control structure. It has one keyword for.
 One important aspect of for loop is all the three components of a loop (viz.
initializing, testing condition and updating (increment/decrement)) is given in the
head of for loop
 General syntax:
NESTED for LOOP

 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

 Break and continue: break and continue are unconditional control


construct.
i. break This statement is useful to terminate a loop and transfer control
of loop under
out special situations.
 break statement works with while, do….while, for and switch
statements.
ii. Continue Statement is helpful to skip particular set of statements in
body and to continue execution from the beginning of loop.
loop
Following syntax clearly depicts how control shifts to beginning of a loop on
finding
continue statement
Following syntax clearly depicts how control shifts to
beginning of a loop on finding continue statement.
Following program syntax diagrammatically represents the
working mechanism of break statement.
THE goto STATEMENT

 goto is an unconditional branching statement.The syntax of goto is as follows:


THANK
YOU
THANK YOU

You might also like