Loops and control statements

Address Data Source reference: C cycle | newbie tutorial https://www.runoob.com/cprogramming/c-loops.html

1.while cycle

Definition: when a given condition is true, statement or group of statements is repeated. It will test the condition before executing the loop body.

grammar:

while(condition)

{

   statement(s);

}

Here, statement (s) may be a single statement, may be several statements in the code block.

condition may be any expression , when any non-zero value to true. Execution cycle when the condition is true. When the condition is false, exit the loop, the program flow will continue to the next statement followed the loop.

2.        for

Definitions: for  loop allows you to write a loop control structure to perform specified number of times.

Format: for (init; condition; increment)

{ statement(s); }

The control flow for loop:

1) init is executed first, and only once. This step allows you to declare and initialize any loop control variable. You can also write any statement that is not here, as long as there is a semicolon can occur.

2) Next, will determine condition. If true, the loop body is executed. If false, the loop body is not executed, and the control flow will jump to immediately for the next statement cycle.

3) After the main loop for execution, control flow jumps back to increment the above statement. This statement allows you to update the loop control variable. The statement can be left blank, as long as there is a semicolon after the condition can occur.

4) Analyzing condition again. If true, the loop is executed, the process will be repeated (the loop body, and then increased by the step value, and then re-determination condition). When the condition becomes false, for loop terminates

5) *** while focusing on cycling conditions, for focusing on the number of cycles.

6) Notes:

#include <stdio.h>

int main ()

{

/ * For loop is executed * / for (int a = 10; a <20; a = a + 1) {

printf ( "value of a:% d \ n", a);}

return 0;

}

Some compiler error, should read: int a = 10;

for(; a<20; a=a+1){

}

3. do ... while loop

Definition: Unlike  for  and  while  loop, which test loop condition of the loop. In the C language, do ... the while  loop is to check its condition at the end of the cycle.

do ... while loop while loop is similar, but do ... while loop will ensure the implementation of at least one cycle.

Examples: int main ()

{

   int a = 10;

   do{

       printf ( "value of a:% d \ n", a);

          a = a + 1;

   }while( a < 10 );

Run Results: a value of: 10

4. Nested loops

Definition: You can use one or more cycles in while, for or do..while cycle.

4.1 nested for loop

语法:for (initialization; condition; increment/decrement)

{

    statement(s);

    for (initialization; condition; increment/decrement)

    {

        statement(s);

        ... ... ...

    }

    ... ... ...

}

Example: to find prime numbers of the program: int main ()

{

   int i, j;

   for(i=2; i<100; i++) {

      for(j=2; j <= (i/j); j++)

        if break ((i% j)!); // if found, is not a prime number

      if (j> (i / j)) printf ( "% d is a prime number \ n", i);

   }return 0;

}

4.2 nested while loop

Syntax: the while (condition1)

{

    statement(s);

    while (condition2)

    {

        statement(s);

        ... ... ...

    }

    ... ... ...

}

Examples: int main ()

{

    int i=1,j;

    while (i <= 5){

        j=1;

        while (j <= i ){

            printf("%d ",j);j++;

        }printf("\n");

        i++;

       }return 0;

}

operation result

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

 4.3 Nested do ... while loop 

Syntax: do

{

    statement(s);

    do

    {

        statement(s);

        ... ... ...

    }while (condition2);

    ... ... ...

}while (condition1);

Examples: int main ()

{

    int i=1,j;

    do{

        j=1;

              do{

            printf("*");j++;

        }while(j <= i);

        i++;printf("\n");

    }while(i <= 5);

    return 0;

}operation result:

*

**

***

****

*****

2. loop control statements continue, break, goto

2.1 Continue statement

Definitions: C language  continue  statement is a bit like a  break  statement. But it is not mandatory to terminate, continue skips the code in the current cycle, the next cycle forced to start.

For the  for  loop, the Continue  statement after the execution increment statement will still be executed. For  while  and  do ... while  loop, the Continue  statement? Re-execute the conditional statement.

 

Examples: int main ()

{

   int a = 13;

   do{

      if( a == 15){

         / * Skip iteration * /

         a ++; printf ( "skip iterative \ n");

         continue;

      }

      printf ( "value of a:% d \ n", a);

      a++;

   }while( a < 17);

   return 0;

}

operation result:

The value of a: 13

The value of a: 14

Skip iteration

The value of a: 16

Examples: int main ()

{

   int a = 13, i;

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

       if(i==1){

              printf ( "skip \ n"); continue;

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

   } 

}operation result:

i=0

jump over

i=2

i=3

Guess you like

Origin www.cnblogs.com/bihua/p/12029214.html