Loop Statement 6

Source: Internet
Author: User

Loop statement
C language while do-while for GOTO (unconditional loop)
1. For Loop
For (expression 1; expression 2; expression 3) {statement };
// The condition can be omitted; the two semicolons cannot be omitted.
// If no condition exists, it indicates an infinite loop.
Expression 1: indicates the initialization of the loop condition.
Expression 2: the condition that controls the loop.
Expression 3: Change the cyclic variable. Tend to end
Exercise: print numbers between 1 and 10
For statement execution process:
Expression 1 assigns a value to the cycle factor when the first cycle enters,
Execute only once. Expression 1 is executed, and expression 2 is judged.
True or not. If it is true, it enters the loop body and the execution ends.
Expression 3, and then execute expression 2 to determine whether it is true. If it is true, it enters the loop body,
Execute expressions 3 and 2 repeatedly until expression 2 is false and the loop ends.
Common for loop types:
For (I = 0; I <n; I ++ ){}
For (I = 0; I <= N; I ++ ){}
For (I = N; I> 0; I --){}
For (I = n-1; I> = 0; I --){}
Exercise: print an odd number from one to one hundred
Modify the first line
200 lines of code per day
<3> Print the wildcard Formula 1*2-1 or 1*2 + 1


Exercise: print one to ten square tables
11
24
39
416
CTRL + C command to end the endless loop and exit


A comma expression is used to calculate the value of each expression at a time. If a value is assigned
Value assigned to variable
Exercise: 3. Calculate the total score and average score of students;
The number of students is required to be customized by the user.
Tip: first, let the user enter the number of students
(Int count = 0)
Read the scores of each student cyclically and calculate the total scores.
For (; I <count; I ++)
Sum/count

For (I = 1; I <10; I ++ ){
For (j = 1; j <= I; j ++)
{
}
}


Functions of nested loops:
Use the outer loop variable to control the number of inner loops;
Exercise: print the 9-9 multiplication table

 

5. The break keyword jumps out or ends.
Enter the year, month, and day to calculate the day of the year.
Goto
Break: Jump out or loop (branch switch)
Continue: continue. Terminate this cycle and enter the next cycle


Print the number between 1 and 30 that cannot be divided by four


While loop do while: WHILE LOOP
While (expression condition) {statement ;}

While (9); is equivalent to for (;) an endless loop.
Do {statement block, multiple statements} while (); Do not forget the last semicolon

Exercise: complete the days. C code with verification
2. Enter five Integers to find the largest

3. Use do while to determine the number of digits in an integer
4. Question: enter an integer to print the binary

 

2. Enter five numbers
Scanf
A> B
A> C
A <d

Every time I have to review, I have to study for 1 hour after the teacher finishes the course,
Consolidate first

 

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.