Branch statement blog: http://t.csdn.cn/U2kZF
Table of contents
Preface: Let's first understand the role of break and continue in the loop
For loop omission error example:
Use do while loop to print 1~10
4.1 Calculating the factorial of n
5. Implement binary (half) search algorithm
5.2 Binary search code implementation
Preface: Let's first understand the role of break and continue in the loop
break: terminate the loop (violence)
continue: Skip the code behind this cycle continue, recycle (gentle)
1. while loop
When the condition is met, the statement after the if statement is executed, otherwise it is not executed. But this statement will only be executed once. Because we find that many practical examples in life are: we need to do the same thing many times. So how do we do it? The C language introduces us: while statement, which can realize loop.
break in while loop
When n=5, the if statement is true, and the break statement is executed. Break jumps out of the whole loop in the while loop. If there are multiple nested loops, it jumps out of the inner loop (jumps out of a set of loops)
continue in while loop
When n=5, execute the continue statement, skip the code after continue, and start the next cycle
2. for loop
Expression 1 is the initialization part, which is used to initialize the loop variable.
Expression 2 is the condition judgment part, which is used to judge the termination of the loop.
Expression 3 is an adjustment part, which is used to adjust the loop condition.
For loop omission error example:
Here are two nested for loops. At the beginning i=0, j=0, enter the inner for loop, loop 4 times out of the inner loop, this is j=4, and then enter the inner for loop again when i=1 At the time, this is because j has no initialization part, so j is still 4, directly jump out of the inner for loop ~~~ originally printed 16 hehe, now only prints 4
break in for loop
When i=5, execute the break code and jump out of the for loop directly
continue in a for loop
When i=5, execute the continue code— jump out of the code behind the continue of this loop, go directly to the adjustment part -> then to the for loop judgment part
for loop usage suggestion
1. Do not modify the loop variable in the body of the for loop to prevent the for loop from getting out of control.
2. It is suggested that the value of the loop control variable of the for statement should be written in the form of "close before and then open".
3. do while loop
The loop is executed at least once, and the usage scenarios are limited, so it is not often used.
、
Use do while loop to print 1~10
break in do while loop
break directly out of the loop
continue in do while loop
continue skips the code behind this loop . Since the adjustment part is skipped here, i=5 is always true, and continue is executed all the time, and 1234 is printed out. Infinite loop
4. Practice
4.1 Calculating the factorial of n
int main()
{
int n = 0;
scanf("%d", &n);
int ret = 1; //注意ret不能定义为0
for (int i = 1; i <=n ; i++)
{
ret *= i;
}
printf("%d", ret);
return 0;
}
4.2 Calculate 1 !+2!+3!+…+10!
int main()
{
int ret = 1;
int sum = 0;
for (int i = 0; i <=10 ; i++)
{
ret *= i;
sum += ret;//sum=sum + ret;也可以这样来看
}
printf("%d", sum);
return 0;
}
5. Implement binary (half) search algorithm
The premise of the binary search algorithm is that the array elements are ordered (ascending), (descending)
5.1 Thought analysis
1. Determine the left and right subscripts left
and right of the searched range 2. Determine the middle element subscript mid according
to left and right 3. Compare the element locked by mid with the element to be searched to determine the new element range left and right
5.2 Binary search code implementation
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };//升序
//printf("%d\n", sizeof(arr));//计算的数组的总大小,单位是字节
//printf("%d\n", sizeof(arr[0]));//4
//printf("%d\n", sizeof(arr) / sizeof(arr[0]));
int k = 7;
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
//1
int left = 0;
int right = sz - 1;
int flag = 0;//flag的作用是标志是否找到了
//2
while (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid] == k)
{
printf("找到了,下标是:%d\n", mid);
flag = 1;
break;
}
else if (arr[mid] < k)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
//1 2
if (flag == 0)
printf("没找到\n");
return 0;
}
operation result
If you think the article is good, I look forward to your one-click triple link. Your encouragement is the source of motivation for my creation. Let us work together and see you at the top! ! !