Video Link: https://youtu.be/WgS_SF1VrEk Tutorial Link: https://www.programiz.com/c-programming/c-for-loop
Syntax :
for (initializationExpression; testExpression; updateExpression) {
// code inside the for loop
}
#include <stdio.h>
int main() {
for (int i = 0; i < 10; i++) {
printf("%d ", i);
}
return 0;
}
Output
0 1 2 3 4 5 6 7 8 9
#include <stdio.h>
int main() {
for (int i = 0; i < 10; i++) {
printf("Emergency condition \n");
}
return 0;
}
Output
Emergency condition
Emergency condition
Emergency condition
Emergency condition
Emergency condition
Emergency condition
Emergency condition
Emergency condition
Emergency condition
Emergency condition
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum = sum + i;
}
printf("Sum: %d", sum);
return 0;
}
Output
Sum: 5050
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 2; i <= 100; i = i + 2) {
sum = sum + i;
}
printf("Sum: %d", sum);
return 0;
}
Output
Sum: 2550
Q. Can you use the for loop to compute the sum of only odd numbers from 1 to 100. In each iteration, you need to add an odd number to the sum.
sum = sum + 1
sum = sum + 3
sum = sum + 5
....
....
sum = sum + 99
#include <stdio.h>
int main() {
int sum = 0;
for(int i = 1; i <= 100; i = i+2) {
sum = sum + i;
}
printf("Sum of odd numbers from 1 to 100: %d", sum);
return 0;
}
Output
Sum of odd numbers from 1 to 100: 2500
Q. In the for loop below, which is the update statement?
for (int i = 1; i < 5; i++) {...}
Options:
-
int i = 1
-
i < 5
-
i++
-
for
Answer: 3