English, asked by vidyathangjam2564, 1 year ago

Different types of iterative statements with examples

Answers

Answered by ShaGańSandHU
2
Iteration statements cause embedded statements to be executed a number of times, subject to the loop-termination criteria. Thesestatements are executed in order, except when a jumpstatement is encountered. + The following keywords are used in iteration statements: do.
Answered by s8528054954
4

Answer:

 while statement

The while loop in C is most fundamental loop statement. It repeats a statement or block while its controlling expression is true. Here is its general form:

while(condition) { // body of loop }

The condition can be any boolean expression. The body of the loop will be executed as long as the conditional expression is true.

Here is more practical example.

//example program to illustrate while looping #include <stdio.h> int main () {     int n = 10;     while (n > 0)     {         printf("tick %d", n);         printf("\n");         n--;     } }

The ouput of the program is:

tick 10 tick 9 tick 8 tick 7 tick 6 tick 5 tick 4 tick 3 tick 2 tick 1

2. do-while statement

The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop. Its general form is:

do{ // body of loop } while(condition);

Each iteration of the do-while loop first executes the body of the loop and then evaluates the conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop terminates. As with all of C’s loops, condition must be a Boolean expression.

The program presented in previous while statement can be re-written using do-while as:

//example program to illustrate do-while looping #include <stdio.h> int main () {     int n = 10;     do     {         printf("tick %d", n);         printf("\n");         n--;     }while (n > 0); }

3. for loop

Here is the general form of the traditional for statement:

for(initialization; condition; iteration) { // body }

The program from previous example can be re-written using for loop as:

//example program to illustrate for looping #include <stdio.h> int main () {     int n;     for(n = 10; n>0 ; n--){         printf("tick %d",n);         printf("\n");     } }

The ouput of the program is same as output from program in while loop.

There can be more than one statement in initilaization and iteration section. They must be separated with comma.

Here, we've presented the example to illustrate this.

//example program to illustrate more than one statement using the comma // for looping #include <stdio.h> int main () {     int a, b;     for (a = 1, b = 4; a < b; a++, b--)     {         printf("a = %d \n", a);         printf("b = %d \n", b);     } }

The ouput of the program is:

a = 1 b = 4 a = 2 b = 3

Similar questions