0% found this document useful (0 votes)
75 views14 pages

Lesson 1 - Brief Review On The Concepts and Fundamentals of C++

The document provides an overview of basic C++ concepts including input/output using cin and cout, declaring variables of different data types, and using basic operators. It also covers conditional statements like if, else if, and else to execute code under certain conditions. Key points covered include declaring and initializing variables, using extraction and insertion operators for input/output, and syntax for if, else if, and else statements to control program flow.
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)
75 views14 pages

Lesson 1 - Brief Review On The Concepts and Fundamentals of C++

The document provides an overview of basic C++ concepts including input/output using cin and cout, declaring variables of different data types, and using basic operators. It also covers conditional statements like if, else if, and else to execute code under certain conditions. Key points covered include declaring and initializing variables, using extraction and insertion operators for input/output, and syntax for if, else if, and else statements to control program flow.
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/ 14

CC123(C) - Lesson 1 Brief Review on the Concepts and

PAGE \*
Fundamentals of C++
MERGEFOR

C++ Basic Input/Output


The <iostream> should always be included when there is an output
and input in the program

▪Use cin>> for INPUT. The symbol >> is called extraction operator.

▪Use cout<< for OUTPUT. The symbol << is called insertion operator.

▪When using cout,


✔ Strings should always be inside the "". Example: cout<<"Hello";
✔ Variables should NOT be inside the "". Example: cout<<variable_name;
✔When strings and variables are declared on the same
line, use << as a concatenator. Example:
cout<<"Hello"<<variable_name;

VARIABLES AND OPERATIONS


C++ Variables
Variables are used to represent a value or hold a value.
In naming variables, always remember the following:
✔ Variables could be alphanumeric (combination of numbers and
letters)
✔ Should always begin with a letter or underscore (A – Z, a – z, _).
✔ Only the special character _ (underscore) is acceptable.
✔ Spaces are NOT allowed.
✔ Reserved words / keywords are also NOT allowed.

Variables are containers for storing data values.

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

Declaring (Creating) Variables

To create a variable, you must specify the type and assign it a value:

Syntax

data type identifier/variable name

type  variable  =  value;

Example

Create a variable called myNum of type int and assign it the value 15:

int myNum;

int myNum = 15;
cout << myNum;

Declare Many Variables

To declare more than one variable of the same type, use a comma-separated list:

Example

int x,y,z;

int x = 5, y = 6, z = 50;


cout << 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

C++ Identifiers/Variable Name

All C++ variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum,
totalVolume).

C++ Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example

int x = 100 + 50;

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

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

Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator (=) to assign the value 10 to


a variable called x:

Example

int x = 10;

Comparison/Relational Operators

Comparison operators are used to compare two values.

Note: The return value of a comparison is either true (1) or false (0).

Reference: https://www.w3schools.com/cpp/default.asp
CC123(C) - Lesson 1 Brief Review on the Concepts and
PAGE \*
Fundamentals of C++
MERGEFOR

In the following example, we use the greater than operator (>) to find out if 5 is


greater than 3:

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

C++ Conditions and If Statements

C++ supports the usual logical conditions from mathematics:

● Less than: a < b


● Less than or equal to: a <= b
● Greater than: a > b
● Greater than or equal to: a >= b
● Equal to a == b
● Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

C++ has the following conditional statements:

● Use if to specify a block of code to be executed, if a specified condition is


true
● Use else to specify a block of code to be executed, if the same condition is
false
● Use else if to specify a new condition to test, if the first condition is false
● Use switch to specify many alternative blocks of code to be executed

The if Statement

Use the if statement to specify a block of C++ code to be executed if a


condition is true.

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";
}

The if - else Statement

Use the else statement to specify a block of code to be executed if the


condition is false.

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

The else if Statement

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

C++ Switch Statements

Use the switch statement to select one of many code blocks to be executed.

Syntax

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

This is how it works:

● The switch expression is evaluated once

● The value of the expression is compared with the values of each case

● If there is a match, the associated block of code is executed

● The break and default keywords are optional, and will be described later in


this chapter

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)

The break Keyword

When C++ reaches a break keyword, it breaks out of the switch block.

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.

The default Keyword

The default keyword specifies some code to run if there is no case match:

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

    cout << "Today is Saturday";


    break;
  case 7:
    cout << "Today is Sunday";
    break;
  default:
    cout << "Looking forward to the Weekend";
}
// Outputs "Looking forward to the Weekend"

Looping Statements

C++ Loops

Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code
more readable.

C++ While Loop

The while loop loops through a block of code as long as a specified condition


is true:

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++;
}

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code


block once, before checking if the condition is true, then it will repeat the loop as
long as the condition is true.

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);

C++ For Loop

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

for (statement 1; statement 2; statement 3)

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 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

The example below will print the numbers 0 to 4:

Example

for (int i = 0; i < 5; i++)


{
  cout << i << "\n";
}

C++ Break

You have already seen the break statement used in an earlier chapter of this


tutorial. It was used to "jump out" of a switch statement.

The break statement can also be used to jump out of a loop.

This example jumps out of the loop when i is equal to 4:

Example

for (int i = 0; i < 10; i++)


{
  if (i == 4)
{
    break;
  }
  cout << i << "\n";
}

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

The continue statement breaks one iteration (in the loop), if a specified condition


occurs, and continues with the next iteration in the loop.

This example skips the value of 4:

Example

for (int i = 0; i < 10; i++) {


  if (i == 4)
{
    continue;
  }
  cout << i << "\n";
}

Reference: https://www.w3schools.com/cpp/default.asp

You might also like