C Language Advanced--loop statement 07

Source: Internet
Author: User

The basic way to work with circular statements:
    • Determine whether a loop body is executed by a conditional expression
    • Conditional expressions Follow the principle of an if statement expression
The difference between do,while,for:
    • Do statements are executed and then judged, and the loop body executes at least once
    • While statements are first judged and executed, the loop body may not execute
    • The For statement is first judged and then executed, which is more concise than the while

Do.....while the way the statement is looped:

 Do {    //loop}while (condition)

The loop mode of the while statement:

 while (condition) {    //Loop}

How to loop for the For statement:

 for (initialize; condition; Change the value of a condition) {     //loop  }

program Example 1:

#include <stdio.h>intF1 (intN) {    intRET =0; if(N >0 )    {         Do{ret+=N; N--; }         while(N >0 ); }        returnret;}intF2 (intN) {    intRET =0;  while(N >0) {ret+=N; N--; }        returnret;}intF3 (intN) {    intRET =0; inti =0;  for(i=1; i<=n; i++) {ret+=i; }        returnret;}intMain () {printf ("%d\n", F1 ( -)); printf ("%d\n", F2 ( -)); printf ("%d\n", F3 ( -)); return 0;}

This code is very concise, you can look a little bit, as a review

The difference between break and continue:
    • Break indicates the execution of the entire loop is terminated
    • Continue means terminating this cycle into the next cycle

In the loop you can use continue and break two statements, Lenovo to switch, inside each case must have a break statement, then we can use the Continue statement inside? The answer is no, the compilation will be an error.

program Example 2:

#include <stdio.h>voidF1 (intN) {    inti =0;  for(i=1; i<=n; i++)    {        if((i%2) ==0 )        {             Break; } printf ("%d", i); } printf ("\ n");}voidF2 (intN) {    inti =0;  for(i=1; i<=n; i++)    {        if((i%2) ==0 )        {            Continue; } printf ("%d", i); } printf ("\ n");}intMain () {F1 (Ten); F2 (Ten); return 0;}

This code is also very concise, you can see the right as a review

program Example 3:

1#include <stdio.h>2#include <malloc.h>3 4 intFuncintN)5 {6     inti =0;7     intRET =0;8     int* p = (int*)malloc(sizeof(int) *n);9     Ten      Do One     { A         if(NULL = = p) Break; -          -         if(N <5) Break; the  -         if(N > -) Break; -          -          for(i=0; i<n; i++) +         { -P[i] =i; +printf"%d\n", P[i]); A         } at          -RET =1; -} while(0 ); -      -printf"Free (p) \ n"); -      in      Free(p); -      to     returnret; + } -  the intMain () * { $     if(Func (Ten) )Panax Notoginseng     { -printf"ok\n"); the     } +     Else A     { theprintf"error\n"); +     } -  $     return 0; $}

With 29 lines of code I can extend it to free a null pointer. Another wild pointer, the null pointer is by the difference, for this concept I will write another blog to supplement, after all, and this blog theme does not match, hehe.

Summary:
    • The For loop is first judged after entering the loop body
    • The For loop is suitable for occasions where the number of loops is fixed
    • The while loop is first judged in the loop to enter the body execution
    • The while loop is suitable for situations where the cycle times are variable
    • The Do....while loop first executes the loop body and then the condition is judged
    • Do.....while cycle at least once loop body

C Language Advanced--loop statement 07

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.