C++ Basics Tutorial - Lesson 3
Related Blog Items
- C++ Basics - Tutorial
- C++ Tutorial Part 2 - Advanced
- C++ Advanced Tutorial - Lesson 11
- C++ Advanced Tutorial - Lesson 8
- C++ Advanced Tutorial - Lesson 3
Please refer lesson 2..
3. Control Structures
3.1 Control Structures
3.2 if
3.3 if-else
3.4 Conditional Operator
3.5 while
3.6 for Loop. 17
3.7 Avoid Modifying the Control Variable in the Loop Body
3.8 switch
3.9 do-while
3.10 break and continue
3. Control Structures
3.1 Control Structures
C++ has only seven control structures:
Sequence
Selection “if” single-selection structure
Selection “if/else” double-selection structure
Selection “switch” multi-selection structure
Repetition “while”
Repetition “do/while”
Repetition “for”
3.2 if
if( grade >= 60 ) cout << "Passed! \n" ;
3.3 if-else
if( grade >= 60 ) cout << "Passed! \n" ; else if( grade >= 40 ) cout << "Failed! \n" ; else cout << "Shamed! \n" ;
3.4 Conditional Operator
Conditional operator “?” and “:” are used to form an operand or statement, the value of which is chosen from two options, depending on the value of the condition expression:
condition expression ? option 1 : option 2;
Example:
cout << ( grade >= 60 ? "Passed! \n" : "Failed! \n" ); grade >= 60 ? cout << "Passed! \n" : cout << "Failed! \n" ;
3.5 while
while ( condition expression ) statement;
If the condition expression is true the statement will be performed, and the condition expression checked again. If there is no action in the statement to cause the condition expression to become false eventually, it will cause a infinite loop .
“while” structure actually is an “if” structure with a go to statement at the end to go back to “if” .
3.6 for Loop
for(expression 1; expression 2; expression 3)
statement
expression 1: initialize the loop s control variable;
expression 2: loop-continuation condition;
expression 3: increment the control variable.
As a complete loop, first the condition is checked, if satisfied, the statement is executed, then the control variable is incremented.
Example:
d7050d4f549caaab0cbc172f0f705653003
Notice that after the loop the variable counter will have a value of 12.
Expression 1 and 3 can be lists of comma-separated expressions. The comma used here are comma operators . The value of the list is the value of the last expression. It is usable when you need more than one local variable in the loop. By initializing these variables inside the loop instead of outside the loop, it makes the program clearer, and also conforms with the Principle of Least Privilege (PLP).
If expression 1 is missing from the for header but it has been initialized before the loop, the value will be used. If expression 2 is missing, the continuation condition will be by default true and the loop will run infinitely. If the control variable is incremented in the loop body, then expression 3 can be saved. Anyway the colon can not be saved.
Many programmers prefer expression 3 to be counter ++ , because increment occurs only after the loop body is executed.
3.7 Avoid Modifying the Control Variable in the Loop Body
It is not a error but it can produce unexpected results.
3.8 switch
switch (controlling expression)
{
case a:
case b:
statements;
break;case c:
statements;
break;default:
statements;
}
a, b, c are called “case labels”. They can only be a constant, or a character represented by ‘a’ or ‘A’ . When “switch” statement is executed, the case labels are one by one compared with the controlling expression. When one is equal to the expression, all the statements after that case label will be executed, until meeting one “break” statement. So putting different labels together simply means “OR” .
If a “default:” label is put, when no case label is matched, the statements after the “default:” label is executed. It is not a must but a good practice to always put a default label even if you are absolutely sure your program is free of bugs.
A “break” statement is not required after the “default” case if it is at the last.
3.9 do-while
do
{
statement1;
statement2;
}while (continuation statement);
The only difference between this and while statement is that the continuation condition is checked after the body had be executed.
#include <iostream> int main () { char keyboard; int a; do { cout << "Enter one character, and I will tell you its ASCII: \n \n"; cin >> keyboard; cout >> (a = static_cast <int> (keyboard)) << endl <<"\n \n \n \n"; }while (a != 101 && a != 69); return 0; }
If we use “while” we have to add one statement before the loop: “a=0;”.
3.10 break and continue
In the body of “while” , “for” , “do/while” or “switch” , “break” statement causes immediate exit from the statement, while “continue” skips the statements after it until the end of loop, and begins as normal the next loop.
Notice that break can only exit one layer of loop. If there are more than one layers of nested control structure, break can not exit all of them:
int main() { fstream file1("test.txt", ios::out); int i, j; for (i = 0; i < 10; i++) { file1 << "Outer loop: i = " << i << endl; for (j = 0; j < 10; j++) { file << "Inner loop: j = " << j << endl; if(i == 3 && j == 3) break; } file1 << endl; } file1.close(); cin >> i; }
In this case, a unstructured programming technique goto can be used.
Popularity: 16%
You need to log on to convert this article into PDF
Related Blog Items - C++ Basics - Tutorial
- C++ Tutorial Part 2 - Advanced
- C++ Advanced Tutorial - Lesson 11
- C++ Advanced Tutorial - Lesson 8
- C++ Advanced Tutorial - Lesson 3
Related Blog Items
- C++ Basics - Tutorial
- C++ Tutorial Part 2 - Advanced
- C++ Advanced Tutorial - Lesson 11
- C++ Advanced Tutorial - Lesson 8
- C++ Advanced Tutorial - Lesson 3
No Comments
No comments yet.