2. Function:
for (statement 1; condition; statement 2)
{
Loop body
}
Statement 1: Initializing statements
Statement 2: Increment statement (statement executed after execution of the loop body)
1.for executes statement 1 at the beginning (the entire for loop executes only once)
2. Determine whether the condition is established, if the condition is established, will be executed once the loop body, then will execute the statement 2, again determine whether the condition is established
2. Note
don't write semicolons behind for ()
for (int i=0; i<5; i++);
{
printf ("haha \ n");
}
Error: The scope of variable A is ambiguous
If you want to define a new variable in the loop body, you must enclose it in curly 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 the 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", a);
}
The simplest use of a for loop to implement a dead loop
for (;;);
3. Loop Nesting Exercises
1 /*2 Friends List 13 Friends 14 Friends 25 Friends List 26 Friends 17 Friends 28 Friends List 39 Friends 1Ten Friends 2 One */ A -#include <stdio.h> - intMain () the { - - for(inti =1; i<=4; i++) - { + //printf ("Buddy list%d\n", i + 1); -printf"Friends List%d\n", i); + A /* at printf ("Friend 1\n"); - printf ("Friend 2\n"); - printf ("Friend 3\n"); - printf ("Friend 4\n"); - printf ("Friend 5\n");*/ - in for(intj =1; j<=7; J + +) - { toprintf"friend%d\n", j); + } - } the * return 0; $
1 /*2 prompts the user to enter a positive integer n, if n=5, outputs the following graph, other n values , and so on3 *****4 ****5 ***6 **7 *8 */9 Ten#include <stdio.h> One A intMain () - { - //1. Define a variable to store values entered by the user the intn =0;//Be sure to initialize - - //2. Determine the value of n is not a logical - while(N <=0) + { - //2.1 Prompting the user to enter a positive integer +printf"Please enter a positive integer: \ n"); A at //2.2 Receiving the input data -scanf"%d", &n); - } - - //3. Output Graphics - for(inti =0; i<n; i++)//How many lines are there? in { - //printf ("*****\n"); to for(intj =0; j<n-i; J + +) +{//How many of them are there in each row -printf"*"); the } * $printf"\ n");Panax Notoginseng } - the return 0; +}
"Learning note", "C language" cyclic structure-for