Solo Learn
Solo Learn
Multiple Conditions:
Lesson Takeaways:
You can combine and chain conditions using parentheses and logical operators.
WHILE LOOP:
Loops
While Loop
The while loop takes a condition and repeats its statements while the condition is true.
Example:
int num = 5;
while(num > 0) {
cout << num << endl;
num = num - 1;
}
The while loop checks for the condition num > 0. If it evaluates to true, it
executes the statements within its body. Then it checks for the statement again
and repeats.
The given code will output the numbers 5 to 1 and then stop, as num will reach 0.
The statement num = num - 1 decreases the value of num by 1 each time the
loop runs.
This is important, as without it the loop would run forever.
Shorthand Operators
Sometimes you might need to increase or decrease the value of a variable by a
different value than 1.
For these cases, C++ provides shorthand operators, too!
For example, x=x+2 can be shortened to x+=2
do while
Another variation of the while loop is do-while.
Here is an example:
int num = 0;
do {
cout << num << endl; num+=2;
} while(num <= 10);
The difference with a while loop is that the condition is checked after the code,
meaning the code in the do is executed at least once, even if the condition
is false.
Also, note the semicolon after the while condition.
Try changing the condition in the code abov
Lesson Takeaways
The code in the while loop runs as long as the condition is true.
The ++ and -- operators are used to increase and decrease the value of a
variable by one.
- The do-while loop is similar to a while loop, but it is guaranteed to run at least
once.
for Loops
EXAMPLE:
Let's look at the code and understand how it works:
for(int i=1;i<=5;i++) {
cout << i << endl; }
The first part runs once when we enter the loop and initializes the variable.
The for loop is best when we know the number of times we need to run the loop.
Remember the break; statement that was used in switch to stop it when
a case was matched?
It can also be used to stop a loop.
continue
The continue statement skips the current loop iteration and continues with the next one.
Arrays
Imagine working on a shopping app. Your program will need to store and work with
multiple products and their prices.
Instead of creating separate variables for each price, we can use an array to store all
values!
An array stores multiple values in a single variable.
An array needs to be declared like a variable, with the type of the items it will hold.
After declaring the array, we can access the items using their position, also called
the index.
The item with index 3 is actually the 4th item of the array.
That's because array indexes start from 0, meaning that the first element's index is 0
rather than 1.
Lesson Takeaways
Awesome! Here are some key points about arrays:
Arrays allow to store multiple values in a single variable.
When creating an array, we need to provide the type of the items and the size of the
array, like this:
int numbers[15];
Array items are accessed using their indexes, placed in square brackets. The first item
has the index 0.
You can also create an array with values using the following syntax, initializing its
values:
int numbers[] = {1, 2, 3, 4, 5};
Arrays
To make iterating over arrays easier and shorter, C++ provides another type of
the for loop, called the for-each loop.
Here is an example:
for(double x: prices) {
Notice the colon after the variable - it reads as "for each x in prices".
Remember the auto keyword? It was used to automatically set the type of a variable
based on the value it is assigned to.
We can also use the auto keyword in a for-each loop:
Lesson Takeaways
Now you know how to loop over arrays.
You can use a for loop to loop over an array.
For example, for an array called arr of 5 items:
//current item is x
}
POINTERS:
Lesson Takeaways
Pointers might seem confusing at first, but don't worry, you'll get the hang of it! Here are
the key takeaways:
A pointer is a variable that stores the memory address of another variable.
It is declared using a * and the type of the value it points to, for example: int *p;
The memory address of a variable can be accessed using the & operator, and assigned
to a pointer, for example: int *p = #
The value of a memory address can be accessed using the * operator, for example *p is
the value stored at the address that p points to.
A pointer can also be assigned to an array and be used to access the elements of the
array, by simply incrementing the pointer.