C--(seven) while loop

Source: Internet
Author: User
Tags ticket

Knowledge points
while cycle
do. While Loop statement
while looping and character processing
• Circular use issues to be aware of

=============================================

As you know, the while and for loops have a similar function

1. Think: Why is there a For loop that also requires a while loop
Applications for 2.while Cycles
3.while Cycle Structure
4.while Loop Run logic
5. Example:
1) How to output 10 through the while loop *

while (conditional expression)


2) Practice:
Output [1-20]
3) Practice:
Calculates the 1-10 and
Calculate 1-10 Product

6.while Loop and for loop comparison
While: Indeterminate number of loops
For: Determines the number of loops

for (expression 1; expression 2; expression 3)

Loop statement;

Expression 1;
while (expression 2)
{
Loop statement;
Expression 3;
}

=============================================
Do.. While Loop statement
Do
{
Loop statement;
}while (cyclic conditions);

1. Thinking: Why this cycle is needed
Solution: Get in the car ticket problem, first get on the ticket and check in before boarding
2.do.. While loop structure
Guarantee that the loop will be executed at least once

3. Cyclic running logic
4. Example:

572


570 573

Output 1-1000
Practice:
Guess number Game
1. Give a number
2. User enters a number
3. Determine if the user input number and num are consistent
4. End
5. If errors, repeat steps 2 and 3
============================================
Issues needing attention in recycling
1. Loop variable definition position
for (int i = 0; i <; ++i)
//
int i;
for (i = 0; i <; ++i)
2. Various dead loops
for (i = 0;i < i--)
for (i = 0;; i++)
for (i = 0;i < 10;i++)

{
printf ("*");
}


for (;;)
{

}
while (1)
{

}
3. The value of the loop variable at the end of the loop
4.for after Loop plus;
=============================================
While Loop and character handling
Requirements: How to continuously receive user input until the input Q
scanf ("%c", &ch);

1. When to end the cycle
When the user enters the Q
2. How to iterate over the user's input

3. Practice:
Count the number of numeric characters entered by the user
4. Question:
Enter n characters to calculate the number of input characters
5. Example
Count the number of uppercase letters
Practice:
Count the case in user input, number of numbers

=============================================
Cyclic common programming
1. Calculate the M-Time of N
2. Calculate the factorial of n number
3. Calculate the number of digits of any number 9999 < < 65535

Cycle start: num
Loop end: num = = 0
Loop increment: num/= 10

1/10== 0-1
12/10 = = 1-2
123/10 = = 12-3
1234/10 = = 123-4

Exercise: Generate a random number, take out the data on each bit and get the number of bits information


============================================
Three types of structural differences and linkages:
1. Contact:
For (cyclic initial condition expression; cyclic conditional expression; loop increment expression)

Loop statement;

A while loop structure that can overwrite the same functionality

Cyclic initial condition expression;
while (loop condition expression)

Loop statement;

cyclic increment expression;

2. Difference:
For loop number determination
While loop count is indeterminate
Do...while cycle times are indeterminate, ensuring that the cycle is executed at least once

C--(seven) while loop

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.