Lesson 1 - Brief Review On The Concepts and Fundamentals of C++
Lesson 1 - Brief Review On The Concepts and Fundamentals of C++
PAGE \*
Fundamentals of C++
MERGEFOR
▪Use cin>> for INPUT. The symbol >> is called extraction operator.
▪Use cout<< for OUTPUT. The symbol << is called insertion operator.
In C++, there are different types of variables (defined with different keywords), for
example:
● int - stores integers (whole numbers), without decimals, such as 123 or -123
● double - stores floating point numbers, with decimals, such as 19.99 or -19.99
● char - stores single characters, such as 'a' or 'B'. Char values are surrounded
by single quotes
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
● string - stores text, such as "Hello World". String values are surrounded by
double quotes
● bool - stores values with two states: true or false
To create a variable, you must specify the type and assign it a value:
Syntax
Example
int myNum;
int myNum = 15;
cout << myNum;
To declare more than one variable of the same type, use a comma-separated list:
Example
int x,y,z;
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
Identifiers can be short names (like x and y) or more descriptive names (age, sum,
totalVolume).
C++ Operators
Example
int x = 100 + 50;
Arithmetic Operators
Assignment Operators
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
Example
int x = 10;
Comparison/Relational Operators
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
Example
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater than 3
Logical Operators
Logical operators are used to determine the logic between variables or values:
Conditional Statements
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
You can use these conditions to perform different actions for different decisions.
The if Statement
Syntax
if (condition)
{
// block of code to be executed if the condition is true
}
In the example below, we test two values to find out if 20 is greater than 18. If
the condition is true, print some text:
Example
● if (20 > 18)
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
{
cout << "20 is greater than 18";
}
Syntax
if (condition)
{
// block of code to be executed if the condition is true
}
else
{
// block of code to be executed if the condition is false
}
Example
int time = 20;
if (time < 18)
{
cout << "Good day.";
}
else
{
cout << "Good evening.";
}
// Outputs "Good evening."
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
Use the else if statement to specify a new condition if the first condition is false.
Syntax
if (condition1)
{
// block of code to be executed if condition1 is true
}
else if (condition2)
{
// block of code to be executed if the condition1 is false and
condition2 is true
}
else
{
// block of code to be executed if the condition1 is false and
condition2 is false
}
Example
int time = 22;
if (time < 10)
{
cout << "Good morning.";
}
else if (time < 20)
{
cout << "Good day.";
}
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
else
{
cout << "Good evening.";
}
// Outputs "Good evening."
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
The example below uses the weekday number to calculate the weekday name:
Example
int day = 4;
switch (day) {
case 1:
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
// Outputs "Thursday" (day 4)
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need
for more testing.
Example
int day = 4;
switch (day) {
case 6:
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
Looping Statements
C++ Loops
Loops are handy because they save time, reduce errors, and they make code
more readable.
Syntax
while (condition)
{
// code block to be executed
}
In the example below, the code in the loop will run, over and over again, as long as
a variable (i) is less than 5:
Example
int i = 0;
while (i < 5)
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
{
cout << i << "\n";
i++;
}
Syntax
do {
// code block to be executed
}
while (condition);
The example below uses a do/while loop. The loop will always be executed at least
once, even if the condition is false, because the code block is executed before the
condition is tested:
Example
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);
When you know exactly how many times you want to loop through a block of
code, use the for loop instead of a while loop:
Syntax
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
{
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 3 is executed (every time) after the code block has been executed.
Example
C++ Break
Example
C++ Continue
Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR
Example
Reference: https://www.w3schools.com/cpp/default.asp