[Study Notes] [C Language] cyclic structure-for, learning note structure-

Source: Internet
Author: User

[Study Notes] [C Language] cyclic structure-for, learning note structure-
1. Use:

For (Statement 1; condition; Statement 2)
{
Loop body
}

Statement 1: initialization statement
Statement 2: Incremental statement (statement executed after the loop body is executed)

1. The for statement is executed once at the beginning. 1 (the entire for loop is executed only once)
2. Determine whether the condition is true. If the condition is true, a loop body is executed, Statement 2 is executed, and the condition is true again.

2. Note

Do not enter a semicolon after ().
For (int I = 0; I <5; I ++ );
{
Printf ("Haha \ n ");
}

Error: the scope of variable a is not clear.
To define a new variable in the loop body, you must wrap it in braces {}.
For (int I = 0; I <5; I ++)

Int a = 10;


Error
For (int I = 0; I <10; I ++, a ++)
{
// A can only be used in loop body {}
Int a = 10;
}

Int a = 10;

For (int I = 0, a = 9; I <5; I ++)
{
// Int I = 10;
Int a = 11;

Printf ("a = % d \ n", );
}

The simplest use of the for loop to achieve an endless loop
For (;;);

 

3. nested loop exercises
1/* 2 friend list 1 3 friend 1 4 friend 2 5 friend list 2 6 friend 1 7 friend 2 8 friend list 3 9 friend 110 friend 211 */12 13 # include <stdio. h> 14 int main () 15 {16 17 for (int I = 1; I <= 4; I ++) 18 {19 // printf ("friend list % d \ n", I + 1); 20 printf ("friend list % d \ n", I ); 21 22/* 23 printf ("friend 1 \ n"); 24 printf ("friend 2 \ n"); 25 printf ("friend 3 \ n "); 26 printf ("friend 4 \ n"); 27 printf ("friend 5 \ n"); */28 29 for (int j = 1; j <= 7; j ++) 30 {31 printf ("friend % d \ n", j); 32} 33} 34 35 return 0; 36
1/* 2 prompt the user to enter a positive integer n. If n = 5, the following figure is output, other n values and so on 3 ******* 4 ****** 5 ***** 6 **** 7*8 */9 10 # include <stdio. h> 11 12 int main () 13 {14 // 1. define a variable to store the user input value 15 int n = 0; // You must initialize 16 17 // 2. judging n value combination unreasonable 18 while (n <= 0) 19 {20 // 2.1 prompt the user to enter a positive integer 21 printf ("Please enter a positive integer: \ n "); 22 23 // 2.2 receive input data 24 scanf ("% d", & n); 25} 26 27 // 3. output graph 28 for (int I = 0; I <n; I ++) // number of rows: 29 {30 // printf ("***** \ n"); 31 for (int j = 0; j <n-I; j ++) 32 {// The number of 33 printf ("*"); 34} 35 36 printf ("\ n"); 37} 38 39 return 0; 40}

 

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.