C ++ cyclic statement instances

Source: Internet
Author: User

C ++ cyclic statement instances
Goto redirection statement

LabelIt is added by a name with a colon":. This label is usually placed on the left side of an executable c ++ statement and jumps through the goto statement following it.

A goto is a turning statement that jumps to a label unconditionally.

As the goto statement can directly jump to a certain place of the program to start execution, it will lead to many imperceptible errors and make the program structure look less rigorous and loose.

Therefore, do not use the goto statement again. Once an error occurs in the goto statement, it is very difficult to debug it.

Example:

# Include
 
  
Int main () {int I = 1; number: ++ I; // nubmer is the std: cout <"*"; if (I <10) {// 9 cycles, so 9 "*" goto number is output; // jump to the number position unconditionally} std: cout <"\ n program ends! \ N "; std: cout <" ********** \ n "; return 0 ;}
 

Output:

* ******** Program ended! *********
While loop statement

As long as the condition following the while statement is true, a piece of code is continuously executed. The while statement is executed only when the conditions are met, and does not jump out of the loop until the conditions are not met.

Example:

# Include
 
  
Int main () {int I = 1; while (I <10) {std: cout <"*"; ++ I;} std :: cout <"\ n program ended \ n"; std: cout <"********** \ n"; return 0 ;}
 

Output:

* ******** Program end *********
A while LOOP statement with character Conditions

Characters can also be used as conditions for while LOOP statements.

Example:

# Include
 
  
Int main () {char choice = 'y'; while (choice = 'y' | choice = 'y') {std :: cout <"how worried can a gentleman be? Yi is like a river spring water flowing eastward \ n"; std: cout <"again? (Y/N): "; std: cin> choice;} std: cout <" program ended! \ N "; return 0 ;}
 

Output:Enter y

Q: How much can I worry about it? Can I see it again when the spring water goes east? (Y/N? (Y/N ):

Output: Input n

Q: How much can I worry about it? Can I see it again when the spring water goes east? (Y/N): n program ended!
A while LOOP statement with a limited number of times
# Include
 
  
Int main () {int count = 0; // count int counts; // number of cycles std: cout <"How many times do you want to see it? "; Std: cin >>> wait; while (count <strong) {std: cout <" how worried can I be, yi Jiang spring water to the east flow \ n "; + + count; // count} std: cout <"program ended! \ N "; return 0 ;}
 

Output:Input 4

How many times do you want to see it? 4. I asked how worried I could be, and how worried I could be when I was in the spring water of yijiang, and how worried I was when I was in the spring water of yijiang, yi Lijiang spring water to the east to ask how worried you can have, Yi Lijiang spring water to the east flow program ended!

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.