MCD4720 Fundamentals of C++
MCD4720 Fundamentals of C++
MCD4720 Fundamentals of C++
Fundamentals of C++
Lecture 3
Syntax Building Blocks
2: Selection, Repetition
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been reproduced and communicated to you by or on behalf of Monash University
pursuant to Part VB of the Copyright Act 1968 (the Act).
The material in this communication may be subject to copyright under the Act. Any further reproduction
or communication of this material by you may be the subject of copyright protection under the Act.
Do not remove this notice.
2
MCD4720 Foundations of C++
Today’s Lecture Overview
• Selection
• Repetition
4
This Week’s Learning Objectives
• Understand how to implement decisions and the role they play in
program design
• Understand how to implement loops and when to use each type of
loop
5
Decisions
• Selection is the next main control structure for us to look at
• Rather than each line of code executing one after the other, with
selection (also known as decisions) a choice has to be made about
whether a line of code runs, or indeed which line of code to run
• You will (hopefully!) be familiar with this from Scribble or any other
programming that you have done
6
Decisions - Algorithm
• The basic notion of a decision is the same in all programming
languages… it is sometimes just the exact syntax that varies
if (expression) if (expression)
statement1;
statement;
else
statement2;
7
Decisions - Scribble
9
Decisions
• Or we can have a more complex statement:
13
Expressions
• So, for example, our expression can be nice and simple:
• (myAge >= 18)
14
More Complex Decisions – else if
• Decisions with 3 or more options usually move from nested IF’s to an
alternative
• We can have a sequence of IF’s using ELSE IF
if (expression1)
statement1;
else if (expression2)
statement2;
…
else
statementX;
15
More Complex Decisions – else if
•For example
if (score >= 80) {
cout << "You got a HD!";
}
else if (score >= 70) {
cout << "You got a D!";
}
else if (score >= 60) {
cout << "You got a C!";
}
else if (score >= 50) {
cout << "You got a P!";
}
else {
cout << "Sorry, but you didn’t pass";
}
16
More Complex Decisions – else if
• If expression1 is true, statement1 is executed and the rest of the
sequence is skipped
• Otherwise expression2 is tested, etc etc
• If no expression is true then the statement associated with the final
else clause is executed
17
More Complex Decisions – switch
• Our other option is a switch statement (like Java)
switch (choice) {
case value1: statement1;
break;
case value2: statement2;
break;
…
default: statementX;
}
18
More Complex Decisions – switch
• For example
switch (letter) {
case 'a':
cout << "a is for alpha" << endl;
break;
case 'b':
cout << "b is for bravo" << endl;
break;
default:
cout << "I don't know that letter?" << endl;
}
19
More Complex Decisions – switch
• Notice the break statements after each case?
• We don’t need them, but if we don’t have them the code won’t stop
executing… even if the case isn’t true!
• Default handles the non-matching cases
• Remember you can’t test for expressions… only actual values
• It is also important to note that floating point numbers are also invalid
20
Loops
• Repetition (i.e. loops) are our final control structure to look at
• Like decisions, their fundamental structure does not really differ
between programming environments
• We will look at three different variations:
• While (pre-test)
• Do… while (post-test)
• For (counted)
21
While Loops
• A while loop is just a decision that keeps on executing while the
expression is true, evaluated at the beginning of the loop:
while (expression)
statement;
22
While Loops
• For example
char again = 'y';
while (again == 'y') {
cout << "\nDo You Want To Play Again? (y/n): ";
cin >> again;
}
23
Do… While Loops
• A do… while loop is the same, however the expression is evaluated at
the end of the loop:
do
statement;
while (expression);
24
Do… While Loops
• For example
char again = 'y';
do {
cout << "\nDo You Want To Play Again? (y/n): ";
cin >> again ;
} while (again == 'y');
25
For Loops
• A For loop is typically used anytime we need to iterate over a range of
values:
27
For Loops
• Obviously, we don’t NEED for loops… we can perform the same task with a
standard while loop
• For example:
int x = 1; // Note this is the initialisation
}
• But a for loop makes it nice and easy for us, as well as inherently telling us it
is a counted loop
28
What Loop Do I Choose?
• How do we choose between repetition structures? As the programmer, you can decide.
There are some rules of thumb though:
• while loops are for loops with possibly zero iterations. These loops check to see if
the condition is meet before looping. So choose this if a loop may run zero or more
times.
• do…while loops are for loops with a least one iteration; where the program runs
the following code before it checks whether or not to loop another time.
• for loops are for counter-controlled loops where you know how many times you
want to loop the following code.
• It is important to note that not all programming languages have all three types of loop!
29
A Word on Break and Continue
• break and continue statements can be used to violate the normal
behaviours of a loop
• As their names suggest:
• break will break out of a loop prematurely
• continue will force a loop back to the start prematurely
30
Break and Continue
int x = 0;
while (true) {
x += 1;
if (x > 10) {
break;
}
if (x == 5) {
continue;
}
cout << x << endl;
}
31
Break and Continue
• Only use break and continue statements if you really have to
• Most things can be done without them (switch statements are obvious
exceptions) and they can lead to both confusing code and sloppy
programming
• Please avoid!
32
What Should I Do Now?
• Write more C++!
• Make sure you are still comfortable with decisions and loops and can
implement them in C++
• Start using arrays to store your data
• Get ready to get right into C++!
33