cyclic statement
Loop statements can execute the same set of statements many times over, and the for keyword can be used to write loops; you can have a variable represent a set of numbers sequentially in a for loop, and then use the same set of statements to process each of the numbers represented by the variable. This variable is called a loop variable, and the following format is used to describe the process of changing the loop variable, which calculates each number from the first one in a uniform pattern and lets the loop variable represent it. (assignment of the starting number to the loop variable; logical expressions describing the relationship between the loop variable and the ending number; rules for calculating the next number based on the previous number)
for( ; ;){
}
If the solution to a problem involves a process of changing a set of numbers, and each number is handled in a similar way you can use loops to solve the problem.
Exercise: write a program to display the following on the screen; display one line at a time, for loop implementation
1 x 9 =9
2 x 8 =16
3 x 7=21
4 x 6 =24
5 x 5 =25
Click to view code
/*
*
* First one line at a time, with the following displayed on the screen
*
*1x9=9
*2x8=16
*3x7=21
*4x6=24
*5x5=25
*
*
* */
#include<>
int main(){
int num1 = 0,tmp =10.
for(num1 = 1;num1 < 6;num1 ++){
printf("%d x %d = %d\n ",num1,(tmp-num1),num1 * (tmp-num1));
}
return 0.
}
Exercise: write a program to get two integers from the keyboard; calculate the sum of all the odd numbers between them and display it on the screen
Click to view code
/*
*
* Find all odd sums between two integers to display on the screen
*
* */
#include<>
int main(){
int max = 0,min = 0,tmp = 0,num = 0,sum = 0;
printf("Please enter two digits \n");
scanf("%d%d",&min,&max);
if (min >= max){
tmp = min; min = max; if (min >= max){
tmp = min; min = max; max = tmp; if (min >= max){
max = tmp; }
}
for (num = min;num <= max;num ++){
if (num & 1){
sum += num; }
}
}
printf("Summing result %d\n",sum); }
return 0; }
}
Suppose there is the following for loop
for(1;2;3){
4
}
He executes in groups as follows: [1,2] [4,3,2] [4,3,2] ......[4,3,2]. The first group is different from all the others in that normally the for loop must end between two groups; if the logical expression numbered 2 in the last group of a particular group turns out to be true it starts the next group, otherwise it ends the loop.If the for loop ends normally, the loop variable falls outside the specified range of numbers after the loop ends.If the logic expression in the middle of the parentheses of the for loop is omitted, it means that the logic expression will always be true, and this kind of loop can't be ended normally, so they are called dead loops.
for(;;)
C99 specification allows temporary declaration of loop variables in the parentheses of a for loop, which can only be used in the loop, and cannot be used after the loop ends (unsupported use).
Exercise: Chickens and rabbits in the same cage; 40 heads, 100 feet. Write a program to calculate how many rabbits and chickens each
Click to view code
/*
*Chickens and rabbits in the same cage
*Head = 40
*Feet = 100
*♪ How many of each?
*
* */
#include<>
int main (){
int num = 0; for (num = 0; num <=40;num ++){
for (num = 0; num <=40;num ++){
if (4 * num + 2 * (40-num) == 100){
printf("There are %d rabbits and %d\n chickens",num ,40-num);
break;
}
}
printf("num = %d\n",num); }
} printf("num = %d\n",num); return 0; }
}
break statement
You can use the break; statement in a loop to immediately end the execution of the loop, once the break statement is executed, all the statements in the loop are no longer executed, including the loop variable is no longer changing, if the loop uses the break; statement to end the end of the loop after the loop variable must be in the specified range of numbers, if you write a loop do not know how many times the loop to repeat the execution of the loop you can write a dead loop and end it with the break; statement. If you don't know how many times the loop will be repeated when you write it, you can write a dead loop and end it with the break; statement.
continue statement
You can use continue inside the curly braces of a loop; the statement jumps directly to the end of the curly braces, and all statements in between are not executed in this loop.
random number
Unpredictable numbers are called random numbers; rand standard function can be used to obtain a random number, in order to use this standard function needs to include this header file, srand standard function is used to set the random number seed, this function requires an integer to be used as a seed, different seeds to get a different random number, in order to use this standard function needs to include the header file, any program only needs to set once! random number seed, time standard function can be used to obtain the current time, this function uses an integer to represent the time obtained; the same second within the time obtained on behalf of the integer will not change. In order to use this standard function you need to include the header file.
Exercise: writing a guessing game
Click to view code
/*
*
*
*guessing game
*
*
*
* */
#include<>
#include<>
#include<>
int main (){
int num = 0,guess=0;
srand(time(0));
num = rand() % 100;
while (1){
printf("Please enter a guess number\n");
scanf("%d",&guess);
if (guess > num){
printf("big\n");
}
else if (guess < num){
printf("small\n");
}
else {
printf("Oh, that's right, ... (when one suddenly remembers sth one wanted mention)\n");
break;
}
}
return 0;
}
The program first obtains a random number between 0 and 99, and then allows the user to guess the number, each time to give the appropriate prompts after guessing (guessing big, guessing small, guessing right) until the end of the program to guess the right. Guessed small, guessed the right), until the end of the program to guess the right
Branches and loops are flow control statements; they allow statements in a program to be executed out of top-to-bottom order.
goto statement
The goto statement is also a flow control statement that specifies any statement in the program as the next statement; try not to use the goto statement.
Exercise: Display the following five lines on the screen
54321
5432
543
54
5
Only one digit can be displayed at a time
Click to view code
/*
*
*54321
*5432
*543
*54
*5
* Display one digit at a time
*
* */
#include<>
int main (){
int num = 0,num1 = 0; for (num = 1;num <= 5;num ++){
for (num = 1;num <= 5;num ++){
for (num1 = 5;num1 >= num;num1 --){
printf("%d",num1);
}
printf("\n");
}
return 0; }
}
multiple loop
A problem can be solved using multiple loops if the solution involves multiple sets of numbers that are related to each other, with the loop variable in the outer loop representing the slower changing numbers and the loop variable in the inner loop representing the faster changing numbers.
Exercise: Suppose a currency contains three denominations, $1, $2, and $5.
Write a program to find all possible combinations of $10 and display them all on the screen
Click to view code
/*
*
* Currency combination display
*
*
* */
#include<>
int main (){
int num = 0, num1 = 0; for (num = 0;num <= 2;num ++){
for (num = 0;num <= 2;num ++){
for (num1 = 0;num1 <= 5;num1 ++){
if (5 * num + 2 * num1 <= 10){
printf("There are %d sheets for $5, %d sheets for $2, %d sheets for $1 \n",num,num1,10 - 5 * num - 2 * num1);
}
}
}
return 0; }
}
while keyword
The while keyword can also be used to write loops, not easy to use the for keyword to achieve the loop can be considered to use the while keyword to achieve, while the loop format is as follows
while (logical expression) {
Repeatedly executed statements
}
This structure means that the statement inside the curly braces is executed repeatedly until the result of the logical expression is false, if the logical expression in the while loop is always true, it becomes a dead loop, the while loop can also use the break; and continue; statements; the while loop alternates between calculating the logical expression and the statement inside the curly braces, the first step of the while loop is to The first step of the while loop is to compute the result of the logical expression. If the logical expression is false, the while loop is terminated and the statement inside the curly braces may not be executed.
do...while
do... .while format can also be used to write loops, do... . while loop format is as follows
do {
Repeatedly executed statements
} while (logical expression)
do... . while loop must be terminated by a semicolon after the parentheses. do... The do...while loop also alternates between the computation of the logical expression and the statement inside the curly braces, the do... The do...while loop executes the statement inside the curly braces first, and the do... . while loop ensures that the statement inside the curly braces is executed at least once.
Exercise: write a program to get two non-negative integers from the keyboard; calculate their greatest common divisor and display the result on the screen.