While
1. Execution order
Format:
while (expression)
Statement
2. Comparison with for
For and while can be converted to each other
for (1; 2; 3)
A
Equivalent to
1;
while (2)
{
A
3;
}
While and for can convert each other
But for is more logical and less error prone, it is recommended to use the for
3. For example
Enter a number from the keyboard, return Yes if the number is a palindrome, otherwise return no
Note: palindrome numbers are just writing and writing backwards. For example, 1221, 12321 are palindrome numbers.
1 /*2 April 1, 2015 16:37:443 Purpose: Enter a number from the keyboard, if the number is a palindrome number, return yes, otherwise return no4 5 6 */7 8# include <stdio.h>9 Ten intMainvoid) One { A intVal//Store the number to be judged - intm; - intsum =0; the -printf"Please enter the number you need to determine:"); -scanf"%d", &val); - +m =Val; - while(m)//The value of M is true if nonzero, and the loop body is executed + { Asum = sum*Ten+ m%Ten; atM/=Ten; - } - - if(Sum = =val) -printf"yes!\n"); - Else inprintf"no!\n"); - to return 0; + - } the /* * running the results in VC6.0 is: $ -----------------------------Panax Notoginseng Please enter the number you need to determine: 12321 - yes! the ----------------------------- + Summary: Analysis to enter 1234 for example: A 1> m=1234 Cycle conditions established the sum=0*10+1234%10=4 + M=M/10 = 123 - 2> m=123 Cycle conditions established $ sum=4*10+123%10=43 $ m=123/10=12 - 3> m=12 Cycle conditions established - sum=43*10+12%10=432 the m=12/10=1 - 4> m=1 Cycle conditions establishedWuyi sum=432*10+1%10=4321 the m=1/10=0 - 5> m=0 Cycle conditions are not established Wu - final sum = 4321 About 1234 is not a palindrome number . $ - - - A */View Code
4. When to use while, when to use the for
20.while Cycle