If statement
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 10" << 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 == 20) {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;
}