More Loops Arrays: ITP 165 - Fall 2015 Week 4, Lecture 1
More Loops Arrays: ITP 165 - Fall 2015 Week 4, Lecture 1
Do-While Loop
int number = -1;
do
{
std::cout << "Enter a number >= 0: ";
std::cin >> number;
if (number < 0)
{
std::cout << "Invalid number." << std::endl;
}
}
while (number < 0);
do
{
Condition (must
be in parenthesis)
Required
semicolon
Thats it!
For Loop
All loops can do the same things
Some loops are better than others in certain cases
While loops good when you dont know exactly when to end
Defined by some limit or user input
Do While loops good when you know you need it at least once, but
dont know when to end
Same as While loop
For loops good when you know how many times to run before you
end
For Loop
#include <iostream>
int main()
{
for (int index = 0; index < 10; index++)
{
std::cout << index << std::endl;
}
return 0;
}
For Loops
For keyword
Required parentheses
Required semicolons
Index update
For Loops
The result of the program
For Loops
The index of the loop does not need to be used in the body
statements
It is a separate variable that is created within the scope of the loop
specifically for looping purposes
int counter = 0;
for (int index = 0; index < 10; index++)
{
std::cout << counter << std::endl;
counter++;
}
For Loops
Loop indexes dont have to be integers, but work best when they
are
Loop indexes dont have to increase by 1, but work best when they
do
Loop indexes can also decrease by 1 (--)
For Loops run as long as the conditional is true, which for integer
values is usually a set number of iterations.
For Loops
Hard to run in to an infinite loop with a for loop
But beware:
for (int index = 0; index < 10; index++)
{
std::cout << index << std::endl;
index--;
}
Loops in General
Loops have 3 defining features:
Intialization: Starting of the loop index
Condition: Must be true for loop to continue
Update: Changes in loop index
While loops:
Initialization: happens BEFORE THE LOOP
Condition: happens when DEFINING THE LOOP
Update: happens IN THE LOOP
For loops:
Initialization, Condition, Update: happens when DEFINING THE LOOP
Loops in General
int lIndex = 0;
while (lIndex < 10)
{
//body statements...
lIndex++;
}
Initialization
Conditon
Update
Arrays
An array is a collection of one or more elements that share the
same type
Declaring an Array
Number of elements
in array (must be in
square brackets)
int myArray[10];
End of
statement
Array Indices
To access a specific element in an array, we use a whole number
index
The indices
Start at 0 (eg. the first element in the array is at index 0)
End at number of elements minus 1
Array Example
std::string names[5];
The name of the array is names
The type of the data in the array is std::string
There are 5 elements in the array
The index of the first element is index 0
The index of the last element is index 4
For example, this declares the array, and then sets the value at
index 0 to James:
std::string names[5];
names[0] = "James";
names[0] = "James";
= for
assignment
You have to
remember to change
a 5 to a 10 in two
different places.
int i = 0;
Could we use a variable?
while (i < 5)
{
std::cout << names[i] << std::endl;
i++;
}
int i = 0;
while (i < NUM_STUDENTS)
{
std::cout << names[i] << std::endl;
i++;
}
Variable
i
Value
0
Variable
i
Value
0
Variable
i
Value
0
Variable
i
Value
1
Variable
i
Value
1
Variable
i
Value
1
Variable
i
Value
2
Variable
i
Value
2
Variable
i
Value
2
Variable
i
Value
3
Variable
i
Value
3
Variable
i
Value
3
Variable
i
Value
4
Variable
i
Value
4
Variable
i
Value
4
Variable
i
Value
5
Variable
i
Value
5
Lab Practical #5