2. Introduction
• The first of the real programming structures we are going to
look at is the repetition family.
• Used to repeat blocks of code.
• Used to repeat blocks of code.
• Haha, did you see what I did there??
• They’re somewhat more versatile than copy and paste.
• And also play to the fundamental traits of programmers.
3. The Programmer Mindset
• Good programming is based on one basic truth about
programmers.
• We’re horribly lazy.
• Anything that the computer can be doing, it should be doing.
• The less we have to do, the better.
• Repetition structures let us do that to a degree.
4. An Example
• Imagine a program that would give you the average of five
numbers.
• Prompt the user
• Pull in the number
• Add it to a total
• Prompt the user
• Pull in the number
• Add it to a total
• Etc
5. The Program
#include <iostream>
using namespace std;
int main() {
int total = 0;
int tmp;
cout << "Enter number 1" << endl;
cin >> tmp;
total = total + tmp;
cout << "Enter number 2" << endl;
cin >> tmp;
total = total + tmp;
cout << "Enter number 3" << endl;
cin >> tmp;
total = total + tmp;
cout << "Enter number 4" << endl;
cin >> tmp;
total = total + tmp;
cout << "Enter number 5" << endl;
cin >> tmp;
total = total + tmp;
total = total / 5;
cout << "Average is " << total;
}
6. Problems
• Not easy to read.
• Fine for five numbers, more difficult for five hundred.
• Prone to error
• How do you know you’ve repeated the code enough?
• Hard to expand
• What if we now want 500 numbers?
• Difficult to fix
• What if there’s a typo?
• Fix every line of code!
• Can’t adapt to runtime situations.
7. The Repetition Structure
• Repetitions are used to manage these problems.
• We provide a piece of code that says to the compiler ‘repeat
this section of code five times’
• We move responsibility for the repetition onto the compiler.
• Where it belongs.
8. Repetitions
• The loop we are going to talk about today is the for loop.
• It’s an example of a bounded loop
• We use it when we know, at the time the program is running,
how many times we want to loop.
• There are several parts to this structure.
• We’ll go through each in turn.
• The braces on the structure are important too.
• They denote ownership.
9. Parts of a For Loop
• A for loop works on a counter.
• The counter tells us how many times we have repeated.
• Or to use the correct jargon, iterated.
• In the structure of the for loop, we have three parts.
• The initialisation
• This occurs at the start of the loop and never again
• The continuation condition
• We check to see this after ever loo
• The upkeep
• We do this after each time the code has been executed.
10. Parts of a For Loop
• A for loop then has the following structure:
int counter; // Declaration of the counter variable
for (initialization ; continuation ; upkeep) {
// Code to be executed
}
11. The Braces
• Braces are used to indicate ownership.
• Not quite, but it’s a good enough explanation for now.
• The code in the braces belongs to the for loop.
• If no braces are provided, the next statement only belongs to
the for loop.
• This is not good practice.
• For loops don’t end with a semi-colon.
• A semi-colon in C++ is considered a statement all by itself.
12. The Continuation
• The continuation is what determines whether or not a loop
keeps iterating.
• For a for loop, we use the special symbol <
• It means less than.
• This is a comparison operator, and we’ll see more of these as we go
along.
• Usually we base our continuation on the counter variable.
• is the counter less than a set number?
• counter < 5
13. Rewritten Program
#include <iostream>
using namespace std;
int main() {
int total = 0;
int tmp;
int counter;
for (counter = 0; counter < 5; counter++) {
cout << "Enter number " << counter + 1 << endl;
cin >> tmp;
total = total + tmp;
}
total = total / 5;
cout << "Average is " << total;
}
14. Benefits
• Easier to read.
• You can tell at a glance how many times the loop will repeat.
• Easier to maintain.
• Just one section of code to alter.
• Easier to expand.
• If we need to average 500 numbers, we have much less code to
change.
• No transcription errors.
15. Program Style
• Our first structure brings with it our first aesthetic issue.
• Program layout
• One of the things most neglected by beginner programmers is the
layout of their code.
• At their own great cost!
• There are many different ways to layout programs.
• This module adopts one, but does not require its use.
• It does require the use of a layout though.
16. Module Style
• Braces on the same line as the structure.
• Indent one level per level of ownership.
• Indent the statements that belong to main by one level.
• Indent the statements that belong to the for loop that belongs to
main by two levels.
• Judicious use of white space!
• You’ll see this style in all of the lecture notes.
• It is not enforced by the compiler, but leads to more readable
programs.
17. This Program Does Not Work -
Why?
#include <iostream>
using namespace std;
int main() {
int total = 0;
int tmp, i;
for (i = 0; i < 5; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> tmp;
total = total + tmp;
total = total / 5;
}
cout << "Average is " << total;
}
18. Program Style
• The more structures you have, the harder a program becomes
to read if you don’t adopt a style.
• Readable programs are programs that you can debug.
• The layout of the code gives you syntactical clues.
• It shows you where statements belong in the body of the
program.
19. Program Style
• Comments are also hugely important.
• These are text encoded in a program that gets ignored by the
compiler.
• They are notes for the developer.
• You can indicate a single line to be commented using two
slashes:
• // This is a comment.
• Or a block of code using /* and */
• /* This is also a comment */
20. Comments
• Comments should be used to explain complex code.
• It should be used to explain what you intend, not what the code
actually does.
int i;
/*
This for loop modifies a temporary variable
by the counter.
*/
int temp = 10;
for (int i = 1; i < 10; i++) {
temp = temp + i;
}
int i;
/*
This for loop gives the power of temp to
ten
*/
int temp = 10;
for (int i = 1; i < 10; i++) {
temp = temp + i;
}
Bad Better
21. Comments
• There are many heuristics about comments.
• Such as X% of your code should be comments
• I don’t adhere to any of these.
• I find indiscriminate commenting actually detracts from readability.
• Some guidelines:
• Comment intention where your intention is not blindingly obvious.
• Later when you start to use functions.
• Document what the function does.
• Document parameters
• Document return values
22. Comments
• Comments help gain big picture understanding.
• Don’t comment the obvious.
• Consider your audience.
• It’s not going to be your relatives who read your code.
• It’s going to be fellow programmers who understand the syntax.
• Comment your code like the person who ends up maintaining it is a
violent psychopath who knows where you live.
• Damian Conway
23. Program Style
• Good programs are readable programs.
• Good code is its own documentation.
• You should always make use of meaningful names for your
variables.
• Again, not enforced by the compiler.
• Bad variable names inhibit understanding:
• av = (av / 100) * 102.15;
• Good variable names increase understanding:
• accountValue = (accountValue / 100) * 102.15;
24. Program Style
• Avoid the use of magic numbers.
• Numbers embedded in your code with no explanation.
int interestRate = 102.15;
int percent = 100;
accountValue = (accountValue / percent) * interestRate;
25. Program Style
• I don’t want to harp on about this.
• But I’m going to anyway.
• The single worst thing you can do in a program is ignore the
aesthetics.
• The single best thing you can do is adopt a strict coding style.
• There are holy wars of formatting about which style is best.
• They’re all roughly as good as each other. It doesn’t matter what
style you adopt, it only matters that you adopt one and stick to it.
26. Summary
• Repetition is a powerful programming tool.
• Repetition is a powerful programming tool.
• Repetition is a powerful programming tool.
• If a joke is funny once it’s funny every time, right?
• Some jokes aren’t funny the first time.
• Program style is important!
• Don’t neglect it!
• Comment your code
• Where appropriate