✔ C-LANGUAGE CHAPTERS:-
1. C-Introduction
2. C-Constants
3. C-Basic Programs
4. C-Instructors
5. C-Operators
6. C-If Else Statement
7. C-Loop Contructs
8. C-Nesting Of Loops
9. C-Programming Examples
10. C-Functions
11. C-Functions Naming Convention
12. C-Pointers
13. C-Arrays
14. C-Arrays & Functions
15. C-Strings
16. C-Structures
17. C-Union
18. C-Storage Classes
19. C-PreProcessor
20. C-Header Files
21. C-R/W one Line
22. C-Accessing Random File
23. C-Important Point
24. C-Interview Questions
1. C-Introduction
2. C-Constants
3. C-Basic Programs
4. C-Instructors
5. C-Operators
6. C-If Else Statement
7. C-Loop Contructs
8. C-Nesting Of Loops
9. C-Programming Examples
10. C-Functions
11. C-Functions Naming Convention
12. C-Pointers
13. C-Arrays
14. C-Arrays & Functions
15. C-Strings
16. C-Structures
17. C-Union
18. C-Storage Classes
19. C-PreProcessor
20. C-Header Files
21. C-R/W one Line
22. C-Accessing Random File
23. C-Important Point
24. C-Interview Questions
C-Loop Constructs performs a particular set of instructions again and again until a specified condition will not become false. The specified condition helps compiler in termination of loops.
This construct is also called Iteration/Repetitive construct.
In C programming, Loops can be categorized into two ways:
- Determinate
- Inderminate
- Pre Tested
- Post Tested
- Determinate: - In this, number of steps that are to be performed are known to programmer.
Eg: Determinate loop is for loop.
- Inderminate: - In this, number of steps that are to be performed are not known to the programmer.
They are further divided into two: -
- Pre Tested: - in this, conditions are checked first, and then the body of the loop is executed.
- Post Tested: - in this, conditions are checked after the body of the loop is executed.
Indeterminate + Pre Tested = while
Indeterminate + Post Tested = do while
‘C’ supports three looping constructs:
- The while loop
- The for loop
- The do while loop
- While loop: - this loop is suitable in situations where knowledge of the termination condition is not known. while loop evaluated or check the condition first before entering into the loop and if condition is true then only control enters into loop. It is called Entry controlled loop.
Syntax: while(condition)
{
-----------;
-----------; Body of Loop
-----------;
Increment/decrement;
}
Eg: Write a program to print person`s name 20 times on screen [using while loop]
Solution: #include<stdio.h>
void main( )
{
int i = 1;
while( i <= 20)
{
printf(“Hello Gaurav…!!”);
i++;
}
}
- for loop: - this is very common loop and used with array normally. It is also a pre tested + determinate loop and it is also called as Entry Controlled loop.
Syntax: for(Initialization; Test condition; increment/decrement )
{
-----------;
-----------; Body of Loop
-----------;
}
The for loop contains 3 parts:
- initial condition, that contains initial values of all the loop counter
- test condition, it specifies the condition until the loop has to be executed it is also called repetitions desired section
- increment/decrement, it contains value by which a loop counter is to be increased or decreased.
Eg: write a program to print person`s name 20 times on screen [using while loop]
Solution: #include<stdio.h>
void main( )
{
for( i = 0; i < 20; i++)
{
printf(“Hello MGaurav…!!”);
}
}
- do while loop: - this loop ensure that the program will get executed at least for once whether the condition is false or true. It is an Exit controlled loop.
Syntax: do
{
-----------;
-----------; Body of Loop
-----------;
Increment/decrement;
} while(condition);
The statement written in loop will execute at least once, if the condition is false and then come out from the loop and if the condition is true then body of the loop is executed until the condition is true.
Eg: write a program to print person`s name 20 times on screen [using while loop]
Solution: #include<stdio.h>
void main( )
{
int i = 1;
do
{
printf(“Hello Gaurav…!!”);
i++;
}while(i <= 20);
}
// The while condition part must be terminated by semicolon( ; ).
What is the Difference between while and do while loop?
while | do while |
1. It is an entry controlled loop | 1. It is an exit controlled loop |
2. First, condition will check and then enters into the body of loop. | 2. First, enters into the body of loop and then check the condition. |
3. If the condition becomes false for first time then loop will not get executed. | 3. If the condition becomes false for first time then also loop will get executed for at least once. |
4. No semicolon is needed after while clause. | 4. Semicolon is needed |