C++ Controls
To be or not to be, that is the question.
By default, instruction execution is sequential. Conditional statement is used to change the program execution sequence.
If, If..else
choose one of the two options...
if (a > b) cout << "a is greater than b";
else cout << "a is less than or equal to b"; // only one of the two cout will be executed
What if we want to have more than one instructions in the option?
... use curly braces to "block" the instructions.
if (a > b) {
cout << "a is greater than b";
cout << "are you happy?";
}
else {
cout << "a is less than or equal to b"; // only one of the two cout will be executed
}
... since there is only one instruction, braces are not needed, but it does not hurt.
What if the decision is not a simple one?
... compound conditions are possible.
... check out the logic operators
if (a > =10 && a < 100) {
cout << "a is a 2 digit number\n";
cout << "are you happy?" << endl; // note that endl and \n achieve the same result
}
Relational & Logical Operators
Conditional statements need a true or false "condition" (the bool type). It is typically generated by a relational or logical operators.
Relational operators are: >, <, >=, <=, == (equal), != (not equal)
Note:
>= is one operator, you can not have space in between.
same for all other multi-character operators (e.g. ==, !=, +=, ++, etc)
pay attention to equal, a=3 is totally different from a==3. a=3 is an assignment function which ALWAYS yield true.
there is one tricky situation... Even though you shouldn't write code like it, we know if (a=3) always yields true. How about if (a=0)? Will if (a=0) yield true or false?
Logical operators are: && (and), || (or), ! (not)
Note:
! and != are totally different thing.
in computer, 0 is false, everything else is true
Switch
switch (x) {
case 1: cout << "x is 1\n";
break;
case 2:
case 3: cout << "x is 2 or 3" << endl;
break;
case 4: cout << "x is 4\n"; // 4 will continue to 5
case 5: cout << "x is 5\n"; break;
default:
cout << "everything else" << endl;
}
Note:
Go to cplusplus.com tutorial control structures or MIT slides under flow of controls for more tutorial on these statement.
There is usually more than one way to solve conditional issues. Try mapping numerical grade to letter grade, e.g. 90+ is A, 80-90 is B, etc..
I added the files "branch.cpp" in the attachment below which shows couple ways of doing grade mappings.
Loop statement instructs the repetition of instructions.
For loop
While loop
Do While loop
Three different ways to solve the same problem : print 2 digit even numbers.
#define LO 10
#define HI 100
for (int i= LO; i < HI; i=i+2) cout << i << " " ;
cout << endl;
i = LO;
while ( i < HI) {
cout << i << " ";
i=i+2;
}
i = LO;
do {
cout << i;
i = i+2;
} while ( i < HI );
Note:
Use #define to avoid "hard code" constant, so that it is easier to change the value later.
When more than one instructions are needed for a block, use { } to group them.
Try a few different boundary conditions, e.g. set both LO and HI to the same value and see the difference among the three approaches.
Have you experiment the difference between ++i and i++?
Which loop structure is better? it depends on personal preference and problem at hands.
Loop Interrupt -- break and continue
Discussions:
What if we want to change the design to print out even numbers between LO and HI?
for (int i= LO; i < HI; i++) {
if (i % 2 == 0) cout << i << " " ;
}
cout << endl;
What if we don't want to print out even numbers that are also multiple of 3?
for (int i= LO; i < HI; i++) {
if ( ( i %2 ==0) && ( i%3 !=0) ) cout << i << " " ;
}
cout << endl;
Because of operator precedence and true/false definition, the following code is equivalent:
for (int i= LO; i < HI; i++) if ( i %2 ==0 && i%3 ) cout << i << " " ;
cout << endl;
...... Although it is "less code", I won't recommend this style of programming for a simple reason: hard to read. A few more parenthesis or braces or comparators do not really improve performance, but it sure makes code more readable.
How about a different approach?
for (int i= LO; i < HI; i++) {
if ( i % 2 ) continue;
if ( i % 3 == 0 ) continue;
cout << i<< " ";
}
cout << endl;
...... If it is not an even number, we continue to next loop (i.e. skip the rest). If it is a multiple of 3, we skip it too. Coding can be a good brain exercise, right?