0% found this document useful (0 votes)
222 views

C Programming While and Do

This document discusses while and do-while loops in C programming. While loops repeat a block of code as long as a test expression is true, while do-while loops always execute the code block at least once before checking the test expression. Examples are provided to demonstrate how each loop type works, including nested loops and using break to exit a loop early. Key differences between while and do-while loops are that do-while loops always execute the code block at least once regardless of the test expression.

Uploaded by

Joshua
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views

C Programming While and Do

This document discusses while and do-while loops in C programming. While loops repeat a block of code as long as a test expression is true, while do-while loops always execute the code block at least once before checking the test expression. Examples are provided to demonstrate how each loop type works, including nested loops and using break to exit a loop early. Key differences between while and do-while loops are that do-while loops always execute the code block at least once regardless of the test expression.

Uploaded by

Joshua
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

C Programming while and do...

while Loop
Loops are used in programming to repeat a specific block of code. After reading
this tutorial, you will learn how to create a while and do...while loop in C
programming.

Loops are used in programming to repeat a specific block until some end condition is
met. There are three loops in C programming:

1. for loop
2. while loop
3. do...while loop

while loop
The syntax of a while loop is:
while (testExpression)

//codes

where, testExpression checks the condition is true or false before each loop.

How while loop works?


The while loop evaluates the test expression.

If the test expression is true (nonzero), codes inside the body of while loop are
exectued. The test expression is evaluated again. The process goes on until the test
expression is false.

When the test expression is false, the while loop is terminated.

Flowchart of while loop


Example #1: while loop
// Program to find factorial of a number
// For a positive integer n, factorial = 1*2*3...n

#include <stdio.h>
int main()
{
int number;
long long factorial;

printf("Enter an integer: ");


scanf("%d",&number);
factorial = 1;

// loop terminates when number is less than or equal to 0


while (number > 0)
{
factorial *= number; // factorial = factorial*number;
--number;
}

printf("Factorial= %lld", factorial);

return 0;
}

Output

Enter an integer: 5

Factorial = 120

To learn more on test expression (when test expression is evaluated to nonzero (true)
and 0 (false)), check out relational and logical operators.

do...while loop
The do..while loop is similar to the while loop with one important difference. The body
of do...while loop is executed once, before checking the test expression. Hence,
the do...while loop is executed at least once.

do...while loop Syntax


do
{

// codes

while (testExpression);

How do...while loop works?


The code block (loop body) inside the braces is executed once.

Then, the test expression is evaluated. If the test expression is true, the loop body is
executed again. This process goes on until the test expression is evaluated to 0 (false).

When the test expression is false (nonzero), the do...while loop is terminated.


Example #2: do...while loop
// Program to add numbers until user enters zero

#include <stdio.h>
int main()
{
double number, sum = 0;

// loop body is executed at least once


do
{
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);

printf("Sum = %.2lf",sum);

return 0;
}

Output

Enter a number: 1.5

Enter a number: 2.4

Enter a number: -3.4

Enter a number: 4.2

Enter a number: 0

Sum = 4.70

Example 2: C++ do...while Loop


// C++ program to add numbers until user enters 0

#include <iostream>
using namespace std;

int main()
{
float number, sum = 0.0;
do {
cout<<"Enter a number: ";
cin>>number;
sum += number;
}
while(number != 0.0);

cout<<"Total sum = "<<sum;

return 0;
}

Output

Enter a number: 2

Enter a number: 3

Enter a number: 4

Enter a number: -4

Enter a number: 2

Enter a number: 4.4

Enter a number: 2

Enter a number: 0

Example of do while loop


#include <stdio.h>
int main()
{
int j=0;
do
{
printf("Value of variable j is: %d\n", j);
j++;
}while (j<=3);
return 0;
}
Output:

Value of variable j is: 0


Value of variable j is: 1
Value of variable j is: 2
Value of variable j is: 3
C# - do while
The do-while loop is the same as a 'while' loop except that the block of code
will be executed at least once, because it first executes the block of code
and then it checks the condition.

Syntax:
do
{
//execute code block

} while(boolean expression);
As per the syntax above, do-while loop starts with the 'do' keyword followed
by a code block and boolean expression with 'while'.

Example: do while loop


int i = 0;

do
{
Console.WriteLine("Value of i: {0}", i);

i++;

} while (i < 10);

Try it
Output:

Value of i: 0 

Value of i: 1 

Value of i: 2 

Value of i: 3 

Value of i: 4 

Value of i: 5 
Value of i: 6 

Value of i: 7 

Value of i: 8 

Value of i: 9

Just as in the case of the for and while loops, you can break out of the do-
while loop using the break keyword.

Example: break inside do-while


int i = 0;

do
{
Console.WriteLine("Value of i: {0}", i);

i++;

if (i > 5)
break;

} while (true);

Try it
Output:

Value of i: 0 

Value of i: 1 

Value of i: 2 

Value of i: 3 

Value of i: 4 

Value of i: 5
Nested do-while
The do-while loop can be used inside another do-while loop.

Example: Nested do while loop


int i = 0;

do
{
Console.WriteLine("Value of i: {0}", i);
int j = i;

i++;

do
{
Console.WriteLine("Value of j: {0}", j);
j++;

} while (j < 2);

} while (i < 2);

Try it
Output:

Value of i: 0 

Value of j: 0

Value of j: 1

Value of i: 1

Value of j: 1

 Points to Remember :
1. The do-while loop executes the block of code repeatedly.
2. The do-while loop execute the code atleast once. It includes the conditional
expression after the code block and the increment/decrement step should be
inside the loop.
3. Use the break keyword to stop the execution and exit from a do-while loop.
4. An nested do-while loop is allowed.

You might also like