0% found this document useful (0 votes)
9 views47 pages

Module 2

Module 2 covers operators in C, including arithmetic, relational, equality, logical, unary, conditional, bitwise, assignment, and sizeof operators, as well as type conversion and typecasting. It also discusses decision control statements like if, if-else, and switch statements, along with iterative statements such as while, do-while, and for loops. The document provides detailed explanations and examples for each concept to aid understanding.

Uploaded by

deepakadur123
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)
9 views47 pages

Module 2

Module 2 covers operators in C, including arithmetic, relational, equality, logical, unary, conditional, bitwise, assignment, and sizeof operators, as well as type conversion and typecasting. It also discusses decision control statements like if, if-else, and switch statements, along with iterative statements such as while, do-while, and for loops. The document provides detailed explanations and examples for each concept to aid understanding.

Uploaded by

deepakadur123
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/ 47

Module 2

contents

 Operators in C, Type conversion and typecasting.


Decision control and Looping statements:
 Introduction to decision control,
 Conditional branching statements,
 iterative statements,
 nested loops,
 break and continue statements,
 goto statement
Operators in C
 An operator is a symbol that specifies the mathematical ,logical, or relational operation to be
performed.
 C language supports different types of operators.
 It is used with variables and constants to form expressions
 These operators can be categorized into the following major groups
 Arithmetic operators
 Relational operators
 Equality operators
 Logical operators
 Unary operators
 Conditional operators
 Bitwise operators
 Assignment operators
 Comma operators
 Sizeof operators
Arithmetic operators
 Consider 3 variables declared as,
int a=9,b=3,result;
 We will use these variables to explain Arithmetic operators.
 It can be applied to any integer or floating point number.
 %can be applied only to integers.
 while performing modulo division, sign of result is always the sign of 1st
operand. E.g.: -12%3=-1
Relational operators(Comparison operators)

 its an operator that compare 2 values


 Expression that contain relational operators are called relational expressions.
 It return Boolean values
 Its used to determine the relationships between the operands
 These are evaluated from left to right
Equality operators

 C supports 2 kind of equality operators to compare their operands for equality or


inequality
1. equal to(==) 2. not equal to(!=) operators
 Equality operators have lower precedence than relational operators
Logical operators

 C supports 3 logical operators – logical AND, logical OR, logical NOT


 Evaluated from left to right
1.Logical AND(&&)
 It’s a binary operator , which evaluates 2 relational expressions or 2 values
 For example: (a<b) &&(b>c)
2.. Logical OR(| |)
 It returns a false value if both the operands are false, otherwise returns a true value
 example: (a<b)||(b>c)
3.Logical NOT
 It takes a single expression and reverses the value of expression
Logical operators

 Example: int a = 10, b;


b = !a;
Unary operators
 It acts on single operand . C supports 3 unary operators
1)Unary minus, 2)increment and 3)decrement operators

1)Unary minus:When operand is preceded by a minus sign, the unary operator negates its value.
 Eg: int a, b=10;
a= -(b);
The result of this expression is a=-10 bcz variable b has +ve value. After applying unary minus operator on
operand b, the value becomes -10(ie -ve value)

Increment(++) and decrement(--) operator:


 ++ increases value of its operand by 1, -- decreases value of its operand by 1
 Increment and decrement operators have 2 variants., prefix and postfix
Conditional operators

 Conditional or ternary(?:) is just like an if else statement that can be used within
expressions
 Its useful when there are 2 or more alternatives for an expression
 Syntax of conditional operator is
exp1 ? exp2 : exp3
Here exp1 is evaluated first,if it is true ,then exp2 is evaluated and becomes the result of the
expression, otherwise exp3 is evaluated and becomes the result of the expression
Example: large = (a>b) ? a : b
First a> b is evaluated,if a > b then large = a else large = b
Bitwise operators
 Operators that performs operations at bit level
 These operators include 1)bitwise AND 2)bitwise OR 3)bitwise XOR 4)Shift operators
 1)bitwise AND: like Boolean AND bitwise AND operator performs operation on bits instead of bytes,
chars,integers etc
 2)bitwise OR: when we use this operator ,the bit in first operand is ORed with corresponding bit in 2 nd
operand
 3)bitwise XOR:It perform operation on individual bits of operands.
 EXAMPLE: int a=10, b=20 , c=0;
 C= a&b; c=a|b; c= a^b;
Bitwise operators

 Shift operators:
 C supports 2 shift operators 1)shift-left 2)shift-right
 Its simple and are responsible for shifting bits either to the left or to the right
 Syntax: operand op num
 Example: x << 1 produces 0011 1010
Assignment operators
 Its responsible for assigning values to the variable
 Example: int x;
x = 10;
Comma operator: it takes 2 operands
 Example int a = 2. b=3, x=0;
x=(++a, b+=a);
x=6
Sizeof operators:
Example: int a=10;
unsigned int result;
result = sizeof(a);
size of a is 2 .since a is an integer it requires 2 bytes of storage in memory
Type conversion and typecasting.


Type conversion or typecasting of variables refers to changing a variables of one
data types into another

Type conversion is done implicitly whereas typecasting has to be done explicitly by
programmer

Type conversion:

Its done when the expressions has variables of different data types

To evaluate the expression, the data type is promoted from lower to higher level where
the hierarchy of data types (from higher to lower) can be given as: double, float, long,
int, short, and char.

Figure 9.14 shows the conversion hierarchy of data types.

Type conversion is automatically done when we assign an integer value to a floating
point variable.

Consider the code given below in which an integer data type is promoted to float. This
is known as promotion (when a lower level data type is promoted to a higher type).
Type conversion and typecasting.

consider the following group of statements:
float f= 3.5;
int i;
i = f:

The statement i = f results in f to be demoted to type int, i.e., the fractional part of f
will be lost and i will contain 3 (not 3.5). In this case demotion takes place, i.e., a
higher level data type is converted into a lower type.

Whenever demotion occurs, some information is lost. For example, in this case the
fractional part of the floating point number is lost

Example : float x;
int y = 3;
x = y;
Type conversion and typecasting.

TypeCasting
 Typecasting is also known as forced conversion.
 Typecasting an arithmetic expression tells the compiler to represent the value of the
expression in a certain way.
 It is done when the value of a higher data type has to be converted into the value of a
lower data type. But this casting is under the programmer's control and not under
compiler's control.
 For example, if we need to explicitly typecast a floating point variable into an integer
variable, then the code to perform typecasting can be given as:
 Example: float salary = 10000.00;
int sal;
sal = (int) salary;
Type conversion and typecasting.
 When floating point numbers are converted to integers (as in type
conversion), the digits after the decimal are truncated. Therefore,
data is lost when floating-point representations are converted to
integral representations.
 So in order to avoid such type of inaccuracies, int type variables
must be typecast to float type.
 As we see in the code above, typecasting can be done by placing
the destination data type in parentheses followed by the variable
name that has to be converted.
 Hence, we conclude that typecasting is done to make a variable
of one data type to act like a variable of another type.
Decision control and Looping statements:
 C supports 2 types of decision control statements
1. Conditional type branching
2. unconditional type branching
 In the case of decision control statements in C language (nested if
and if-else), a group of available statements will get executed in
case the conditions we have are true.
Conditional branching statements
 This statements help to jump from one part of the program to
another depending on whether a particular condition is satisfied
or not. These include
1. if statement
2. if-else statement
3.if-else-if statement
4. switch statement
1. if statement
 The if statement is the simplest form of decision control
statements that is frequently used in decision-making.
 The if block may include one statement or n statements enclosed
within curly brackets.
 First, the test expression is evaluated. If the test expression is
true, the statement of if block (statement 1 to n) are executed
otherwise these statements will be skipped and the execution will
jump to statement x.
 The statement in an if block is any valid C language statement
 the test expression is any valid C language expression that may
include logical operators.
 The general form of a simple if statement is shown in Figure
10.2.
2. if-else statement
 Its usage is very simple.
 The test expression is evaluated. If the result is true, the statements followed by the
expression is executed else if the expression is false, the statement in else part will be
executed.
 The general form of a simple if-else statement is shown in Figure 10.3.
 In the syntax shown, we have written statement block.
 A statement block may include one or more statements.
 According to the if-else construct, first the test expression is evaluated. If the
expression is true, statement block 1 is executed and statement block 2 is skipped.
 Otherwise, if the expression is false, statement block 2 is executed and statement block
1 is ignored.
3.if-else-if statement
 The if-else-if construct works in the same way as a normal if
statement and if else statement.
 if-else-if construct is also known as nested if construct.
 Its construct is given in Figure 10.4.
 It is not necessary that every if statement should have an else
block as c supports simple if statements.
 After the first test expression or the first if branch, the
programmer can have as many else-if branches as he wants
depending on the expressions that have to be tested.
Rules for using if, if-else, and if-else-if
statement
 Rule 1: The expression must be enclosed in parentheses.
 Rule 2: No semicolon is placed after the if/if-else/ if-else-if
statement. Semicolon is placed only at the end of statements in
the statement block.
 Rule 3: A statement block begins and ends with a curly brace. No
semicolon is placed after the opening/closing braces.
Dangling Else Problem
 With nesting of if-else statements, we often encounter a problem known as
dangling else problem.
 This problem is created when there is no matching else for every if statement.
 In such cases, C always pairs an else statement to the most recent unpaired if
statement in the current block.
 Consider the following code which shows such a scenario.
if(a > b)
if(a > c)
printf("In a is greater than b and c");
else
printf("In a is not greater than b and c");
 The problem is that both the outer if statement and the inner if statement might
conceivably own the else
Comparing Floating Point
 Numbers
We should never use floating point it numbers for testing quality.
 This is because floating point numbers are just approximations, so it is always
better to use floating point numbers for testing
 We can test for approximate equality by subtracting the two floating point
numbers (that are to be tested) and comparing their absolute value of the
difference against a very small number, epsilon.
 For example, consider the code given below which compares two floating
point numbers Note that epsilon is chosen by the programmer to be small
enough so that the two numbers can be considered equal.
4. switch statement
 A switch case statement is a multi-way decision statement that is
a simplified version of an if-else block that evaluates only one
variable.
 The general form of a switch statements shown in Figure 10.5.
 It is mostly used in 2 situations
 1.when there is only one variable to evaluate in the expression
 2.when many conditions are being tested for
 Default case is executed when no match is found between values
of switch and case statements and thus there are no statements to
executed.
Iterative statement
 Iterative statements are used to repeat the execution of a list of
statements, depending on the value of an integer expression.
 C language supports three types of iterative statements also
known as looping statements.
 They are:
• while loop
• do-while loop
• for loop
while loop
 The while loop provides a mechanism to repeat one or more
statements while a particular condition is true.
 Figure 10.6 shows the syntax and general form of representation
of a while loop.
 In the while loop, the condition is tested before any of the
statements in the statement block is executed.
 If the condition is true, only then the statements will be
executed otherwise if the condition is false, the control will
jump to statement y, which is the immediate statement
outside the while loop block.
 For example look at the following code which print first 10
numbers using while loop
while loop
Do-while loop
 The do-while loop is similar to the while loop.
 The only difference is that in a do-while loop, the test condition is evaluted at
the end of the loop.
 Now that the test condition is evaluted at the end, this clearly means that the
body of the loop gets executed at least one time (even if the condition is false).
 Figure 10.7 shows the syntax and general form of representation of a do-while
loop.
 Note that the test condition is enclosed in parentheses and followed by a
semicolon.
 The statements in the statement block are enclosed within curly brackets.
 The curly brackets are optional if there is only one statement in the body of
the do-while loop.
 Like the while loop, the do-while loop continues to execute whilst the
condition is true.
Do-while loop
 There is no choice whether to enter the loop or not because the loop will be
executed at least once irrespective of whether the condition is true or false.
 Hence, entry in the loop is automatic.
 There is only one choice: to continue or to exit. The do-while loop will
continue to execute while the condition is true and when the condition
becomes false, the control will jump to statement following the do-while
loop.
 Similar to the while loop, the do-while is an indefinite loop as the loop can
execute until the user wants to stop.
 The number of times the loop has to be executed can thus be determined at
the run time.
 However, unlike the while loop, the do-while loop is a bottom-checking
loop, since the control expression is placed after the body of the loop.
 The major disadvantage of using a do-while loop is that it always executes
at least once, even if the user enters
Do-while loop
For loop
 Like the while and do-while loops, the for loop provides a
mechanism to repeat a task until a particular condition is true.
 Its also known as a determinate or definite loop because the
programmer knows exactly how many times the loop will repeat.
 The syntax and general form of a for loop is given in Figure 10.8.
 When a for loop is used, the loop variable is initialized only once.
With every iteration of the loop, the value of the loop variable is
updated and the condition is checked.
 If the condition is true, then the statement block of the loop is
executed, else the statements comprising the statement block of the
for loop are skipped and the control jumps to the immediate
statement following the for loop body.
For loop
 The for loop is widely used to execute a single or a group of
statements a limited number of times.
 Multiple statements can be included in the third part of the for
statement by using the comma operator. For example, the for
statement given below is valid in C.
 for (1=0, j=10;i<j; i++, j- -)
For loop
 In this code, the loop initializes i to 0 and increments its value.
 Since a semicolon is placed after the loop, It means that loop
does not contain any statement.
 So even if the condition is true, no statement is executed.
 The loop continues till i becomes 10 and the moment i=10, the
statement following the for loop is executed and the value of i
(10) is printed on the screen.
 If there is no initialization to be done, then initialization stmnt
can be skipped by giving only semicolon
Nested loops
 C allows its users to have nested loops, i.e., loops that can be
placed inside other loops.
 Although this feature will work with any loop such as while, do-
while, and for, it is most commonly used with the for loop,
because this is easiest to control.
 A for loop can be used to control the number of times that a
particular set of statements will be executed. Another outer loop
could be used to control the number of times that a whole loop is
repeated.
Jump statements
1.Break statement
2. continue statement
3. go to statement
1.Break statement
 In C, the break statement is used to terminate the execution of the
nearest enclosing loop in which it appears.
 We have already seen its usage in the switch statement.
 The break statement is widely used with for loop, while loop, and
do-while loop.
 When the compiler encounters a break statement, the control passes
to the statement that follows the loop in which the break statement
appears.
1.Break statement
 Its syntax is quite simple, just type keyword break followed with
a in which it is semicolon.break;
 In switch statement if the break statement is missing then every
case from the matched case label till the end of the switch,
including the default, is executed.
 This example given below shows the manner in which break
statement is used to terminate the statement
 Note that the code is meant to print first 10 numbers using a
while loop, but it will actually print only numbers from 1 to 4.
 As soon as i becomes equal to 5, the break statement is executed
and the control jumps to the statement following the while loop.
 Hence, the break statement is used to exit a loop from any point
within its body, bypassing its normal termination expression.
 When the break statement is encountered inside a loop, the loop
is immediately terminated, and program control is passed to the
next statement following the loop.
 Figure 10.9 shows the transfer of control when the break
statement is encountered
Continue statement
 Like the break statement, the continue statement is used in the
body of a loop.
 When the compiler encounters a continue statement the rest of
the statements in the loop are skipped and the control is
unconditionally transferred to the loop continuation portion of the
nearest enclosing loop.
 Again like the break Statement, the continue statement cannot be
used without an enclosing for , while ,or do-while statement.
 When the continue statement is encountered in the while and do-
while loops, the control is transferred to the code that tests the
controlling expression.
 However, if placed within a for loop, the continue statement
causes a branch to the code that updates the loop variable. For
example, look at the following code.
goto statement
 The goto statement is used to transfer control to a specified label.
 However, the label must reside in the same function and can
appear only before one statement in the same function.
 The syntax of goto statement is shown in Figure 10.11.
 Here, label is an identifier that specifies the place where the
branch is to be made.
 Label can be any valid variable name that is followed by a colon
(:).
 The label is placed immediately before the statement where the
control has to be transferred.
 The label can be placed anywhere in the program either before or
after the goto statement.
 If the label is placed after the goto statement, then it is called a
forward jump and in case it is located before the goto statement,
it is said to be a backward jump.
 IF condition THEN goto label

You might also like