Black Horse Programmer--------------The C-language loop structure

Source: Internet
Author: User

Loop structure While loop

1.while Cycle Basic Use

int main ()
{
int cout = 0;
while (COUNT<=50)
{
++count;
printf ("Do%d push-ups \ n Count");//(Loop body)

}
return 0;
}

When using the while loop, identify the action that needs to be repeated before determining the constraint.

Keywords for 2.while loops

Continue
End the current loop body and go to the next loop body execution

int main ()
{
int cout = 0;
while (COUNT<=50)
{
++count;
if (count%10 ==0);
{
Continue If it is a multiple of 10, then jump out if you re-judge the loop condition
}
printf ("Do%d push-ups \ n Count");//(Loop body)
}
return 0;
}

Break

Directly ends the entire while loop, exiting the entire loop of the keyword

int main ()
{
int cout = 0;
while (COUNT<=50)
{
++count;
printf ("Do%d push-ups \ n Count");
if (count = = 30)
{
Break This means that the loop exits the entire while loop to count== 30.
}
}
return 0;
}

Summarize
While loop operation principle
1. If the initial condition is not established, the loop body will never be executed
2. If the condition is established, a loop body will be executed, the execution is complete, and the condition is determined again ....


Exercise 1:
Prompts the user to enter a positive integer n, computes the 1+2+3+...+n and

#include <stdio.h>

int main ()
{
int n = 0; Be sure to initialize
while (n <= 0)
{
1. Prompt for input
printf (Please enter a positive integer: \ n);
2. Receive input

scanf ("%d", &n);
}



/***************** Method 2**************
int n;

if (n <= 0)
{
printf ("Illegal input!! ");
return 0;
}
int sum = 0

***********************************/

int number = 1//value added by default

3. Calculation
while (number < n)
{
sum + = number;
number++;
}

printf ("%d\n", sum)
return 0;
}

Exercise 2:
Calculates the number of multiples of all 3 within 100

#include <stdio.h>

int main ()
{
Record the number of multiples of 3
int count = 0;

Record the value of the current check
int number = 0;

while (number <100)
{
number++;
Description number is a multiple of 3

if (number%3 = = 0) {
count++;
}
}
printf ("Number of multiples of 3 in 1-100:%d\n, Count")
return 0;
}

While loop usage note

while (10)//10 is true, 1
{
printf ("hahahah\n");
}

The above code causes a dead loop, so pay attention to the condition of the while loop

int a = 10;
while (a>0)
printf ("hahahah\n"); If there is no curly brace after the while, then the first statement after the while is the loop body
{

}

int a = 10;
while (a>0); A dead loop, followed by a semicolon that represents a while loop, the structure is finished, and the back is not the while part

{
printf ("hahahah\n");
}

while (1); The simplest cycle of death


Do and loop

do{

}while (conditions); The Do and loop executes the condition first, regardless of the condition.

In many cases the while and do are interchangeable

For loop

for (statement 1; condition; statement 2)
{
Circulation body;
}

Statement 1: Initializing statements
Statement 2: Increment statement (statement executed after execution of the loop body)

The For Loop executes a statement 1 at the beginning (the entire for loop executes only once)
Determine whether the condition is established, if set up, will execute a loop body, and then will execute statement 2, again determine whether the condition is established

#include <stdio.h>

int main ()

{
for (int count = 0; count <50; count++)
{
printf ("Do%d push-ups \n,count");
}
return 0;
}

The For loop defines the variable in the statement, and the variable is recycled after the statement ends, while the while loop defines the variable outside the statement and does not reclaim the variable in time, so the for loop is higher than the while performance.

For loop use note (detailed in code)

for (int i = 0;i<5;i++); Be careful not to write semicolons behind for ()
{
printf ("haha\n");
}


Here is the wrong wording, for the definition of the variable to add {}, otherwise the scope of the variable is ambiguous, the compiler error
for (int i = 0;i<5;i++);
int a = 10;

for (int i = 0;i<5;i++,a++);
{
int a = 10; This definition is wrong because the variable a was destroyed before the a++ was executed.
int i = 10; This definition is not error, because the scope of I defined in Statement 1 already includes the i++ in statement 2
}
The loop body is automatically destroyed each time it finishes executing.

for (;;);//For loop the simplest dead loop


For loop nesting (when used as output QQ friends list)

#intclude <stdio.h>
int main ()
{
for (int i=3;i<=3;i++)
{
printf ("Buddy list%d\n", i);
for (Int J =1;j<=6; J + +)
{

printf ("Friend%d\n", J);
}
}
return 0;
}

The above code will output I friends list, each list has a J friend

For loop nesting exercises

Prompts the user to enter a positive integer n, if n=5, outputs the following graph, other n values, and so on
*****
****
***
**
*


#include <stdio.h>
int main ()
{
/prompt a variable to store the value entered by the user
int n = 0;

Judging the value of n is not reasonable
while (n <=0)
{
Prompt user to enter a positive integer
printf ("Please enter a positive integer: \ n");
scanf (%d,&a);
}
Output graphics

for (int i=0; i<=n;i++)//How many rows
{
for (int j=0;j<=n-i;j++)
{
printf ("*");
}
pintf ("\ n");
}

return 0;
}

Additional Knowledge supplement

Break and Continue use note

Break Use occasions
1.switch statement, exit the entire switch statement
2. Loop structure: Exit the entire loop structure
While
Do While
For

Continue use occasions
Loop structure: End the current loop body, enter the next loop body
While
Do While
For

Break and while are only valid for the most advanced loop body

Black Horse Programmer--------------The C-language loop structure

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.