2. INDEX
Operators
Operator Precedence and assosiativity
Evaluating expressions
Selection and making decisions
Two way Selection
Multiway Selection
Repetition
Concept of Loop
Pre-test and Post-test loops
Initializing and updating
Event Counter Controlled Loops
Loops in C
Statements related to looping
Looping Applications
Summation
Powers
Smallest and Largest Programming Examples
3. EXPRESSIONS IN C
An expression is a sequence of operands and operators that reduces into a single
value.
Expressions can be simple or complex.
An Operator is a syntactical token that represents the action to be taken.
An Operand is an object on which operation is performed.
A simple expression contains only one operator. Example : 2+5.
A complex expression contains more than one operator. Example: 2+5*4.
To evaluate a complex expression we divide it into several simple expressions.
The order in which operators in a complex expression are evaluated is determined by a
set of priorities known as Operator Precedence.
Simple expressions are divided into six categories based on the operator precedence
,number of operands and relative positions of operator and operand.
4.
5. Primary Expressions:
Primary Expression contains only one operand with no operator.
In c, operand is primary expression can be a name, a constant or a parenthesized expression.
Null operator in this expression has precedence.
Names, literal constants, Parenthesized expressions are categories of Primary expressions.
Names:
A name is any identifier for a variable, a function or any other object in the language.
Examples: A B12 PRICE CALC INT_MAX
Literal Constants:
A constant is a piece of data whose value cannot change during the execution of the program.
Examples: 5 123.5 ‘A’ “Welcome”
Parenthetical Expressions:
A value enclosed in parenthesis must be reducible to single value and is therefore a primary
expression.
These may include any complex expression when they are enclosed in parenthesis.
Examples: (2*3+4) (a=2*b+6)
6. Postfix Expressions
The postfix expression consists of one operand followed by one operator.
Function call, postfix increment and post fix decrement and various kinds of postfix
expressions.
Function call:
Function calls are postfix expressions.
The function name is operand and the operator is parenthesis followed by name.
The parenthesis may contain arguments or be empty. When arguments are present they
are part of the operator.
Postfix increment or decrement:
The postfix increment or decrement are also postfix operators.
In the postfix increment (a++) the variable is increased by 1.
Thus a++ results in the variable a being increased by 1 same as a=a+1.
The postfix decrement (a--) also has a value where the value of a is decremented by 1.
7. Prefix Expressions:
In prefix expressions, the operator comes before the operand.
Prefix increment/Decrement:
The operand of a prefix expression must be a variable.
With the prefix operators, the effect takes place before the expression that contains
the operator is evaluated.
Reverse of the postfix operation.
The effect of both postfix and prefix operations are same.
8.
9. Unary Expressions:
A unary expression like a prefix expression consists of one operator followed by one
operand.
Unary expression can have an expression or a variable as operand.
sizeof:
The sizeof operator returns us the size , in bytes of a type or a primary
expression.
By specifying the size of an object during execution, we make our program
more portable to other hardware.
Example: sizeof(int)
It is also possible to find the size of a primary expression.
Example: sizeof -345.23
sizeof x
10. Unary Plus/Minus:
The unary plus and unary minus operators can be used to compute the arithmetic
value of an operand.
The plus operator does not change the value of expression. If the expression is
negative it remains negative.
Minus operator change the value algebraically.
Cast Operator:
The cast operator converts one expression type to another.
For example , to convert an integer into a real number, we would use:
float(x)
With unary operators, only the expression value is changed. Integer variable x is
unchanged.
11. Binary Expressions:
Binary expressions are formed by an operand-operator-operand combination.
Any two numbers added, subtracted, multiplied or divided are usually formed in algebraic notation which is a binary
expression.
Multiplicative Expression:
The result of multiply operator(*) is product of the two operands..
The operands can be any arithmetic type (integral or floating point)
The result of a divide(/) operator depends on the type of the operands.
If one or both operands is of floating point type, the result is floating point quotient.
If both operands are integral type, the result is integral part of the quotient.
Modulo (%) operator divides the first operand by second operand and returns the remainder rather than the
quotient.
Both operands must be integral type and the operator returns the remainder as an integer type.
10 % 3 //evaluates to 1
true % 4 //evaluates to 1
‘A’ % 10 // evaluates to 5
22.3 % 2 //Error: Modulo cannot be floating point.
12. Additive Expressions:
In additive expressions, the second operand is added to or subtracted from the first operand, depending on the operand
used.
The operands in an additive expression can be any arithmetic types (integral point or floating point).
Additive operators have lower precedence than multiplicative operators .
Assignment Expressions:
The assignment expression evaluates the operand on the right side of the operator (=) and places it’s value in the variable on
the left .
The assignment expression has a value in variable on the left.
The value of the expression is the value on the right of assignment operator.
The side effect places places the expression value in the variable on the left of assignment operator.
There are two forms of assignment: simple and compound.
Simple assignment:
The simple assignment is found in algebraic expressions.
a=5 b=x+1 i=i+1
Compound assignment:
A compound assignment is a shorthand notation for a simple assignment .It requires that the left operand be repeated as a
part of right expression. Some of compound expressions: *=,/=,%=,+=,-=.
Compound expression x*=y+3 is evaluated as: x=x*(y+3)
14. C - Operators
An operator is a symbol that tells the compiler to perform specific mathematical or
logical functions. C language is rich in built-in operators and provides the following
types of operators −
1.Arithmetic Operators
2.Relational Operators
3.Logical Operators
4.Bitwise Operators
5.Assignment Operators
6.Misc Operators
22. Ternary Expressions:
Ternary expressions contains a condition followed by two statements or values.
If condition is true the first statement is executed otherwise second statement.
Conditional Operators are sometimes called ternary operators because they take three arguments
Syntax: Condition ? (expression 1) : (expression 2) :
Two expressions are separated by a colon. If condition is true expression1 gets evaluates
otherwise expression2 . The condition is always written before ? Mark.
23. Operator Precedence and associativity:
Precedence is used to determine the order in which different operators in a complex
expression are evaluated.
Associativity is used to determine the order in which operators with same precedence
evaluated in the same expression.
The concept of precedence explains the priority in performing various operations.
Simple example of precedence:
2+3*4
The above expression is actually two binary expressions with one addition and one
multiplication operator.
Precedence of multiplication operator is higher compared to precedence of addition
operator. This results in evaluating multiplication first and then addition.
24. Associativity :
Associativity can be left to right or right to left.
Left to right associativity evaluates expression by starting on left and moving to right.
Example: 3*8/4%4*5
In the above expression we have four operators of same precedence.
Associativity describes how sub expressions are grouped together .
Since their associativity is from left to right they are grouped as follows:
((((3*8)/4)%4)*5)3
Right to left associativity occurs when more than one assignment operator occurs in an
assignment expression.
The assignment operators must be interpreted from right to left.(right most expression
evaluates first.) then it’s value will be assigned to the operand on left of the assignment
operator .
Example: a+=b*=c-=5
(a+=(b*=(c-=5))
(a=a+(b=b*(c=c-5)))
25.
26. Side effects: A side effect is an action that results from the evaluation of an expression . C first
evaluates the expression on the right of assignment operator and then places the value in the left
variable.
Value of the left variable changes once operator on the right side of expression evaluates.
Evaluating Expressions:
To evaluate expressions with out side effects we need to follow rules as mentioned below:
Replace the variables by their values
Evaluate the highest precedence operators and replace them with the resulting
value.
Repeat step 2 until the result is a single value.
To evaluate the expression with side effects ie, parenthesized expressions we need to follow rules
as mentioned below:
Calculate the value of parenthesized expression
Evaluate the postfix expression
Evaluate the prefix expression
Then multiply and division are now evaluated using their associativity rule.
Last step is to evaluate the subtraction.
27. Selection and making decisions in C
Selection and making decisions in C programming includes two catregories:
1. Two way Selection
2. Multiway Selection based on the context.
28. Two- way Selection:
The basic decision making statement in the computer is two-
way selection.
The decision is described to the computer as a conditional
statement that can be answered either true or false.
If the answer is true, one or more action statements are
executed.
If answer is false then a different action or set of actions are
executed.
Regardless of which set of actions is executed the program
continues with the next statement after the selection.
29. C implements two way selection with if…else statement.
An if..else statement is a composite statement used to make
decision between two alternatives.
There are some syntactical points to be followed to implement
if…else statement:
1. The expression must be enclosed in parenthesis.
2. No semicolon is needed for an if..else statement
3. The expression can have a side effect
4. Both the true or false statements can be any
statement or a null statement.
5. Both the statements can be same. Multiple
statements can be combined into a compound
statement through use of braces.
6. We can swap the position of statement 1 and
statement 2 if we use the compliment of original
statement.
30. Null else statement:
If the else condition is not required that is null else statement , if it is null then else can
be omitted.
It is possible to omit the false branch but the true branch can not be omitted.
To eliminate it we need to use rule 6 which allows us to compliment the expression
and swap two statements .
The result is known as complemented if…else .
31.
32. if…else may statements may be any statements including another if…else.
When an if..else is included within an if..else it is known as nested if statement.
There is no limit how many levels can be nested.3
33.
34. Multi-way Selection:
C has two different ways to implement multiway selection.
The first way is by using the switch statement .
The another way is through else-if a convenient way of if elseif implementation.
The switch statement can be used only when the selection condition reduces to an
integral expression.
35. The switch statement:
Switch is a composite statement used to make a decision between many
alternatives.
Although the switch expression can use any expression that reduces to an
integral value, the most common is unary expression in the form of integral
identifier.
The selection alternatives known as case label must be C integral types.
For every possible value in the switch expression a separate case label is
defined.
Everything from the case label to the next case label is a sequence.
The case label simply provides an entry point to start executing the code.
The default label is a special form of case label . It is executed when ever none
of other case values matches the value in switch expression.
36.
37. Rules for switch statement:
The control expression that follows the keyword switch must be an integral type.
Each case label is the keyword case followed by a constant expression.
No two case labels can have the same constant expression value.
But two case labels can be associated with the same set of actions.
The default label is not required. If the value of expression does not match with any
labelled constant expression the control transfers outside of the switch statement .
The switch statement can include at most one default label that can be coded any where
but it is traditionally coded last.
38.
39. The else-if is an artificial C construct that is
only used when
The selection variable is not an integral
The same variable is being tested in the
expression.
40.
41. Repetition
Concept of loop:
Loop is a action that is repeated over and over again .
A loop shall be stopped when the task is done .
To make sure that it ends, we must have a condition that controls the loop .
That means we need to check the condition before and after execution point of each
iteration.
If the task is not achieved then the loop will iterate until the task is done and it
terminates.
42. Pre-test and Post-test loops
In a pretest loop, the control expression is tested first . If it is true, the loop continues
otherwise the loop is terminated.
In posttest loop, in each iteration, the loop actions are executed then the control
expression is tested . If it is true, new iteration starts else loop terminates.
43. Initialization and Updating:
Before a loop can start, we need to declare where the loop need to start.
Such declaration is called initialization.
Initialization can be done before the first execution of the loop body.
It sets the stage for loop actions.
Initialization can be explicit or implicit.
When the initialization is explicit, we include code to set the beginning values of key loop
variables.
Implicit initialization provides no direct initialization code , it rather relies on a pre-existing
situation to control the loop.
Loop Update:
The actions that cause changes to values to the initialized declarations , such changes are called
loop update.
Updating is done in each iteration , usually as the last action.
If the body of the loop is repeated m times, then the updating is done m times.
44.
45. Event and Counter-Controlled loops:
Event – Controlled loops:
In an event – controlled loop, an event changes the control expression from true to false.
When reading data , reaching the end of the data changes the expression from true or false.
In event – controlled loops, the updating process can be implicit or explicit.
If it is explicit, such as finding piece of information , it is controlled by the loop.
If it is implicit, it is controlled by external condition.
Counter – Controlled Loops:
When we know the number of times an action is to be repeated, we use a counter controlled loop.
We must initialize, update and test the counter.
Although we need to know the number of times we want to execute the loop, the number need not be a
constant.
It can also be a variable or a calculated value.
The update can be increment in case where we are counting up and decrement when we are counting down.
Loop Comparison:
The number of iterations of a loop is given as n.
In a pre-test loop, when we come out of the loop, the limit test has been executed n+1 times.
In a post-test loop, when we come out of the loop, the limit test has been executed only n times.
46. Loops in C
C has three loop statements : the while , the for and the
do-while.
The while and the for loops are pretest loops and do-while
is posttest loop.
While and do-while event controlled loops and for is counter
controlled loops.
47. The while-loop:
The while loop is a pretest loop
It uses an expression to control the loop.
As it is a pretest loop, it tests the expression before every iteration of the
loop.
No semicolon is needed at the end of while statement
If we want to include multiple statements in the body, we must put them in a
compound statement block.
48.
49.
50. The for loop:
The for statement is a pretest loop that uses three expressions .
The first expression contains any initialization statement.
The second expression contains limit-test.
The third expression contains updating expression.
Expression 1 is executed when the for starts .
Expression 2 is tested before each iteration .
Since the for loop is a pretest loop, the body of for loop is not
executed if the limit condition is false at the start of loop.
Expression 3 which is update expression is executed at the end of
each loop .
C allows the limit test in Expression 2 to be a variable.
The value of variable shall be changed during the execution of
loop.
If for loop must be executed as a compound statement if for loop
includes more than one statement.
51.
52.
53. The do…while loop:
The do…while statement is a post-test loop.
It uses an expression to control the loop, but it tests the expression / condition after the
execution of the body.
The body of do…while loop must be one and only one statement.
If we need to include multiple statements in the body of loop we must code them inside
the compound statement.
Do….while loop is implemented while data validation.
54.
55.
56. The Comma expression:
A comma expression is a complex expression made of two expressions separated by
commas
Such kind of expressions are mostly used in for statements.
The expressions are evaluated from left to right.
The value and type of the expressions are the value and type of the right expression.
The comma expression has the lowest priority of all expressions.
57.
58. Other statements related to looping:
break, continue statements are related to are other statements related to looping.
break
In a loop, the break statement causes a loop to terminate.
If a code includes a series of nested loops, break statement terminates only the innerloop.
break statement needs a semicolon
break statement can be used in any of the looping statements while, do .. while and for
loops and can be used in switch condition.
continue
The continue statement does not terminate the loop but simply transfers to testing
expression in while and do .. while.
The use of continue statement can also be considered as unstructured programming.
59.
60. Looping Applications:
Summation , product, smallest or largest are common applications of loops.
A common design runs through all the looping statements .
With few exceptions, each loop contains initialization code , looping code,
and disposition code
61. Summation:
When we need to add many numbers or a variable series of numbers we can use the
add operator in a loop.
A sum function has three logical parts: (i)initialization of any necessary working
variables (ii) the loop which includes summation code and any data validation code
and (iii) the disposition code to print or return the result.
In each loop we read the next number and add it to the accumulator
A similar application of counting , is a special case of summation in which we add 1 to
the counter instead of adding the number we read to the accumulator.
62.
63.
64. Powers
A product loop is useful for two common applications: raising a number to a
power and calculating the factorial of a number
Notice that this includes initialization logic to validate the parameter list.
If either of the parameters is invalid , we return zero an error indicator.
For product based applications, such as powers we must initialize it to 1.
65.
66.
67. Smallest and largest
We often encounter a situation we must determine the smallest or
larget among a series of data.
This is also called a natural looping structure.
Each iteration then tests the current smallest to the next number.
If this new number is smaller than the current smallest , we replace
the smallest.
To find the smallest of a series , the initialization sets the initial
value of smallest to the largest.
The loop then proceeds to read a series of numbers and tests each
once against previously stored smallest number.
Since the smallest number starts with maximum integer value, the
first number automatically becomes smallest.
Therefore the result entirely depends on data being read. And
returns smallest number.
To find the largest number, we need to initialize smallest variable to
a very small number.
To find the largest number, we need to initialize the result to a very
large number.