Professional Documents
Culture Documents
UNIT II-part 1 Notes
UNIT II-part 1 Notes
UNIT II-part 1 Notes
Control Structures: Selection Statements (making decisions) – if, if-else, nested if, else if
ladder and switch statements.
---------------------------------------------------------------------------------------------------------------------
OPERATORS
An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions.
The operators are classified into three types based on the number of operands it operates.
C language is rich in built-in operators .C language provides eight different types of operators.
They are
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Bitwise Operators
conditional operators
special Operators
Arithmetic Operators
The various arithmetic operators supported by C language are shown in the following table.
−
Binary Subtracts second operand from the first. A − B = -10
Arithmetic operators except Modulus Operator work on integers and float. Modulus operator
works only on integers.
Ex -7%3= -1
7%-3=1
- 7%-3=-1
Relational Operators
Relational operators are used to compare two values. C language supports six types of relational
operator and all are binary types. The following table shows all the relational operators supported
by C.
== Checks if the values of two operands are equal or not. If yes, then (A = = B) is not
the condition becomes true. true. so 0 is
returned
!= Checks if the values of two operands are equal or not. If the values (A! = B) is true.
are not equal, then the condition becomes true.
So 1 is returned.
> Checks if the value of left operand is greater than the value of right (A > B) is not
operand. If yes, then the condition becomes true. true. So 1 is
returned
< Checks if the value of left operand is less than the value of right (A < B) is true.
operand. If yes, then the condition becomes true. So 1 is returned
>= Checks if the value of left operand is greater than or equal to the (A >= B) is not
value of right operand. If yes, then the condition becomes true. true. so 0 is
returned
<= Checks if the value of left operand is less than or equal to the value (A <= B) is true.
of right operand. If yes, then the condition becomes true. So 1 is returned
Logical Operators
Logical operators are used to join two conditions. The logical operators either return true (1) or
false (0).C language supports three types of logical operators.
&& Called Logical AND operator. If both the operands are non-zero, then Binary
the condition becomes true.
! Called Logical NOT Operator. It is used to reverse the logical state of Unary
its operand. If a condition is true, then Logical NOT operator will
make it false.
These operators are unary operators and they work only on integer data types.
Increment operators are of two types pre increment and post increment.
If the operator occurs before operand then the operator is Pre increment operators. Ex ++a
If the operator occurs after operand then the operator is Post increment operators. Ex a++
Decrement operators are of two types pre decrement and post decrement.
If the operator occurs before operand then the operator is Pre decrement operators. Ex --a
If the operator occurs after operand then the operator is Post decrement operators. Ex a--
Operator Type Description
The difference between pre increment and post increment operator lies in the usage of
expressions.
Ex:
int a=5;
y=a++;
Assignment Operators
The assignment operator is used to assign RHS value to LHS variable. The syntax of assignment
operator is
var_name=expression;
+= Add AND assignment operator. It adds the right operand to the left C += A is equivalent
operand and assign the result to the left operand. to C = C + A
-= Subtract AND assignment operator. It subtracts the right operand from C -= A is equivalent
the left operand and assigns the result to the left operand. to C = C - A
*= Multiply AND assignment operator. It multiplies the right operand with C *= A is equivalent
the left operand and assigns the result to the left operand. to C = C * A
/= Divide AND assignment operator. It divides the left operand with the C /= A is equivalent
right operand and assigns the result to the left operand. to C = C / A
cond?expr1:expr2;
Bitwise Operators
Bitwise operator works on bits and performs bit-by-bit operation. These operators work
on integer data types.
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B)
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B)
~ Binary Ones Complement Operator is unary and has the effect of 'flipping' (~A )
bits.
<< Binary Left Shift Operator. The left operands value is moved left by the A << 2
number of bits specified by the right operand.
>> Binary Right Shift Operator. The left operands value is moved right by the A >> 2
number of bits specified by the right operand.
The truth tables for &, |, and ^ is as follows −
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
A = 0011 1100
B = 0000 1101
-----------------
~A = 1100 0011=-61
Special Operators
Ex: z=(x=2,y=3,x+y);
z=5;
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
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.
Type Conversion in C
When variables and constants of different types are combined in an expression then they are
converted to same data type.
The process of converting one predefined type into another is called type conversion.
When the type conversion is performed automatically by the compiler without programmers
intervention, such type of conversion is known as implicit type conversion or type promotion.
The compiler converts all operands into the data type of the largest operand.
The sequence of rules that are applied while evaluating expressions are given below:
1. If either of the operand is of type long double, then others will be converted to long
double and result will be long double.
2. Else, if either of the operand is double, then others are converted to double.
3. Else, if either of the operand is float, then others are converted to float.
4. Else, if either of the operand is unsigned long int, then others will be converted to
unsigned long int.
5. Else, if one of the operand is long int, and the other is unsigned int, then
1. if a long int can represent all values of an unsigned int, the unsigned int is
converted to long int.
6. Else, if either operand is long int then other will be converted to long int.
7. Else, if either operand is unsigned int then others will be converted to unsigned int.
It should be noted that the final result of expression is converted to type of variable on left side
of assignment operator before assigning value to it.
Also, conversion of float to int causes truncation of fractional part, conversion of double to float
causes rounding of digits and the conversion of long int to int causes dropping of excess higher
order bits.
The type conversion performed by the programmer by posing the data type of the expression of
specific type is known as explicit type conversion.
(data_type)expression;
For example,
x=(int)a+b*d;
The following rules have to be followed while converting the expression from one type to
another to avoid the loss of information:
1.Decision making statements: Decision making is about deciding the order of execution of
statements based on certain conditions .
2.Iteration statements:Iteration statements are used to perform looping operations until the given
condition is true.
Decision making statements are of two types-conditional and unconditional decision making
statements.
Conditional Statements in C:
Conditional statements are used to execute a statement or a group of statement based on certain
conditions or test expressions. The various conditional statements are:
1. if statement
2. switch statement
simple if : If test expression is evaluated to true (nonzero), statements inside the body of if is
executed. If test expression is evaluated to false (0), statements inside the body of if is skipped.
If the condition is true then ‘statements of if’ are executed, will be executed, else the control will
be transferred to the next statement after if.
if else statement : In if else if the condition is true then ‘statements of if’ are executed. If it is
false then ‘statements of else’ are executed.
If test expression is true, code inside the body of if statement is executed; and code inside the
body of else statement is skipped.
If test expression is false, code inside the body of else statement is executed; and code inside the
body of if statement is skipped.
else if ladder: When a multipath decision is involved then we use else if ladder.
In else if, if the condition1 is true the statements 1 are executed. If it is false the condition2 in
else if is checked and if it is true it will be executed. If none of the condition is true then default
statements under else are executed.
Nested if - else:
When a series of decisions are involved, we may have to use more than one if-else statement in
nested form
2.Switch: Is a control statement which allows us to make a decision from the number of choices.
expression in the switch must result in integer value. Maximum one of the label values match the
expression value in order for the corresponding statements to execute. Otherwise default
statement executes if present in the switch statement.
Break: When a case is matched then all its statements till break are executed, 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.
The switch statement is often faster than nested if...else (not always). If no case is matched then
statements of default are executed.
Advantages and Disadvantages of switch statement over multiple if else statement.
Advantages:
It is more descriptive
It saves CPU cycles.
It enhances readability
Disadvantages:
Only fixed values can be tested, logical operation (<, >, >/) or relational operation can’t
be.
Only one variable can be used as parameter for the switch statement, whereas nested ‘if
statement’ allow several conditions with different parameter.
3) Ternary or ? : Operator:
Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire
? expression.
If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the
expression.
1) goto statement : In C programming, goto statement is used for altering the normal
sequence of program execution by transferring control to some other part of the program.
syntax:
goto label;
………………..
………………..
label: statement;
Note:
Though goto statement is included in ANSI standard of C, use of goto statement should be
reduced as much as possible in a program.
Reasons to avoid goto statement
Though, using goto statement give power to jump to any part of program, using goto
statement makes the logic of the program complex and tangled.
In modern programming, goto statement is considered a harmful construct and a bad
programming practice.
The goto statement can be replaced in most of C program with the use of break and
continue statements.
In fact, any program in C programming can be perfectly written without the use of goto
statement.
All programmer should try to avoid goto statement as possible as they can.
2.Break statements
In C programming, break statement is used with conditional if statement.
The break is used for terminating the loop immediately after it is encountered.
syntax:
break;
The break statement can be used in terminating all three loops for, while and do...while loops.
see jumping out of loop
continue statements
It is sometimes desirable to skip some statements inside the loop. In such cases, continue
statements are used.
syntax: continue;