Usage of the while/do while statement in C Language

Source: Internet
Author: User

In C language, do while and while are the same as those in vb and asp.net. Next I will introduce the usage of while/do while statements in C language.

Usage of the while statement in C Language


The while statement is generally in the form of a while (expression) statement.
The expression is a loop condition, and the statement is a loop body.

The syntax of the while statement is to calculate the value of the expression. When the value is true (not 0), the loop body statement is executed. The execution process can be expressed.

[Example 6.2] use the while statement to calculate the value from 1 to 100. Representation algorithm using traditional flow chart and N-S structure flow chart

The Code is as follows:

Main ()
{
Int I, sum = 0;
I = 1;
While (I <= 100)
{
Sum = sum + I;
I ++;
}
Printf ("% dn", sum );
}


[Example 6.3] count the number of characters entered in one line from the keyboard.

The Code is as follows:
# Include <stdio. h>
Main (){
Int n = 0;
Printf ("input a string: n ");
While (getchar ()! = 'N') n ++;
Printf ("% d", n );
}

In this example, the cyclic condition is getchar ()! = 'N', meaning that as long as the character entered from the keyboard is not a carriage return, the loop continues. The number of input characters in the loop body n ++ is counted. In this way, the program counts the number of characters in one line.

Pay attention to the following points when using the while statement:

The expressions in the while statement are generally relational expressions or logical expressions. As long as the expression value is true (not 0), the loop can continue.
[Example 6.4]

The Code is as follows:
Main (){
Int a = 0, n;
Printf ("n input n :");
Scanf ("% d", & n );
While (n --)
Printf ("% d", a ++ * 2 );
}

In this example, n cycles are executed. Each execution is performed, and the n value is reduced by 1. The value of the loop body output expression a ++ * 2. This expression is equivalent to (a * 2; a ++ ).

If the loop body contains more than one statement, it must be included in {} to form a composite statement.


C do-while statement

The general form of the do-while statement is:
Do
Statement
While (expression );

The difference between this loop and the while loop is that it first executes the statements in the loop and then judges whether the expression is true. If it is true, the loop continues. If it is false, the loop ends. Therefore, the do-while loop must be executed at least once. The execution process can be expressed.

[Example 6.5] use the do-while statement to calculate the value from 1 to 100.

The Code is as follows:
Main ()
{
Int I, sum = 0;
I = 1;
Do
{
Sum = sum + I;
I ++;
}
While (I <= 100)
Printf ("% dn", sum );
}

Similarly, when many statements are involved in a loop, use "{" and "}" to enclose them.

[Example 6.6] compare the while and do-while loops.

(1) main ()

The Code is as follows:
{Int sum = 0, I;
Scanf ("% d", & I );
While (I <= 10)
{Sum = sum + I;
I ++;
}
Printf ("sum = % d", sum );
}

(2) main ()

The Code is as follows:

{Int sum = 0, I;
Scanf ("% d", & I );
Do
{Sum = sum + I;
I ++;
}
While (I <= 10 );
Printf ("sum = % d", sum );
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.