Today lets learn about another loop control statement i.e., for loop. For loop is used to repeatedly execute certain set of instructions.
For loop allows us to specify 3 things in a single line:
1. Loop count initialization.
2. Condition Checking.
3. Modification Statement(increment/decrement statements).
Related Read
while loop in C programming
Relational Operators In C
Logical Operators In C
Video Tutorial: For Loop In C Programming Language
[youtube https://www.youtube.com/watch?v=cMsK_r5kRlo]
Source Code: For Loop In C Programming Language
#include<stdio.h> int main() { int i; for(i = 0; i < 10; i++) { printf("%d IBM\n", i ); } return 0; }
Output:
0 IBM
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
#include<stdio.h> int main() { int i; for(i = 0; i < 10; i++) { printf("%d IBM\n", i + 1); } return 0; }
Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM
i = 1 and i<= 10
#include<stdio.h> int main() { int i; for(i = 1; i <= 10; i++) { printf("%d IBM\n", i); } return 0; }
Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM
No initialization statement and No Modification Statement
#include<stdio.h> int main() { int i = 0; for(; i < 10;) { printf("%d IBM\n", i + 1); i = i + 1; } return 0; }
Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM
No Curly braces and No initialization statement
#include<stdio.h> int main() { int i = 0; for(; (i < 10); i = i + 1) printf("%d IBM\n", i + 1); return 0; }
Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert