Dark Horse programmer--"Dark Horse video Notes" The use of the C language Foundation while

Source: Internet
Author: User

One. Format:

   1:  while (condition)
   2:  
   3:  {
   4:  
   5:   loop body
   6:  
   7:  }
   8:  

Two. Operating principle

1. If the initial condition is not established, the loop body will never be executed

2. If the condition is set up, a loop body is executed, the execution is completed, and the condition is determined again.

Three. While common two statements: break and Continue

   1:  int count = 0;
   2:  
   3:  while (count<50)
   4:  
   5:  {
   6:  
   7:  ++count;
   8:  
   9:  if (count%10! = 0)
  Ten:  
One   :  {
  :  
  :  printf ("Do%d push-ups \ n", Count);
  :  
  :  }
  :  
  :  }
  :  

1.break, directly ending the entire while loop

   1:  int count=0;
   2:  
   3:  while (Count < 50)
   4:  
   5:  {
   6:  
   7:  ++count;
   8:  
   9:  printf ("Do%d push-ups \ n", Count);
  Ten:  
One   :  If (count = = 20)
  :  
  :  {
  :  
  :  break;
  :  
  :  }
  :  
  :  }
  :  
  :  return 0;
  :  
  :  }
  :  

2.continue, end the current loop body and go to the next loop body execution

   1:  int count=0;
   2:  
   3:  while (count<50)
   4:  
   5:  {
   6:  
   7:  ++count;
   8:  
   9:  if (count%10 = = 0)
  Ten:  
One   :  {
  :  
  :  //end this cycle directly and enter the next cycle
  :  
  :  continue;
  :  
  :  }
  :  
  :  printf ("Do%d push-ups \ n", Count);
  :  
  :  }
  :  

Four. The common dead loop of the while statement

1. The simplest cycle of death

   1:  while (1);

2. Never-ending "hahaha"

   1:  while (10)
   2:  
   3:  {
   4:  
   5:  printf ("haha \ n");
   6:  
   7:  }
   8:  

Five. While application small example

   1:  /*
   2:  
   3:   Prompts the user to enter a positive integer n, computes the 1+2+3+...+n and
   4:  
   5:  * /
   6:  
   7:  #include <stdio.h>
   8:  
   9:  int main ()
  Ten:  
One   :  {
  :  
//1. Prompt for input   
  :  
  :  printf ("Please enter a positive integer: \ n");
  :  
+   :  //2. Receive input
  :  
+   :  //define variable save user input integer
  :  
  :  int n;
  :  
At   :  scanf ("%d", &n);
  :  
  :  if (n<=0)
  £ º  
  :  {
  :  
  :  printf ("illegal input \ n");
  :  
  :  return 0;
  :  
  :  }
  :  
  :  //3. Calculation
  £ º  
  Panax notoginseng  ://(1 + N) * N/2;
  :  
  :  //define variable saving and
Max   :  
In   :  int sum = 0;
  £ º  
Number   :  int//value added by default
  :  
  :  while (number < n)
  :  
  :  {
  :  
  £  number++;
  :  
  Wuyi:  //cumulative
  :  
  :  }
Wu   :  
  :  printf ("%d\n", sum);
  :  
  £ º  return 0;
  :  
  :  }
  :  

Dark Horse programmer--"Dark Horse video Notes" The use of the C language Foundation while

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.