An iteration or loop in a computer program is a set of statements that is executed repeatedly.
C provides three iteration constructs:
Syntax on while statement:-
Syntax on do..while statement:-
Syntax on for statement:-
The while loop is used to perform a set of actions as long as the given condition is true.
The condition for the while loop can be any expression, including a numeric constant value.
The do…while loop is also used to perform a set of actions as long as the given condition is true. But here, the condition is checked after the actions have been performed; hence, the loop executes at least once.
Break statement:- Upon encountering a break statement, the compiler stops the loop execution immediately and the control proceeds to the statement (if any) after the loop.
Any statement placed after the break statement is not executed for that iteration if the break statement is executed
Continue statement:- continue is used to skip further statements within a loop and proceed to the next iteration of the loop.
Any statement written after continue is not executed for that iteration when the continue statement is encountered.
A loop enclosed within another loop is called a nested loop. When two loops are nested, the outer loop takes control of the number of complete repetitions of the inner.
C Program to print 1 to 10 numbers using while loop.
C Program to print 1 to 10 numbers using do..while loop.
C Program to print 1 to 10 numbers using for loop.
<<== C Basic
C provides three iteration constructs:
- While
- Do…while
- For
Syntax on while statement:-
initialization;
while(condition)
{
statement or body of the loop;
increment/decrements;
}
Syntax on do..while statement:-
initialization;
do
{
statement or body of the loop;
increment/decrements;
}while(condition);
Syntax on for statement:-
for (initialization; condition; increment/decrements){
statement or body of the loop;
}
The while loop is used to perform a set of actions as long as the given condition is true.
The condition for the while loop can be any expression, including a numeric constant value.
The do…while loop is also used to perform a set of actions as long as the given condition is true. But here, the condition is checked after the actions have been performed; hence, the loop executes at least once.
Break statement:- Upon encountering a break statement, the compiler stops the loop execution immediately and the control proceeds to the statement (if any) after the loop.
Any statement placed after the break statement is not executed for that iteration if the break statement is executed
Continue statement:- continue is used to skip further statements within a loop and proceed to the next iteration of the loop.
Any statement written after continue is not executed for that iteration when the continue statement is encountered.
A loop enclosed within another loop is called a nested loop. When two loops are nested, the outer loop takes control of the number of complete repetitions of the inner.
#include<stdio.h> main() { int i; i=1; while(i<=10) { printf("%d\n", i); i++; } getch(); }
C Program to print 1 to 10 numbers using do..while loop.
#include<stdio.h> main() { int i; i=1; do { printf("%d\n", i); i++; } while(i<=10); getch(); }
C Program to print 1 to 10 numbers using for loop.
#include<stdio.h> main() { int i; for(i=1; i<=10; i++) { printf("%d\n", i); } getch(); }
<<== C Basic
No comments:
Post a Comment