Lesson 6: Control Statements (DAY 3)
Control statements are used to control the flow of a program. The if and the switch statements are selection statements (also called conditional statements). They select the path that the program will take depending on certain conditions. The for loop, do loop and while loop are all iterative statements – they cause the program to iterate (repeat) a section of code. Finally, the break, continue and goto statements are jump statements, which can be used to get the program out of a loop. P.S. : goto statements are forbidden in this class.
In all cases, code blocks are delimited by braces { }. This eliminates the need for a ENDIF or LOOP or NEXT statement as in BASIC.
The if statement
This follows pretty much the same as with BASIC:
Code:
if (condition)
}
// put code here
}
else if (othercondition)
{
// more code here
}
else
{
// last bit of code here
}
Notes:
1. the conditional expressions are put in round brackets ( )
2. code blocks are defined by curly braces { }
3. Notice that the first line does not end with a semicolon. This is because the if doesn’t finish until we get to the last brace.
4. unlike in BASIC, else and if are two separate words, always.
If a code block has only one single line to execute, it need not be placed in braces:
Code:
if (condition)
a = 3; // ****only one line of code here – no braces needed
else
{
a = 6; // two lines
b = 75; // need curly braces
}
The switch/case statement
This is identical to the SELECT/CASE statements in BASIC:
Code:
cout << “pick a number from 1 to 4”;
cin >> num;
switch(num)
{
case 1:
cout << “Not a bad number. A little thin perhaps”;
break;
case 2:
cout << “Now we’re talking a number!”;
break;
case 3:
cout<< “Please!”;
case 4:
cout << “Yawn. Next!”
break;
default:
cout << “Read the instructions! Hello, McFly!!”;
}
The switch/case statements are a little tough to get used to, but they are perfect to use with menus.
Notes:
1. The break statement stops the flow from continuing from one case statement to the next. For example, case 3 has no break statement. If the user picks the number 3, the computer will respond with “Please! Yawn. Next!”.
2. The default case is all cases that haven’t been mentioned. This is essentially what the else statement does for an if statement. Default is usually used so the computer can respond to illegal values entered by the user.
The do loop and the while loop
Code:
do
{
// do stuff here
}while(condition);
Notice that the last line ends in a semicolon. Recall that all C++ statements end with either a semicolon or a brace.
This do loop will loop as long as (i.e. while) the condition is true. Note that this is the opposite of a do loop in BASIC, which loops until a condition is met.
A do loop will always execute the code block at least once, since the condition is not checked until the end of the block. If this is undesirable, we use a while loop:
Code:
while(condition)
{
// do stuff here
}
Note that there is no semicolon needed, since the block now ends with a brace.
The for loop
The for loop looks a little different than in BASIC, but it has all of the same elements in it:
Code:
for (i=0; i < 10; i++)
{
// do stuff here
}
there are three parts to the for loop statement.
· i=0, sets the initial value of our counter, in this case i.
· i < 10, is our control condition. The loop will continue as long as the condition is true.
· i++, is identical to saying i = i + 1. This mathematical expression is so common in loops that the makers of C++ decided to give it an abbreviation. Here are some other abbreviations:
Code:
i-- (decrement)
i+=2 (increment by 2 – an abbreviation of i = i + 2)
i-=6 (decrement by 6 – an abbreviation of i = i – 6)
i*=2 (multiply by 2 – an abbreviation of i = i * 2)
etc.
We could alter these three elements of our loop as desired:
Code:
for(i = 10; i < 50; i++) //loops from 10 to 49 (as long as i < 50)
for (i = 10; i > 0; i-- ) //loops from 10 down to 0 (as long as i > 0)
for (i = 1; i < 10; i+=3) // i becomes 1, 4, 7 (as long as i < 10)
Assignment Statements vs. Conditional Expressions
In BASIC, an assignment statement assigns a value to a variable:
A conditional expression looks the same, but has a whole different meaning.
In the first example, mynumber IS 88, whereas in the second example, we are asking IS mynumber 88? This produces an ambiguity, that is, the expression could mean two things, depending on the context. C++ does not allow this ambiguity, and avoids it by using a different symbol for “relational equality”:
Code:
mynumber = 88; // sets mynumber to 88
mynumber == 88; // determines if mynumber is 88
The first symbol (=) is called a arithmetic operator, because it is used in arithmetic expressions. The second symbol (==) is called a relational operator, because it is used to determine the relationship between the left side and the right side (i.e. are they equal?). Other important operators:
Code:
Relational Operators
> (more than)
< (less than)
>= (more than or equal)
<= (less than or equal)
!= (not equal to)
== (equal to)
Logical Operators:
&& (And)
|| (Or)
! (Not)
Quick Summary:
1. There are two kinds of control statements: selection statements and iterative statements.
2. Code blocks begin and end with braces { }
3. Use break to get out of a code block.
4. All c++ statements end in either a semicolon ; or a brace }
5. A control statement (if, for, do) does not really end until the last brace.
6. C++ offers shortcuts such as the increment operator ++, the decrement operator --
Exercises
1. Use a for loop to print out the numbers:
a) 14 to 20 inclusive
b) –10 to 10 inclusive
c) -3, -5, -7, -9
2. Ask the user to enter a number. Use if statements to inform the user if the number is odd or even.
3. Modify your code for #2 to determine if the number is odd or even and positive or negative.
4. Read question #3 carefully. Explain why it is an ambiguous question, and like poor programming, could end up being misinterpreted. What device would a programmer could use to eliminate the ambiguity in the question?
5. Ask the user to enter a number. Use a switch/case statement to give three different responses to the user based on the number, and one default response.
6. Use a while loop for the following code. Ask the user for a number. Keep asking for that number until the user enters a 7.
7. Use an if and a break to modify #7 so that it gives the user 3 tries to get a 7.
8. An example of an expression using some of these relational and logical operators is:
while (i <= 8 && (i==3 || i > 4))
a) This is a difficult expression to read! Write it out in English.
b) Which are the relational operators?
c) Which are the logical operators?
d) What possible values of i will make this expression true?
e) Why are the inner brackets needed?
f) Give the possible values of i with the inner brackets removed.