C + + language base-if statement

Source: Internet
Author: User
Tags bool expression

The IF statement is used to test the condition and execute a table or several statements when the condition is true.
Note: If expression cannot be followed by a semicolon, it itself represents an empty statement in the code, causing the compiler to interpret an empty statement as a statement that executes when the condition is true.
if (x = = 10); warning! Extra semicolon!
DoSomething (x);
Here the DoSomething () function is always executed because the compiler does not consider it to be the first statement executed when the condition is true. Because this code is completely legal (but useless), the compiler cannot issue a warning.

Suppose you want to execute multiple lines of statements when the condition is true, and enclose the statements in curly braces:
if (x > 10) {
cout << "The number is greater than" << end1;
Dosomethingwithnumber (x);
}
When the conditional expression evaluates to False, the code segment associated with the IF statement is ignored, and the program continues to execute the first statement after the code fragment.
Description
C + + contains a number of shortcut methods, one of which is to test true with the variable name, for example:
if (Filegood) ReadData ();
This method is the sketch method for the following statements:
if (Filegood = = True) ReadData ();
This example uses a bool variable or other data type. As long as the variable contains a value other than 0, the expression evaluates to True and the variable name is logically non (!). The operator can test the value of false:
BOOL Filegood = Opensomefile ();
if (!filegood) ReportError ();
Learning C + + shortcuts helps to write more exciting code. Sometimes to do an action when the conditional expression evaluates to true, and another action when the conditional expression evaluates to False, you can use the Else statement as follows:
if (x = =) {dosomething (x);}
else {doadifferentthing (x);}

New terminology
The Else statement and the IF statement are used together to indicate the code segment that is executed when the IF statement fails (that is, when the conditional expression evaluates to false).

The second form of an if statement:
if (cond_expr_1) {
True_statements_1;
}
else if (cond_expr_2)
{true_statements_2;}
else {
false_statements;
}
If the conditional expression cond_expr is 1 true (not 0), the TRUE_STATEMENTS1 code segment is executed, and if the conditional expression cond_expr 1 is false and if the conditional expression Cond_expr 2 true (not 0), execute the True_statements 2 yards; If two expressions are false, execute false_statements code segment.

--------------------------------
Using loops
Loops are a common element of all programming languages. Loops can be used to repeat an array, repeat a specified number of times for an action, read a file from a disk, and so on.
The loop has: For loop, while loop, and do While loop.
These loops are basically the same, and all loops have the following common elements:
· Starting point
· A loop body, usually enclosed in curly braces, containing the statements to be executed each time the loop
· End
· Determine the test condition for the loop termination
· Optional use of break and continue statements

The For loop is the most commonly used loop, taking three parameters: the starting number, the test condition, and the increment expression.
For Loop statement:
for (initial; cond_expr;adjust)
{statements;
}
The For loop repeats the statements code segment until the conditional expression cond_expr is not true. The loop state is initialized by the initial statement, and the state is modified with the Adjust statement after the statements code segment is executed. A typical example of a for loop is described below:
for (int i=0;i<10;i++) {
cout << "This is iteration" << i << end1;
}

The while loop differs from the for loop in that it has only one test condition that is checked at the beginning of each loop. The loop continues to run as long as the test condition is true.
int x;
while (x < 1000) {
x = Dosomecalculation ();
}
In this case I call a function, assuming that it will eventually return a value greater than or equal to 1000. As long as the return value of this function is less than the 1000,while loop, continue to run. When the variable x contains a value greater than or equal to 1000, the test condition turns false, and the program goes into the first statement after the closing parenthesis of the while loop. The while loop is usually tested with a bool variable. The test variable state can be set in the loop body:
bool done = false;
while (!done) {
Some code here
Done = Somefunctionreturningabool ();
More code
}

The Do While loop is essentially the same as the while loop, but has a difference of two points.
, while the loop test occurs at the beginning of the loop body, while the Do While loop test occurs at the end of the loop: bool done = false;
do {
Some code
Done =somefunctionreturningabool ();
More code
while (! done);
Using a dowhile loop or a while loop depends on the function of the loop itself. Do-While loop statement in syntax: do {
tatements;
while (cond_expr);
As long as the conditional expression cond_expr is true (not 0), do loop repeats statements code segment. The loop state must be initialized before the do statement and explicitly modified in the code snippet. The loop terminates when the conditional expression cond_expr is false.

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.