Home > Common Problem > body text

What does while mean in C language?

清浅
Release: 2020-07-10 17:22:05
Original
55261 people have browsed it

The while in C language is a basic loop mode in computers. It means that when the expression is true, the prediction is executed, and when the expression is false, the loop is jumped out.

What does while mean in C language?

#The while in C language is a loop statement and a basic loop mode of the computer. Enter the loop when the conditions are met, and jump out if the conditions are not met

The execution sequence of while

The execution sequence of while loop is very simple, its format is:

while (表达式)
{
        语句;
}
Copy after login

When the expression is true, execute the following statement; after the statement is executed, judge whether the expression is true. If it is true, execute the following statement again; then judge whether the expression is true... and so on. Continue until the expression is false and break out of the loop. This is the execution sequence of while.

Write a program below to implement this function:

Calculate the value of (1-1/3 1/5-1/7 1/9-1/11…)*4.

# include <stdio.h>
int main(void)
{
    int i = 1;
    int j = 1;
    double sum = 0;  //结果肯定是小数, 所以要定义成double或float型
    while (1.0/i > 1e-6)  /*当1/i小于10的-6次方时停止循环。这个循环条件是自己定的, 定得越小最后的结果就越精确。
    注意1一定要写成小数的形式即1.0*/
    {
        sum += (1.0 / i) * j;
        i+=2;
        j = -j;  //实现正负交替
    }
    sum *=4;
    printf("sum = %lf\n", sum);  //double是%lf, 取6位小数是%.6
    return 0;
}
Copy after login

The output result is:
sum = 3.141591

The above is the detailed content of What does while mean in C language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!