Basic knowledge of C language-cycle structure and basic knowledge cycle of C Language

Source: Internet
Author: User

Basic knowledge of C language-cycle structure and basic knowledge cycle of C Language

Print 1 ~ with while ~ A multiple of 7 between 100
 

Int I = 1;
When the result of a while loop is true, execute the loop body in braces and repeat until the result of the conditional expression is false to end the loop.
While (I <101 ){
If (I % 7 = 0 ){
Printf ("% d", I );
}
I ++;
} Printf ("\ n ");
Inverted output
Int number = 100;
While (number --){
// If the value is not 0, it is true. If the value is 0, it is false.
If (number % 7 = 0 & number ){
Printf ("% d", number );
}
}



Randomly input n random numbers [30, 70] to obtain the maximum and minimum values.

Int n = 0, number = 0, max = 0, min = 70;
Printf ("Enter n value :");
Scanf ("% d", & n );
While (n ){
To obtain a random number in the specified range [a, B], use the arc4random () function to obtain a random number, and then use the following formula to limit the random number range:
Arc4random () % (B-a + 1) +;
Before using the arc4random () random function, introduce the header file # include
Number = arc4random () % 41 + 30;
Printf ("% d", number );
Max = max> number? Max: number;
Min = min <number? Min: number;
N --;
} Printf ("\ n ");
Printf ("% d \ n", max, min );


Enter an integer on the keyboard to simulate the logon process, and compare it with an existing integer to determine whether the logon result is correct. If the logon result is correct, the logon is successful. If the logon result fails, the logon error is printed, enter again. And limited to three input. Use do-while
Int number = 0, I = 3;
Do {
Printf ("enter the password :");
Scanf ("% d", & number );
If (number == 123 ){
Printf ("Login successful! \ N ");
Break;
} Else {
If (I> 1 ){
Printf ("Login error, please enter again, remaining opportunity % d! \ N ", I-1 );
} Else {
Printf ("your account has been frozen. Please try again in 48 hours! \ N ");
}
}
} While (-- I );


The similarities and differences between the while and do-while Loops
Similarities:
They all have a loop structure that does not know the number of cycles in advance.
Similarities and Differences:
While loop: first judge whether the loop condition is true, and then decide whether to execute the loop body. while loop may not be executed once.
Do-while loop: First executes the loop body and then judges whether the loop condition is still true. do-while loop must be executed at least once.

1. initialize cyclic variables, 2. cyclic conditions, 3. Cyclic increments, 4. Cyclic bodies
The order of for loop execution is 1-> 2-> 4-> 3-> 2-> 4-> 3-> 2-> 4-> 3->... -> 2-> 4-> 3-> 2

For (int I = 1; I <101; I ++ ){
If (I % 7 = 0 ){
Printf ("% d", I );
}
} Printf ("\ n ");
For (int I = 1; I <101; I ++ ){
If (I % 10 = 7 ){
Printf ("% d", I );
}
} Printf ("\ n ");
For (int I = 1; I <101; I ++ ){
If (I/10 = 7 ){
Printf ("% d", I );
}
} Printf ("\ n ");
For (int I = 1; I <101; I ++ ){
If (I % 7! = 0 & I % 10! = 7 & I/10! = 7 ){
Printf ("% d", I );
}
}

Loop nesting, the outer loop is executed once, and the inner loop is executed once
For (int I = 1; I <4; I ++ ){
For (int j = 1; j <4; j ++ ){
Printf ("% d", j );
} Printf ("\ n ");
}

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.