10-DoWhile-Infinit-Cont-Function-After Mid

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Lecture - 10- [17,18]

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.

Nested for Loop


Syntax
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);}

Nested While Loop


Syntax
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}

Nested do...while loop


Syntax
do
{
statement(s);
do
{
statement(s);
}
while( condition );
}
while( condition );
Example
The following program uses a nested for loop to find the prime numbers from 2 to 100 −
#include <stdio.h>
int main ()
{
int i, j;
for(i = 2; i<10; i++)
{ //Find prime numbers between 2 and 10
for(j = 2; j <= (i/j); j++)
if(!(i%j))
break; // if factor found, not prime
if(j > (i/j))
printf("%d is prime\n", i);
}
return 0;}
Output
2 is prime
3 is prime
5 is prime
7 is prime

infinite loop 

An infinite loop also called endless loop is a sequence of instructions in a computer program


which executes endlessly, either due to the loop having no terminating condition, result that
can never be found, or one that causes the loop to start again and again.

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

The continue statement in C programming works somewhat like the break statement. Instead


of forcing termination, it forces the next iteration of the loop to take place, skipping any code
in between.
For the for loop, continue statement causes the conditional test and increment portions of the
loop to execute. For the while and do...while loops,continue statement causes the program
control to pass to the conditional tests.
Syntax
continue;

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.

Why to use C functions

 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.

C function declaration, function call and function definition

Following are three aspects in each C function:

 Function declaration - A function declaration is sometime called function prototype


or function signature. This informs compiler about the function name, function
parameters and  return value’s data type.
return_type function_name ( argument list );
 Function call – This calls the actual function
function_name ( arguments list );
 Function definition – This contains all the statements to be executed.
return_type function_name ( arguments list )

Body of function;
}

Example

#include<stdio.h>
#include<conio.h>

void square ( ); // function prototype, also called function declaration


void main( )
{

printf ( "This line is executed from main function\n");


square ( ) ; // function call
}
void square () // function definition
{
int number1, answer;
number1=5;
answer = number1 * number1 ;
printf ( "The square root calculated inside the SQUARE function is =%d", answer);

Output:
This line is executed from main function
The square root calculated inside the SQUARE function is = 25

You might also like