0% found this document useful (0 votes)
30 views10 pages

Solo Learn

Uploaded by

Usman Abdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views10 pages

Solo Learn

Uploaded by

Usman Abdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

INTRODUCTION TO C++

Conditionals and Loops:

 Multiple Conditions:
 Lesson Takeaways:

Logical operators allow you to combine multiple conditions.


 The AND operator && combines two conditions and checks if both of them are true.

 The OR operator || check if any of the conditions are true.

 The NOT operator! Reverses the condition.

 You can combine and chain conditions using parentheses and logical operators.

 WHILE LOOP:

 Loops

 A loop allows you to repeat a block of code multiple times.


 For example, a game can use a loop to take input from the user controls, or a
warehouse management system can loop through all items in the warehouse and
perform calculations.

 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.

Increment & Decrement


 As it's common to increment and decrement a variable's value by 1 in loops, C++
provides special increment and decrement operators.
 For example, num=num-1 can be shortened to num--:
 Similarly, num++ will increase the value of num by 1

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.

 C++ provides shorthand operators to perform mathematical operations on a


variable, for example num=num * 5 can be written as num * = 5.

 - 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 for loop has 3 components in the parentheses:

 The first part runs once when we enter the loop and initializes the variable.

 The second part is the condition of the loop.


 The third part runs every time the loop runs.

 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.

MODULE 2 QUIZ PASSED 

MODULE 3: ARRAYS & POINTERS

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:

double prices[] = {5.99, 3.2, 9.99, 29.99};

for(double x: prices) {

cout << x << endl; }


The loop creates a variable, which is automatically assigned to each value of the array
during the loop.
We called it x in our example, but you can name it anything you want.

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:

Now, the code will work for any type of arrays.

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:

for(int x=0;x<5; x++) {//current item is arr[x]}


Another way to loop over arrays is the for-each loop: for(auto x: arr) {

//current item is x

}
POINTERS:

Remember the following:


 The & operator is used to access the memory location of a variable.
 The * operator is used to access the value of a memory address that is stored in a
pointer.
 The same * sign is also used to declare a pointer, and it is different from the
dereference operator.

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 = &num;
 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.

You might also like