Professional Documents
Culture Documents
program
Output Operator
# include <iostream>
This directory causes the pre processor to add the contents of the iostream
file to the program.
It contains declarations for the identifier cout and the operator <<.
Some old versions of C++ uses a header file called as iostream.h
The header file must be included at the beginning of all the program that
contains the input- output statements.
Return type main()
return 0;
}
Tokens
Constants refer to fixed values that do not change during the execution of the
program
Arithmetic operator
Comparison operator
Assignment operator
Logical operator
Bitwise operators
Arithmetic operator
&& : Called Logical AND operator. If both the operands are non-zero, then
condition becomes true.
|| : Called Logical OR Operator. If any of the two operands is non-zero, then
condition becomes true.
! : Called Logical NOT Operator. Use to reverses the logical state of its operand.
If a condition is true, then Logical NOT operator will make false.
Bitwise operator
& : Binary AND Operator copies a bit to the result if it exists in both operands.
| : Binary OR Operator copies a bit if it exists in either operand.
^ : Binary XOR Operator copies the bit if it is set in one operand but not both.
~ : Binary Ones Complement Operator is unary and has the effect of 'flipping'
bits.
<< : Binary Left Shift Operator. The left operands value is moved left by the
number of bits specified by the right operand.
>> : Binary Right Shift Operator. The left operands value is moved right by the
number of bits specified by the right operand.
Assignment operator
= : Simple assignment operator, Assigns values from right side operands to left
side operand.
+= : Add AND assignment operator, It adds right operand to the left operand
and assign the result to left operand. ( C+=A ; C=C+ A)
-= : Subtract AND assignment operator, It subtracts right operand from the left
operand and assign the result to left operand.
*= : Multiply AND assignment operator, It multiplies right operand with the left
operand and assign the result to left operand.
/= : Divide AND assignment operator, It divides left operand with the right
operand and assign the result to left operand.
% = : Modulus AND assignment operator, It takes modulus using two operands
and assign the result to left operand.
Control Structures
Control Structures are just a way to specify flow of control in programs.
Any algorithm or program can be more clear and understood if they use self-contained
modules called as logic or control structures.
It basically analyses and chooses in which direction a program flows based on certain
parameters or conditions.
One method of achieving the objective of an accurate error resistant and maintainable
code is to use one or any combination of the following three control structures
1. Sequential structure ( straight line)
2. Selection structure ( branching)
3. Loop structure ( iteration or repetition)
Sequential Structure
Sequential Logic (Sequential Flow)Sequential logic as the name suggests follows a
serial or sequential flow in which the flow depends on the series of instructions given to
the computer.
Unless new instructions are given, the modules are executed in the obvious sequence.
The sequences may be given, by means of numbered steps explicitly. Also, implicitly
follows the order in which modules are written.
Most of the processing, even some complex problems, will generally follow this
elementary flow pattern.
Sequential structure
Selection structures are implemented using If , If Else and Switch statements.
Looping structures are implemented using While, Do
While and For statements.
The simple If statement
if (condition) statement
where condition is the expression that is being evaluated. If this condition
is true, statement is executed.
If it is false, statement is ignored (not executed) and the program continues on the
next instruction after the conditional structure.
For example, the following code fragment prints out x is 100 only if the value stored in
variable x is indeed 100:
if (x == 100)
cout << "x is 100";
If- else
We can additionally specify what we want that happens if the condition is not fulfilled by
using the keyword else.
Syntax:
if (condition)
statement1
else
statement2
For example:
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
While loop
Example:
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
continue;
}
cout << i << "\n";
}
Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
To declare an array, define the variable type, specify the name of the array followed
by square brackets and specify the number of elements it should store:
string fruits[4];
Example:
string fruits[4] = {“Apple", “Mango", “Orange", “Grapes"};
// Outputs Apple
{
cin>>arr[i];
}
Multidimensional Array
In C/C++, we can define multidimensional arrays in simple words as array of arrays.
data_type array_name[size1][size2]....[sizeN];
data_type: Type of data to be stored in the array. Here data_type is valid C/C++ data
type
array_name: Name of the array
size1, size2,... ,sizeN: Sizes of the dimensions
Two-Dimensional Arrays
The simplest form of the multidimensional array is the two-dimensional array.
A two-dimensional array is, in essence, a list of one-dimensional arrays.
type arrayName [ x ][ y ];
Where type can be any valid C++ data type and arrayName will be a valid C++ identifier.