C + + Language Basics-Other statements

Source: Internet
Author: User
Tags bool continue execution expression goto

A goto statement can transfer a program to a label at the front with a label and a colon declaration.
The following code demonstrates this statement:
bool done = false;
StartPoint:
Do some stuff
if (!done) goto (startpoint);/loop over, moving on ...
Curly braces are not required here because all the code between the Goto statement and the label is executed.
A goto statement is considered a bad statement in a C + + program. Any work that can be done with a goto statement can be performed using a while and a dowhile loop. A good C + + programmer rarely uses a goto statement in a program. If you turn to C + + from another language, you will find that the basic structure of C + + makes the goto statement redundant.

There are two keywords in the loop that must be introduced, which is to control the continue and break of the program execution in the loop. The Continue statement forces the program to go to the bottom of the loop, skipping any statements after the continue statement. For example, when a test is true, some part of the loop may not need to be executed. You can use the Continue statement to skip any statements after the continue statement:
bool done = false;
while (!done) {
Some Codebool error =somefunction ();
if (error) continue;
Jumps to the top of the loop
Other code that would execute only if no error occurred
}

The

Break statement is used to terminate the loop execution before the Loop normal test condition conforms. For example, you can search for an element in the INTs array, find the number, and then terminate the loop execution to get the index where the number is located:
int index=1
int searchnumber=50;
for (int i=0;i<numelements;i++) {
if (myarray[i]==searchnumber) {
Index=i;break;}
}
if (index!=1)
cout << "number found at index" << index << end1;
Else
cout << "number not found in array." << end1; The
continue and break statements are useful in many cases. As with other knowledge to be introduced, continue and break statements are also constantly familiar in practice.


A switch statement is an advanced if statement that performs one of several code segments based on the result of an expression. An expression can be a variable, a function call result, or another valid C + + expression. Here's a switch statement example: switch (amountoverspeedlimit) {
Case 0:
{fine = 0;
Break
}
Case 10:
{fine = 20;
Break
}
Case 15:
{fine = 20;
Break
}
Case 20:
Case 25:
Case 30:

Fine=amountoverspeedlimit * 10;
Break
}
Default:
{Fine =gotocourt ();
Jailtime=getsentence ();
}
}
The switch statement is divided into several parts. First there is an expression, in this case, the amountoverspeedlimit variable (long enough variable name!), and then the expression is tested with the cases statement, and if Amountoverspeedlimit equals 0 (case 0:), assign a value of 0 to the variable fine, If the amountoverspeedlimit equals 10, assign a value of 20 to the variable fine, and so on. There are break statements in the first three case. The break statement is used to turn out the switch block, where an expression is found, and the remainder of the switch statement can be ignored. Finally, there is a default statement, and if the expression is not matched, the program executes the default statement.

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.