0% found this document useful (0 votes)
77 views40 pages

What Is C++?: Camarines Sur Polytechnic Colleges

The document provides an overview of the C++ programming language including its history, uses, basic syntax, variables, data types, operators, conditions, loops, and functions. C++ is a popular, general-purpose programming language developed in the late 1970s as an extension to C. It is commonly used to develop applications that require high-performance, such as operating systems, games, and embedded systems. The document outlines the basic steps to get started with C++ including installing an IDE and writing a simple "Hello World" program.

Uploaded by

Princess Noble
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)
77 views40 pages

What Is C++?: Camarines Sur Polytechnic Colleges

The document provides an overview of the C++ programming language including its history, uses, basic syntax, variables, data types, operators, conditions, loops, and functions. C++ is a popular, general-purpose programming language developed in the late 1970s as an extension to C. It is commonly used to develop applications that require high-performance, such as operating systems, games, and embedded systems. The document outlines the basic steps to get started with C++ including installing an IDE and writing a simple "Hello World" program.

Uploaded by

Princess Noble
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/ 40

Republic of the Philippines

CAMARINES SUR POLYTECHNIC COLLEGES


Nabua, Camarines Sur

Topic: C++

Coverage:

1. Introduction
2. Get Started
3. Syntax
4. Output
5. Comments
6. Variables
7. User Input
8. Data Types
9. Operators
10. Strings
11. Math
12. Boolean
13. Conditions
14. Switch
15. While Loop
16. For Loop
17. Break/ Continue

What is C++?

C++ is a popular programming language.

C++ is used to create computer programs.

C++ is a cross-platform language that can be used to create high-performance applications.

C++ was developed by Bjarne Stroustrup, as an extension to the C language.

C++ gives programmers a high level of control over system resources and memory.

The language was updated 3 major times in 2011, 2014, and 2017 to C++11, C++14, and
C++17.

Why Use C++


C++ is one of the world's most popular programming languages.

C++ can be found in today's operating systems, Graphical User Interfaces, and embedded
systems.

C++ is an object-oriented programming language which gives a clear structure to programs


and allows code to be reused, lowering development costs.

C++ is portable and can be used to develop applications that can be adapted to multiple
platforms.
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

C++ is fun and easy to learn.

As C++ is close to C# and Java, it makes it easy for programmers to switch to C++ or vice
versa

Applications of C++ Programming

 Application Software Development - C++ programming has been used in


developing almost all the major Operating Systems like Windows, Mac OSX and
Linux. Apart from the operating systems, the core part of many browsers like Mozilla
Firefox and Chrome have been written using C++. C++ also has been used in
developing the most popular database system called MySQL.
 Programming Languages Development - C++ has been used extensively in
developing new programming languages like C#, Java, JavaScript, Perl, UNIX’s C
Shell, PHP and Python, and Verilog etc.
 Computation Programming - C++ is the best friends of scientists because of fast
speed and computational efficiencies.
 Games Development - C++ is extremely fast which allows programmers to do
procedural programming for CPU intensive functions and provides greater control
over hardware, because of which it has been widely used in development of gaming
engines.
 Embedded System - C++ is being heavily used in developing Medical and
Engineering Applications like softwares for MRI machines, high-end CAD/CAM
systems etc.

C++ Get Started


To start using C++, you need two things:

 A text editor, like Notepad, to write C++ code


 A compiler, like GCC, to translate the C++ code into a language that the computer will
understand

C++ Install IDE


An IDE (Integrated Development Environment) is used to edit AND compile the code.

Popular IDE's include Code::Blocks, Eclipse, and Visual Studio.


These are all free, and they can be used to both edit and debug C++ code.

Note: Web-based IDE's can work as well, but functionality is limited.


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

C++ Quickstart
Open Codeblocks and go to File > New > Empty File.

Write the following C++ code and save the file as myfirstprogram.cpp (File > Save File as):

Every C++ program starts from the main() function.


The cout is the standard output stream which prints the "Hello, World!" string on the

monitor.

The return 0; is the Exit status" of the program.


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Line 1: #include <iostream> is a header file library - work with input and output objects,
such as cout (used in line 5). Header files add functionality to C++ programs.

Line 2: using namespace std means that we can use names for objects and variables from
the standard library.

Note: Don't worry if you don't understand how #include <iostream> and using
namespace std works. Just think of it as something that (almost) always
appears in your program

Line 3: A blank line. C++ ignores white space.

Line 4: Another thing that always appear in a C++ program, is int main(). This is called a
function. Any code inside its curly brackets {} will be executed.

Line 5: cout (pronounced "see-out") is an object used together with the insertion operator
(<<) to output/print text. In our example it will output "Hello World".

cout is pronounced "see-out". Used for output, and uses the insertion operator (<<)

cin is pronounced "see-in". Used for input, and uses the extraction operator (>>)

Note:

1. Every C++ statement ends with a semicolon ;.


2. The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Remember:

The compiler ignores white spaces. However, multiple lines makes the code more readable.

Line 6: return 0 ends the main function.

Line 7: Do not forget to add the closing curly bracket } to actually end the main function.

Then, go to Build > Build and Run to run (execute) the program.

Result :

Omitting Namespace
You might see some C++ programs that runs without the standard namespace library.

The using namespace std line can be omitted and replaced with the std keyword, followed by
the :: operator for some objects:

 It is up to you if you want to include the standard namespace library or not.


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

C++ Output (Print Text)


The cout object, together with the << operator, is used to output values/print text:

Note: You can add as many cout objects as you want. However, it does not insert a
new line at the end of the output:

C++ New Lines

To insert a new line, you can use the \n character:


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

 Two \n characters after each other will create a blank line:

Note: Another way to insert a new line, is with the endl manipulator:

Both \n and endl are used to break lines. However, \n is used more often and is the
preferred way.

Exercises 1:
1. Insert the missing part of the code below to output "Hello World".

int main() {
<< "Hello World!";
return 0;
}

2. Insert a new line after "Hello World", by using a special character:


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

3. Comments in C++ are written with special characters. Insert the missing
parts:

C++ Comments
Comments can be used to explain C++ code, and to make it more readable. It can also be
used to prevent execution when testing alternative code. Comments can be singled-lined or
multi-lined.

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by the compiler (will not be
executed).

This example uses a single-line comment before a line of code:


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

This example uses a single-line comment at the end of a line of code:

C++ Multi-line Comments


Multi-line comments start with /* and ends with */.

Any text between /* and */ will be ignored by the compiler:

Example
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";

Single or multi-line comments?


It is up to you which you want to use. Normally, we use // for short comments, and /* */ for
longer.

C++ Variables
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
 string - stores text, such as "Hello World". String values are surrounded by double
quotes
 bool - stores values with two states: true or false
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Declaring (Creating) Variables


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

Syntax
type variable = value;

Where type is one of C++ types (such as int), and variable is the name of the variable (such
as x or myName). The equal sign is used to assign values to the variable.

To create a variable that should store a number, look at the following example:

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

int myNum = 15;


cout << myNum;

You can also declare a variable without assigning the value, and assign the value later:

Example
int myNum;
myNum = 15;
cout << myNum;
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Note that if you assign a new value to an existing variable, it will overwrite the previous
value:

Example
int myNum = 15;  // myNum is 15
myNum = 10;  // Now myNum is 10
cout << myNum;  // Outputs 10

Other Types
A demonstration of other data types:

Example
int myNum = 5;               // Integer (whole number without decimals)
double myFloatNum = 5.99;    // Floating point number (with decimals)
char myLetter = 'D';         // Character
string myText = "Hello";     // String (text)
bool myBoolean = true;       // Boolean (true or false)

Display Variables
The cout object is used together with the << operator to display variables.

To combine both text and a variable, separate them with the << operator:

Example
int myAge = 35;
cout << "I am " << myAge << " years old.";
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Add Variables Together


To add a variable to another variable, you can use the + operator:

Example
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;

Exercise 2:
1. Create a variable named myNum and assign the value 50 to it.

=
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

2. Display the sum of 5 + 10, using two variables: x and y.

3. Create a variable called z, assign x + y to it, and display the result.

4. Fill in the missing parts to create three variables of the same type, using a comma-
separated list:
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

C++ Declare Multiple Variables

Declare Many Variables


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

Example
int x = 5, y = 6, z = 50;
cout << x + y + z;

C++ Identifiers
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).

Note: It is recommended to use descriptive names in order to create understandable and


maintainable code:

Example
// Good
int minutesPerHour = 60;

// OK, but not so easy to understand what m actually is


int m = 60;
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

The general rules for constructing names for variables (unique identifiers) are:

 Names can contain letters, digits and underscores


 Names must begin with a letter or an underscore (_)
 Names are case sensitive (myVar and myvar are different variables)
 Names cannot contain whitespaces or special characters like !, #, %, etc.
 Reserved words (like C++ keywords, such as int) cannot be used as names

Constants
When you do not want others (or yourself) to override existing variable values, use the const
keyword (this will declare the variable as "constant", which means unchangeable and
read-only):

Example
const int myNum = 15;  // myNum will always be 15
myNum = 10;  // error: assignment of read-only variable 'myNum'
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

You should always declare the variable as constant when you have values that are unlikely
to change:

Example
const int minutesPerHour = 60;
const float PI = 3.14;

C++ User Input


You have already learned that cout is used to output (print) values. Now we will use cin to
get user input.

cin is a predefined variable that reads data from the keyboard with the extraction operator
(>>).

In the following example, the user can input a number, which is stored in the variable x.
Then we print the value of x:

Example
int x; 
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Creating a Simple Calculator


User must input two numbers. Then print the sum by calculating (adding) the two numbers:

int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Exercise 3:
1. Use the correct keyword to get user input, stored in the variable x:

int x;
cout << "Type a number: ";
>> ;

2. Fill in the missing parts to print the sum of two numbers (which is put in by the user):

int x, y;
int sum;
cout << "Type a number: ";
>> ;
cout << "Type another number: ";
>> ;
sum = x + y;
cout << "Sum is: " << ;

C++ Data Types


As explained in the Variables chapter, a variable in C++ must be a specified data type:

Example
int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99;     // Floating point number
double myDoubleNum = 9.98;   // Floating point number
char myLetter = 'D';         // Character
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

bool myBoolean = true;       // Boolean


string myText = "Hello";     // String

Basic Data Types


The data type specifies the size and type of information the variable will store:

Data Type Size Description

int 4 bytes Stores whole numbers, without decimals

float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 7
decimal digits

double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 15
decimal digits

boolean 1 byte Stores true or false values

char 1 byte Stores a single character/letter/number, or ASCII values


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Numeric Types
Use int when you need to store a whole number without decimals, like 35 or 1000, and float
or double when you need a floating point number (with decimals), like 9.99 or 3.14515.

int
int myNum = 1000;
cout << myNum;

float
float myNum = 5.75;
cout << myNum;

double
double myNum = 19.99;
cout << myNum;
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

float vs. double

The precision of a floating point value indicates how many digits the value can have after
the decimal point. The precision of float is only six or seven decimal digits, while double
variables have a precision of about 15 digits. Therefore it is safer to use double for most
calculations.

Scientific Numbers
A floating point number can also be a scientific number with an "e" to indicate the power of
10:

Example
float f1 = 35e3;
double d1 = 12E4;
cout << f1;
cout << d1;

Boolean Types
A boolean data type is declared with the bool keyword and can only take the values true or
false. When the value is returned, true = 1 and false = 0.
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Boolean values are mostly used for conditional testing

Example
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun;  // Outputs 1 (true)
cout << isFishTasty;  // Outputs 0 (false)

Character Types
The char data type is used to store a single character. The character must be surrounded by
single quotes, like 'A' or 'c':

Example
char myGrade = 'B';
cout << myGrade;

Example
char a = 65, b = 66, c = 67;
cout << a;
cout << b;
cout << c;
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

String Types
The string type is used to store a sequence of characters (text). This is not a built-in type,
but it behaves like one in its most basic usage. String values must be surrounded by double
quotes:

Example
string greeting = "Hello";
cout << greeting;

To use strings, you must include an additional header file in the source code, the <string>
library:

Example
// Include the string library
#include <string>

// Create a string variable


string greeting = "Hello";

// Output string value


cout << greeting;
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

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;

Although the + operator is often used to add together two values, like in the example above,
it can also be used to add together a variable and a value, or a variable and another
variable:

Example
int sum1 = 100 + 50;        // 150 (100 + 50)
int sum2 = sum1 + 250;      // 400 (150 + 250)
int sum3 = sum2 + sum2;     // 800 (400 + 400)

Example
int sum1 = 100 + 50;        // 150 (100 + 50)
int sum2 = sum1 + 250;      // 400 (150 + 250)
int sum3 = sum2 + sum2;     // 800 (400 + 400)
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.

Assignment Operators
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;

The addition assignment operator (+=) adds a value to a variable:

Example
int x = 10;
x += 5;

A list of all assignment operators:


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Comparison Operators
Comparison operators are used to compare two values.

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

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
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

A list of all comparison operators:

Logical Operators
Logical operators are used to determine the logic between variables or values:
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Exercises 4:
1. Multiply 10 with 5, and print the result.

cout << 10 5;

2. Divide 10 by 5, and print the result.

cout << 10 5;

3. Use the correct operator to increase the value of the variable x by 1.

int x = 10;
x;

4. Use the addition assignment operator to add the value 5 to the variable x.

C++ Strings
Strings are used for storing text.

A string variable contains a collection of characters surrounded by double quotes:

Example
Create a variable of type string and assign it a value:

string greeting = "Hello";

Note: To use strings, you must include an additional header file in the source code,
the <string> library:

Example
// Include the string library
#include <string>
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

// Create a string variable


string greeting = "Hello";

String Concatenation
The + operator can be used between strings to add them together to make a new string.
This is called concatenation:

Example
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;

Note: we added a space after firstName to create a space between John and Doe on
output. However, you could also add a space with quotes ( " " or ' '):

Example
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
cout << fullName;
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Append
A string in C++ is actually an object, which contain functions that can perform certain
operations on strings. For example, you can also concatenate strings with the append()
function:

Example
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName.append(lastName);
cout << fullName;

Note: It is up to you whether you want to use + or append(). The major difference
between the two, is that the append() function is much faster. However, for testing
and such, it might be easier to just use +.

Adding Numbers and Strings


WARNING!

C++ uses the + operator for both addition and concatenation.


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Numbers are added. Strings are concatenated.

If you add two numbers, the result will be a number:

Example
int x = 10;
int y = 20;
int z = x + y;      // z will be 30 (an integer)

If you add two strings, the result will be a string concatenation:

Example
string x = "10";
string y = "20";
string z = x + y;   // z will be 1020 (a string)

Note: If you try to add a number to a string, an error occurs:

Example
string x = "10";
int y = 20;
string z = x + y;
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

String Length
To get the length of a string, use the length() function:

Example
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length();

Tip: You might see some C++ programs that use the size() function to get the length of a
string. This is just an alias of length(). It is completely up to you if you want to use length()
or size():

Example
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.size();

Access Strings
You can access the characters in a string by referring to its index number inside square
brackets [].

This example prints the first character in myString:


Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Example
string myString = "Hello";
cout << myString[0];
// Outputs H

Note: String indexes start with 0: [0] is the first character. [1] is the second character, etc.

This example prints the second character in myString:

Example
string myString = "Hello";
cout << myString[1];
// Outputs e

Change String Characters


To change the value of a specific character in a string, refer to the index number, and use
single quotes:

Example
string myString = "Hello";
myString[0] = 'J';
cout << myString;
// Outputs Jello instead of Hello
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

User Input Strings


It is possible to use the extraction operator >> on cin to display a string entered by a user:

Example
string firstName;
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Your name is: " << firstName;

// Type your first name: John


// Your name is: John

However, cin considers a space (whitespace, tabs, etc) as a terminating character, which
means that it can only display a single word (even if you type many words):

Example
string fullName;
cout << "Type your full name: ";
cin >> fullName;
cout << "Your name is: " << fullName;

// Type your full name: John Doe


// Your name is: John

From the example above, you would expect the program to print "John Doe", but it only
prints "John".

That's why, when working with strings, we often use the getline() function to read a line of
text. It takes cin as the first parameter, and the string variable as second:

Example
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

// Type your full name: John Doe


// Your name is: John Doe

Omitting Namespace
You might see some C++ programs that runs without the standard namespace library. The
using namespace std line can be omitted and replaced with the std keyword, followed by the ::
operator for string (and cout) objects:

Example
#include <iostream>
#include <string>

int main() {
  std::string greeting = "Hello";
  std::cout << greeting;
  return 0;
}
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

C++ Math
C++ has many functions that allows you to perform mathematical tasks on numbers.

Max and min


The max(x,y) function can be used to find the highest value of x and y:

Example
cout << max(5, 10);

And the min(x,y) function can be used to find the lowest value of x and y:

Example
cout << min(5, 10);

C++ <cmath> Header


Other functions, such as sqrt (square root), round (rounds a number) and log (natural
logarithm), can be found in the <cmath> header file:

Example
// Include the cmath library
#include <cmath>
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

cout << sqrt(64);


cout << round(2.6);
cout << log(2);

Other Math Functions


A list of other popular Math functions (from the <cmath> library) :
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

C++ Booleans
Very often, in programming, you will need a data type that can only have one of two values,
like:

 YES / NO
 ON / OFF
 TRUE / FALSE

For this, C++ has a bool data type, which can take the values true (1) or false (0).

Boolean Values
A boolean variable is declared with the bool keyword and can only take the values true or
false:

Example
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun;  // Outputs 1 (true)
cout << isFishTasty;  // Outputs 0 (false)

From the example above, you can read that a true value returns 1, and false returns 0.
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Boolean Expression
A Boolean expression is a C++ expression that returns a boolean value: 1 (true) or 0
(false).

Note: Booleans are the basis for all C++ comparisons and conditions.

You can use a comparison operator, such as the greater than (>) operator to find out if an
expression (or a variable) is true:

Example
int x = 10;
int y = 9;
cout << (x > y); // returns 1 (true), because 10 is higher than 9

Or even easier:

Example
cout << (10 > 9); // returns 1 (true), because 10 is higher than 9

In the examples below, we use the equal to (==) operator to evaluate an expression:

Example
int x = 10;
cout << (x == 10);  // returns 1 (true), because the value of x is equal to 10
Republic of the Philippines
CAMARINES SUR POLYTECHNIC COLLEGES
Nabua, Camarines Sur

Example
cout << (10 == 15);  // returns 0 (false), because 10 is not equal to 15

You might also like