Professional Documents
Culture Documents
Module 2-BPOPS203
Module 2-BPOPS203
MODULE 2 ( Part 1 of 2)
9.15 Operators
9.15 Operators in C:
An operator is a symbol that specifies the mathematical, logical or relational operation to be performed.
The operators can be categorized as follows:
1. Arithmetic operators
2. Relational Operators
3. Equality Operators
4. Logical Operators
5. Unary Operators
6. Conditional Operators
7. Bitwise Operators
8. Assignment operators
9. Comma Operator
10. Size of Operator
Arithmetic operators can be applied to any integer or floating-point numbers. The operator % (modulus)
finds the remainder of an integer division. This operator can be applied only to integer operands.
OPERATOR MEANING
== returns 1 if both operands are equal, 0 otherwise
!= returns 1 if operands do not have the same value, 0
otherwise
Logical AND: A binary operator, which simultaneously evaluates two values or relational
expressions. If both the operands are true, then the whole expression evaluates to true. If both or
one of the operands is false, then the whole expression evaluates to false.
A B A &&B
0 0 0
0 1 0
1 0 0
1 1 1
Logical OR: Returns a false value of both the operands is false. Otherwise it returns a true value.
A B A || B
0 0 0
0 1 1
1 0 1
1 1 1
Logical NOT: Takes a single expression and negates the value of the expression.
A !A
0 1
1 0
Unary operators act on single operands. C language supports three unary operators. They are unary
minus, increment and decrement operators.
Unary Minus: When an operand is preceded by a minus sign, the unary operator negates its
value.
Increment Operator (++) and Decrement Operator (- -) : The increment operator is a unary
operator that increases the value of its operand by 1. Similarly, the decrement operator
decreases the value of its operand by 1.
9.16 Type CONVERSION AND TYPECASTING: Type conversion is used to convert data of one type
to data of another type.
Type conversion is of 2 types:
1. Implicit type conversion: C permits mixing of constants and variables of different types in
an expression. C automatically converts any intermediate values to the proper type so that
the expression can be evaluated without losing any significance. This automatic conversion
is known as implicit type Conversion.
2. Explicit conversion: When the data of one type is converted explicitly to another type with
the help of some pre-defined functions, it is called as explicit conversion. There may be
data loss in this process because the conversion is forceful.
------------####------------
5
MODULE 2 ( Part 2 of 2)
DECISION CONTROL AND LOOPING STATEMENTS
10.2.1 if statement
If statement is the simplest form of decision control statements that is frequently used in decision making..
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, 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.
Updating the loop variable may include incrementing the loop variable, decrementing the loop
variable or setting it to some other value.
loop, because this is easiest to control. The 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. In C, loops can be nested to any desired level. However, loops should
be properly indented so that a reader can easily determine which statements are contained within each for
statement.
Hence, we conclude that the continue statement is somewhat the opposite of the break statement. It forces
the next iteration of the loop to take place, skipping any code in between itself and the test condition of
the loop. The continue statement is usually used to restart a statement sequence when an error occurs.
Here, label is an identifier that specifics 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
10
the goto statement. Whenever the goto statement is encountered the control is immediately transferred to
the statement following the label. Therefore, goto statement breaks the normal sequential execution of the
program. 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 The goto statement is often combined
with the if statement to cause a conditional transfer of control.
Conclusion:
It is not necessary to use goto statement as it can always be eliminated by rearranging the code.
Using the goto statement violates the rules of structured programming.
It is a good programming practice to use break, continue, and return statements in preference to
goto whenever possible.
Goto statements make the program code complicated and render the program unreadable.
------------####------------