11.11 Statement, assignment operators, linear accumulation
intro-to-computer-science-gtiit-05-2024.pdf
Review
Increment and decrement operators
int a, b = 0, c = 0;
a = ++b + ++c;
printf("%d %d %d\n", a, b, c);
a = b++ + c++;
printf("%d %d %d\n", a, b, c);
a = ++b + c++;
printf("%d %d %d\n", a, b, c);
a = b-- + --c;
printf("%d %d %d\n", a, b, c);
2 1 1
2 2 2
5 3 3
5 2 2
Today's topics
- Assignment operators
- Definite iteration ("for" loops)
- Computing sums and products of sequences
- The return statement in
main()
- The comma operator
Assignment operators
- In addition to
=
, there are other assignment operators, such as+=
and-=
. - An expression such as:
k = k + 2;
adds 2
to the (old) value of k
, and assigns the result to k
. Also, the expression as a whole will have that value (the value of the right-hand side expression).
* The expression: k += 2;
accomplishes the same task!
The different assignment operators have the following form: variable op= expression
where op
is an operator. This is equivalent to variable = variable op (expression)
Assignment operators |
---|
= += -= *= /= %= >>= <<= &= ^= \|= |
For example
x = 5
(0 0 0 0 0 1 0 1) if x <<= 1
(0 0 0 0 1 0 1 0) becomes x = 10
Move the digits and filled the blank with 0
p ^= q
is xor \(\equiv p\neq q\)
1. &=
(Bitwise AND assignment)
The &=
operator performs a bitwise AND between two numbers and assigns the result to the left-hand variable.
int x = 6; // In binary: 0000 0110
int y = 3; // In binary: 0000 0011
x &= y; // x = x & y; -> 0000 0010 (binary for 2)
printf("%d\n", x); // Output: 2
Explanation:
-
0000 0110
(6) AND0000 0011
(3) results in0000 0010
(2).
2. ^=
(Bitwise XOR assignment)
The ^=
operator performs a bitwise XOR between two numbers and assigns the result to the left-hand variable.
int x = 6; // In binary: 0000 0110
int y = 3; // In binary: 0000 0011
x ^= y; // x = x ^ y; -> 0000 0101 (binary for 5)
printf("%d\n", x); // Output: 5
Explanation:
-
0000 0110
(6) XOR0000 0011
(3) results in0000 0101
(5).
3. |=
(Bitwise OR assignment)
The |=
operator performs a bitwise OR between two numbers and assigns the result to the left-hand variable.
int x = 6; // In binary: 0000 0110
int y = 3; // In binary: 0000 0011
x |= y; // x = x | y; -> 0000 0111 (binary for 7)
printf("%d\n", x); // Output: 7
Explanation:
-
0000 0110
(6) OR0000 0011
(3) results in0000 0111
(7).
These operators are useful when manipulating individual bits in numbers.
Assignment operators - An Example
#include <stdio.h>
/* Computes the factorial of a natural number n.
* The factorial of n is the result of the product (1 * 2 * ... * n).
* The argument is received from standard input.
* The result is printed out on standard output.
*
* Author: John Doe
* Date: April 18th 2023
* Version: 0.1
*/
int main() {
int n;
scanf("%d", &n);
int factorial = 1;
int i = 1;
while (i <= n) {
factorial *= i;
i++;
}
printf("%d\n", factorial);
return 0;
}
The For Statement
-
It provides an additional mechanism to define iteration.
-
It can be reduced to "while" loops (while loops are more expressive).
-
For loops correspond to a form of iteration known as "definite iteration" .
-
In definite iteration, the number of times that the loop body is executed is determined.
- Very useful for certain iteration patterns, e.g., sequence summations and products.
- In C, it has the following syntax:
for (expr1; expr2; expr3) {
statements;
}
For Loops vs While Loops
For loops can also be written as while loops (while loops are more expressive):
for (expr1; expr2; expr3) {
statements;
}
is equivalent to
expr1;
while (expr2) {
statements;
expr3;
}
For Loop to While Loop Reduction
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial = factorial * i;
}
int factorial = 1;
int i = 1;
while (i <= n) {
factorial = factorial * i;
i++;
}
When and How to use a For statement
-
The for loop is best used when the number of times the loop will iterate is determined and can be expressed in relation to iteration variables.
-
It can also favor readability, with initialization/condition/increment all defined at the top of the loop.
-
The iteration variable should not be modified in the loop body.
-
This maintains the good practice of “advancing” iteration at the end of the loop body.
-
Some really bad practices:
-
Modifying the iteration variables in the loop body.
- Writing conditions with side effects.
- Putting statements not related to the iteration in the initialization.
Manual Execution Example
int a = 0;
for (int i = 0; i < 2; i = i + 1) {
a = a + 2;
}
Execution Table
Step | a |
i |
Condition (i < 2 ) |
---|---|---|---|
int a = 0 |
0 | - | - |
int i = 0 |
0 | 0 | - |
i < 2 |
0 | 0 | true |
a = a + 2 |
2 | 0 | - |
i = i + 1 |
2 | 1 | - |
i < 2 |
2 | 1 | true |
a = a + 2 |
4 | 1 | - |
i = i + 1 |
4 | 2 | - |
i < 2 |
4 | 2 | false |
The Continue
Statement in a For Loop
-
We have already discussed that the use of "loop breaking" statements such as
continue
andbreak
should be avoided. -
It is nevertheless useful to know how these work.
-
The
continue
statement in awhile
loop makes the execution "skip" the remainder of the loop body, i.e., it jumps directly to the loop condition. -
In a
for
loop, it makes the execution jump to the increment of the loop. Then, the condition is evaluated.
Sigma Notation (sequence summation)
A mathematical expression that represents the sum of terms of a sequence within a certain range.
Summations and their implementation
Summations can be straightforwardly mapped into programs, using for loops.
Example
The return
statement
-
The
return
statement is used to communicate return values between programs. -
When modularizing programs in functions (we’ll see that later), return statements are used to communicate back the results of function computations.
- In our simple programs, with just a
main()
routine, we often see areturn 0
statement. -
What happens if we put the
return 0;
statement elsewhere? -
The program exits as soon as the return statement is reached.
-
What happens if we change the
0
integer constant by another one, like1
? -
As a convention,
return 0
is used to indicate that the program has successfully achieved its goal (returning a non-zero value will indicate that there has been a problem in the computation).
The Comma Operator
- The comma operator is a binary operator with expressions as operands.
-
In a comma expression of the form
expr1, expr2
-
expr1
is evaluated first, and thenexpr2
. - The comma expression as a whole has the value and type of its right operand.
Example:
a = 0, b = 1
If b
is an int
, then this comma expression has value 1
and type int
.
-
The comma operator sometimes gets used in
for
statements. -
It allows multiple initializations and multiple processing of indices.
For example, the code:
for(sum=0, i=1; i <= n; ++i)
sum += i;
can be used to compute the sum of integers from 1
to n
.
The Comma Operator, a not-so-recommendable example
- Carrying the idea further, we can stuff the entire loop body into the
for
parentheses:
for(sum=0, i=1; i <= n; sum +=i, ++i);
- But not as:
for(sum=0, i=1; i <= n; ++i, sum +=i);
Because it would make ++i
be evaluated first, and sum
would have a different value.
These uses of comma should be avoided.
- Readability and clarity are seriously affected when abusing comma statements.