There may be times when a set of tasks need to be repeated. It is done with the help of looping statements.
A loop is a block of code that would repeat for a specified number of times or until some condition is satisfied.
Types:
A definite loop is a loop in which the number of times it is going to execute is known in advance before entering the loop. The number of iterations it is going to repeat will be typically provided through an integer
variable.
Iteration is the
repetition
of a process/task in order to generate a sequence of outcomes desired.
In an indefinite loop, the number of times it is going to execute is not known in advance and it is going to be executed until some condition is satisfied.
Some of common examples that you would need to implement indefinite loops are prompting for reading an input until user inserts a positive integer, reading a password until the user inserts the same password twice in a row, etc.
for
loopwhile
loopdo-while
loopLooping variables are generally named
i
,j
, andk
, while it is not recommended citing clarity of the code.
The examples below will use them for easiness of code understanding, making a note that it should not be followed.
for
loopfor
is the most commonly used definite looping statement.
Syntax:
for(<initialization>; <condition>; <increment/decrement>)
A looping variable
is a variable that decides the flow of a loop.
Eg:
The below example prints numbers from 1
to 10
#include<stdio.h>
int main(){
for(int i = 1; i <= 10; i++){
printf("%d ", i);
}
return 0;
}
The output will be:
1 2 3 4 5 6 7 8 9 10
Explanation:
Here i
is the loop variable
i
is being declared and initialized to 1
as the starting value needed is 1
.i <= 10
is the required condition.i
should be varying from $1 \rightarrow 10$, i.e. for every loop it should be increased by 1 ( i++
unary increment operator is used).Watch for
mismatch
in condition and variable change. Incorrect variable change can causeinfinite
loop.
Note:
for(int <variable> = value; <condition>; <increment/decrement>) {
// Statements
}
int <variable>;
for(<variable> = <value>; <condition>; <increment/decrement>){
// Statements
}
int <variable> = <value>;
for(; <condition>; <increment/decrement>){
// Statements
}
i++ or ++i or i-- or --i // unary operation
i += 10 or i *= 20 // assignment and modification
i = i / 2 or i = 1 + 3 // operation then assignment
#include <stdio.h>
int main() {
for (int i = 1; i <= 10;) {
printf("%d ", i++); // increment done here
}
return 0;
}
++i
) vs post-increment ( i++
)Both of them are unary operators doing the same thing, increasing the value by 1
. Still they have differences to be addressed.
1. Post-increment operator (i++
):
In post incrementation, the value is incremented after use
printf("%d", i++);
is same as
printf("%d", i);
i = i + 1;
So here, since there is a need for old value of i
which is being used, it has to be stored along with the updated value, adding the requirement of an extra space (memory).
2. Pre-increment operator (++i
):
In pre-incrementation, the value is incremented before use.
printf("%d", ++i);
is same as
i = i + 1;
printf("%d", i);
So here, since there is a no need for old value of i
, only the updated value is stored. This does not add the requirement of an extra space (memory), unlike post-increment.
The question is which is better. At points where there is not the need of the old value, pre-increment is efficient, like just incrementing inside increment condition in for
loop.
The explanation above applies for decrement operator (
--
) too.