0% found this document useful (0 votes)
67 views5 pages

While Loop Is Used When You Want To Execute A Block of Statements Repeatedly With Checked

The document summarizes different loop structures in C programming including while, do-while, for, break, and continue. The while loop executes a block of code as long as a condition is true. The do-while loop ensures a block of code executes at least once before checking the condition. The for loop allows initialization of a counter, condition to check on each iteration, and increment/decrement of the counter. The break statement exits the current loop and continue skips to the next iteration. Code examples are provided to demonstrate usage of each loop type.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views5 pages

While Loop Is Used When You Want To Execute A Block of Statements Repeatedly With Checked

The document summarizes different loop structures in C programming including while, do-while, for, break, and continue. The while loop executes a block of code as long as a condition is true. The do-while loop ensures a block of code executes at least once before checking the condition. The for loop allows initialization of a counter, condition to check on each iteration, and increment/decrement of the counter. The break statement exits the current loop and continue skips to the next iteration. Code examples are provided to demonstrate usage of each loop type.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

WHILE LOOP

A loop statement allows you to execute a statement or block of statements repeatedly. The
while loop is used when you want to execute a block of statements repeatedly with checked
condition before making an iteration. Here is syntax of while loop statement:

while (expression) {

  // statements

This loop executes as long as the given logical expression between parentheses after while is
true. When expression is false, execution continues with the statement following the loop
block. The expression is checked at the beginning of the loop, so if it is initially false, the loop
statement block will not be executed at all. And it is necessary to update loop conditions in loop
body to avoid loop forever. If you want to escape loop body when a certain condition meet, you
can use break statement

Here is a while loop statement demonstration program:

#include <stdio.h>

void main(){

int x = 10;
int i = 0;

// using while loop statement

while(i < x)
{
i++;
printf("%d\n",i);

// when number 5 found, escape loop body

int numberFound= 5;
int j = 1;

while(j < x){

if(j == numberFound){

printf("number found\n");

break;

printf("%d...keep finding\n",j);

j++;

And here is the output:

1
2
3
4
5
6
7
8
9
10
1...keep finding
2...keep finding
3...keep finding
4...keep finding
number found

DO-WHILE

do while loop statement allows you to execute code block in loop body at least one. Here is do
while loop syntax:
do {

  // statements
} while (expression);

Here is an example of using do while loop statement:

#include <stdio.h>

void main(){

    int x = 5;
    int i = 0;
    // using do while loop statement

    do{

        i++;

        printf("%d\n",i);
    }while(i < x);

  
}

The program above print exactly 5 times as indicated in do while loop body

1
2
3
4
5

FOR
or (initialization_expression;loop_condition;increment_expression){
// statements
}

There are three parts that are separated by semi-colons in control block of the C for loop.

 initialization_expression is executed before execution of the loop starts. This is typically


used to initialize a counter for the number of loop iterations. You can initialize a counter
for the loop in this part.
 The execution of the loop continues until the loop_condition is false. This expression is
checked at the beginning of each loop iteration.
 The increment_expression, is usually used to increase (or decrease) the loop counter. This
part is executed at the end of each loop iteration.
Here is an example of using C for loop statement to print an integer from 0 to 4 on screen:

#include <stdio.h>

void main(){
// using for loop statement
int max = 5;
int i;
for(i = 0; i < max;i++){

printf("%d\n",i);

}
}

And the output is

0
1
2
3
4

BREAK

break statement is used to break any type of loop such as while, do while an for loop. break
statement terminates the loop body immediately. continue statement is used to break current
iteration. After continue statement the control returns to the top of the loop test conditions.

Here is an example of using break and continue statement:

#include <stdio.h>
#define SIZE 10
void main(){

// demonstration of using break statement

int items[SIZE] = {1,3,2,4,5,6,9,7,10,0};


int number_found = 4,i;

for(i = 0; i < SIZE;i++){

if(items[i] == number_found)
{

printf("number found at position %d\n",i);

break;// break the loop


}
printf("finding at position %d\n",i);
}

// demonstration of using continue statement


for(i = 0; i < SIZE;i++){

if(items[i] != number_found){

printf("finding at position %d\n",i);

continue;// break current iteration


}

// print number found and break the loop

printf("number found at position %d\n",i);

break;
}

Here is the output

finding at position 0
finding at position 1
finding at position 2
number found at position 3
finding at position 0
finding at position 1
finding at position 2
number found at position 3

You might also like