0% found this document useful (0 votes)
42 views

Introduction To C++ 02

There are two main ways to define constants in C++ - using the #define preprocessor or using the const keyword. The #define preprocessor replaces all instances of the identifier with the specified value during compilation. The const keyword declares a constant with a specific type. It is generally better practice to define constants in all capital letters for readability. The document then provides examples demonstrating the use of each approach.

Uploaded by

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

Introduction To C++ 02

There are two main ways to define constants in C++ - using the #define preprocessor or using the const keyword. The #define preprocessor replaces all instances of the identifier with the specified value during compilation. The const keyword declares a constant with a specific type. It is generally better practice to define constants in all capital letters for readability. The document then provides examples demonstrating the use of each approach.

Uploaded by

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

Defining Constants

There are two simple ways in C++ to define constants −

 Using #define preprocessor.

 Using const keyword.

The #define Preprocessor


Following is the form to use #define preprocessor to define a constant −
#define identifier value

Following example explains it in detail −

#include <iostream>

using namespace std;

#define LENGTH 10

#define WIDTH 5

#define NEWLINE '\n'

int main() {

int area;

area = LENGTH * WIDTH;

cout << area;

cout << NEWLINE;

return 0;

When the above code is compiled and executed, it produces the following
result −
50
The const Keyword
You can use const prefix to declare constants with a specific type as follows

const type variable = value;

Following example explains it in detail –

#include <iostream>

using namespace std;

int main() {

const int LENGTH = 10;

const int WIDTH = 5;

const char NEWLINE = '\n';

int area;

area = LENGTH * WIDTH;

cout << area;

cout << NEWLINE;

return 0;

When the above code is compiled and executed, it produces the following
result −
50

Note that it is a good programming practice to define constants in CAPITALS.

Operators in C++
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. C++ is rich in built-in operators and
provide the following types of operators −
 Arithmetic Operators

 Relational Operators

 Logical Operators

 Bitwise Operators

 Assignment Operators

 Misc Operators

This chapter will examine the arithmetic, relational, logical, bitwise,


assignment and other operators one by one.

Arithmetic Operators
There are following arithmetic operators supported by C++ language −

Assume variable A holds 10 and variable B holds 20, then –

Operator Description Example

+ Adds two operands A + B will give 30

- Subtracts second operand from A - B will give -10


the first

* Multiplies both operands A * B will give 200

/ Divides numerator by de- B / A will give 2


numerator

% Modulus Operator and remainder B % A will give 0


of after an integer division

++ Increment operator, increases A++ will give 11


integer value by one
-- Decrement operator, decreases A-- will give 9
integer value by one

Relational Operators
There are following relational operators supported by C++ language

Assume variable A holds 10 and variable B holds 20, then –

Operator Description Example

== Checks if the values of two (A == B) is not true.


operands are equal or not, if yes
then condition becomes true.

!= Checks if the values of two (A != B) is true.


operands are equal or not, if
values are not equal then
condition becomes true.

> Checks if the value of left operand (A > B) is not true.


is greater than the value of right
operand, if yes then condition
becomes true.

< Checks if the value of left operand (A < B) is true.


is less than the value of right
operand, if yes then condition
becomes true.

>= Checks if the value of left operand (A >= B) is not true.


is greater than or equal to the
value of right operand, if yes then
condition becomes true.

<= Checks if the value of left operand (A <= B) is true.


is less than or equal to the value
of right operand, if yes then
condition becomes true.

Logical Operators
There are following logical operators supported by C++ language.

Assume variable A holds 1 and variable B holds 0, then −

Operator Description Example

&& Called Logical AND operator. If (A && B) is false.


both the operands are non-zero,
then condition becomes true.

|| Called Logical OR Operator. If any (A || B) is true.


of the two operands is non-zero,
then condition becomes true.

! Called Logical NOT Operator. Use !(A && B) is true.


to reverses the logical state of its
operand. If a condition is true,
then Logical NOT operator will
make false.

Assignment Operators
There are following assignment operators supported by C++ language –

Operator Description Example

= Simple assignment operator,


C = A + B will assign value of A
Assigns values from right side
+ B into C
operands to left side operand.
+= Add AND assignment operator, It
adds right operand to the left C += A is equivalent to C = C
operand and assign the result to left +A
operand.

-= Subtract AND assignment operator,


It subtracts right operand from the C -= A is equivalent to C = C -
left operand and assign the result to A
left operand.

*= Multiply AND assignment operator,


It multiplies right operand with the C *= A is equivalent to C = C *
left operand and assign the result to A
left operand.

/= Divide AND assignment operator, It


divides left operand with the right C /= A is equivalent to C = C /
operand and assign the result to left A
operand.

%= Modulus AND assignment operator,


It takes modulus using two C %= A is equivalent to C = C
operands and assign the result to %A
left operand.

<<= Left shift AND assignment operator. C <<= 2 is same as C = C <<


2

>>= Right shift AND assignment C >>= 2 is same as C = C >>


operator. 2

&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2

^= Bitwise exclusive OR and


C ^= 2 is same as C = C ^ 2
assignment operator.
|= Bitwise inclusive OR and assignment
C |= 2 is same as C = C | 2
operator.

C++ Loop Types


There may be a situation, when you need to execute a block of code several
number of times. In general, statements are executed sequentially: The first
statement in a function is executed first, followed by the second, and so on.

Programming languages provide various control structures that allow for


more complicated execution paths.

A loop statement allows us to execute a statement or group of statements


multiple times and following is the general from of a loop statement in most
of the programming languages −

Decision making structures require that the programmer specify one or more
conditions to be evaluated or tested by the program, along with a statement
or statements to be executed if the condition is determined to be true, and
optionally, other statements to be executed if the condition is determined to
be false.

Following is the general form of a typical decision making structure found in


most of the programming languages –

if (testExpression)

// statements

The if statement evaluates the test expression inside parenthesis.

If test expression is evaluated to true, statements inside the body of if is executed.

If test expression is evaluated to false, statements inside the body of if is skipped.


How if statement works?

#include <iostream>
using namespace std;

int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;

// checks if the number is positive


if ( number > 0)
{
cout << "You entered a positive integer: " << number << endl;
}

cout << "This statement is always executed.";


return 0;

}
Output 1

Enter an integer: 5

You entered a positive number: 5

This statement is always executed.

Output 2

Enter a number: -5

This statement is always executed.

C++ if...else
The if else executes the codes inside the body of if statement if the test expression is
true and skips the codes inside the body of else.

If the test expression is false, it executes the codes inside the body of else statement
and skips the codes inside the body of if.

How if...else statement works?


Flowchart of if...else

Example 2: C++ if...else Statement


// Program to check whether an integer is positive or negative
// This program considers 0 as positive number

#include <iostream>
using namespace std;

int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;

if ( number >= 0)
{
cout << "You entered a positive integer: " << number << endl;
}

else
{
cout << "You entered a negative integer: " << number << endl;
}

cout << "This line is always printed.";


return 0;
}

Output

Enter an integer: -4

You entered a negative integer: -4.

This line is always printed.

You might also like