10-DoWhile-Infinit-Cont-Function-After Mid
10-DoWhile-Infinit-Cont-Function-After Mid
10-DoWhile-Infinit-Cont-Function-After Mid
do...while loop in C
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C
programming checks its condition at the bottom of the loop. A do...while loop is similar to a while loop,
except the fact that it is guaranteed to execute at least one time.
Syntax
do
{
statement(s);
}
while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop
executes once before the condition is tested. If the condition is true, the flow of control jumps back up
to do, and the statement(s) in the loop executes again. This process repeats until the given condition
becomes false.
Flow Diagram
Example
#include <stdio.h>
int main () {
int a = 10;
do {
printf("value of a: %d\n", a);
a = a + 1;
}
while( a < 15 );
return 0;
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
What is nested loop?
C programming allows to use one loop inside another loop. The following section shows a few examples
to illustrate the concept. You can put any type of loop inside any other type of loop. For example, a 'for'
loop can be inside a 'while' loop or vice versa.
infinite loop
Example
The following program will be executed indefinitely
#include <stdio.h>
int main ()
{
int i, j;
for(i = 2; i<10; i++)
{
printf("This loop will execute indefinitely \n");
i--;
}
return 0;}
Continue Statement in C
Flow Diagram
Example
#include <stdio.h>
int main () {
int a = 10;
do {
if( a == 15) {
a = a + 1;
continue;
}
printf("value of a: %d\n", a);
a++;
}
while( a < 20 );
return 0;
}
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Function in C
A large C program is divided into blocks or groups called C function. C function contains set of
instructions enclosed by “{ }” which performs specific operation in a C program. Every
C program has at least one function, which is main(), and all the programs can define
additional functions. Actually, Collection of these functions creates a C program.
C functions are used to avoid rewriting same logic/code again and again in a program.
There is no limit in calling C functions to make use of same functionality wherever
required.
We can call functions any number of times in a program and from any place in a
program.
A large C program can easily be tracked when it is divided into functions.
The core concept of C functions are, re-usability, dividing a big task into small pieces to
achieve the functionality and to improve understandability of very large C programs.
Example
#include<stdio.h>
#include<conio.h>
Output:
This line is executed from main function
The square root calculated inside the SQUARE function is = 25