# C language — circular structure 2 (basic part 6)

Time：2021-1-10

I haven’t written a blog for a long time. Recently, the project has been basically completed, and my daily study needs to be put on the agenda. Although there is no hope in the future, I’d better move forward with my head down… Don’t go into the topic.

Soft crash:CPUoccupy100%, dual core50%The machine slowed down significantly. A soft crash is actually a dead loop. aswhile(1);It’s a dead loop sentence. Please don’t use it in heresy.

* hardCrash: no response from mouse, keyboard and monitor.

Most of the dead loops are caused by forgetting to change the value of the loop variable.

evolution

Procedure 1

Use the for statement to enter 1 to 10 on the screen.

``````//For loop statement, output on the screen 1 - 10
#include
main()
{
int i;
for (i = 1;i<=10;i++)
{
printf("%d\t",i);

}
printf("\n");
}``````

For statement

Format:

for(Initial value of cyclic variable;Conditional expression;Cyclic variable increment)

{

Several sentences

…

}

forCycle execution process

For (① n = 0; ② ⑤ n < 100; ④ ⑦ n + +) / / the operation steps of the loop are carried out in sequence according to the numbers in the upper flow

{

③ (6) circulatory body

}

amount to

n=0;

while(n<100)

{

Circulatory body

n++;

}

continueandbreaksentence

continueThis cycle can be ended without jumping out of the cycle,breakStatement can jump out of the loop directly.

program2

usecontinueseek1reach100The sum of even numbers of

``````// 6_ 2_ Write the even sum of 1 - 100 with ccontinue
#include

main()
{
int i, n = 0;
for(i = 0;i<=100;i++)
{
If (I% 2! = 0) // divide by more than two is odd
{
Continue; // jump out of this cycle and proceed to the next cycle
}
else
{
n = n + i;
}
}
printf("%d", n);
printf("\n");
}``````

break statement

``````//Break statement
#include

main()
{
int i;
for (i = 1;i <= 10; i++)
{
Printf (% D-N ", I); // end of 1 2 3 4 5
If (I = = 5) break; // if I = 5, all for loops end
}

}``````

Simple notation program

``````//6_ 2 marking procedure
#include

main()
{
int n, t;
scanf_s("%d", &n);
if (n % 2 == 0)
{
t = 0;
}
else
{
t = 1;
}
if (t = 0)
{
Printf ("even number / N");
}
else
{
Printf ("odd number / N");
}
}``````

Judge whether a number is prime or not;

Let’s say this number is zeronTo judgenWhether or not prime, must usendivide2reachn-1There is any divisionThat is not a prime, all division is a prime.

2reachn-1You can use oneforLoop solution, inforIf in the loopnIf you divide by any number, you immediately jump out of the loop.

Use a variable as a flag in the loop, if it isbreakIf you jump out of the loop, set this flag0If it exits the cycle naturally, the flag is set1。 When the loop ends, the check flag is0The output is “not a prime number”1Just output “yes.”

Simple idea:

definitioni,n,jThree variables

inputn

jSet up1(default is prime)

loopi=2reachn-1

Ifn÷iIt can be divided into two parts

Jump out of the loop

judgejValue of

If it is0Output “not prime”

If it is1The output is “prime”.

The code is as follows:

``````//6_ 3_ Judge whether a number is prime or not
#include
main()
{
int i, n, j = 1;
scanf_s("%d", &n);
for (i = 2; i < n ; i++)
{
if(n%i == 0)
{
j = 0;
break;
}
}

If (J = = 0) printf ("this number is not a prime number;");
Else printf ("this number is a prime number / N");
}``````

law:

In general cyclebreakorContinue statements are executed after judging by if statements.

forChanges in circulation

There can be no expression between two semicolons in the for statement, for example:

for(;i<100;i++){… }It’s a common way of writing.

If it is written as for (;;)It’s a dead cycle.

It means,forAll expressions in the loop can be increased or decreased according to actual needs,Some people like to put it in the circulation body to judge the circulation conditions, and then use itbreakTo jump out of the loop.

1. Seeking code:

``````///6_ 4: Find sigma K, k = 1 to 100 plus sigma K square, k = 1 to 50 plus sigma k-1, k = 1 to 10
#include

main()
{
Float n = 0; // accumulated value
Int k; // counter
for (k = 1;k<=100;k++)
{
n = k + n;
}

for (k = 1; k <= 50; k++)
{
n = n + k *k;
}
for (k = 1; k <= 50; k++)
{
n = n + 1.0 / k;
}
printf("%f\n", n);
}``````