Saturday, 16 September 2017

Conditional selection - switch

Conditional selection - switch

A switch statement is used instead of nested if...else statements. It is multiple branch decision statement of C++. A switch statement tests a variable with list of values for equivalence. Each value is called a case.

The case value must be a constant integer.

Structure of switch() statement

switch (expression)
  case value: statements...
  case value: statements...
  default : statements...

Individual case keyword and a semi-colon (:) is used for each constant. Switch tool is used for skipping to particular case, after jumping to that case it will execute all statements from cases beneath that case this is called as ''Fall Through''.

In the example below, for example, if the value 2 is entered, then the program will print two one something else!

int main()
  int i;
  cout << ''Enter an integer: '';
    case 4: cout << ''four''; break;
    case 3: cout << ''three''; break;
    case 2: cout << ''two '';break;
    case 1: cout << ''one '';break;
    default: cout << ''something else!'';
  return 0;

To avoid fall through, the break statements are necessary to exit the switch. If value 4 is entered, then in case 4 it will just print four and ends the switch.

The default label is non-compulsory, It is used for cases that are not present.

Control flow if statement in c++

if statement

An if statement contains a Boolean expression and block of statements enclosed within braces.

Structure of if statement
if (boolean expression )
/* if expression is true */
statements... ; /* Execute statements */

If the Boolean expression is true then statement block is executed otherwise (if false) program directly goes to next statement without executing Statement block.

if else statement

If statement block with else statement is known as as if...else statement. Else portion is non-compulsory.

Structure of if...else


If the condition is true, then compiler will execute the if block of statements, if false then else block of statements will be executed.

Nested if...else statement

We can use multiple if-else for one inside other this is called as Nested if-else.

Structure of Nested if...else

else if

Friday, 15 September 2017

Input and Output

 Input Output

cout (output stream)

On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. cout is an instance of iostream class

For formatted output operations, cout is used together with the insertion operator, which is written as << (i.e., two "less than" signs).

cout << "this is Output";
// prints this is Output sentence on screen
cout << 50;
// prints number 50 on screen
cout << x;
// prints the value of x on screen

The << operator inserts the data that follows it into the stream that precedes it. In the examples above, it inserted the literal string Output sentence, the number 120, and the value of variable x into the standard output stream cout. Notice that the sentence in the first statement is enclosed in double quotes (") because it is a string literal, while in the last one, x is not. The double quoting is what makes the difference; when the text is enclosed between them, the text is printed literally; when they are not, the text is interpreted as the identifier of a variable, and its value is printed instead.

For example, these two sentences have very different results:
cout << "Hello"; // prints Hello
cout << Hello; // prints the content of variable Hello

Multiple insertion operations (<<) may be chained in a single statement:
cout << "This " << " is a " << "single C++ statement";

This last statement would print the text This is a single C++ statement. Chaining insertions is especially useful to mix literals and variables in a single statement:
cout << "I am " << age << " years old and my zipcode is " << zipcode;

cin (input stream)

In most program environments, the standard input by default is the keyboard, and the C++ stream object defined to access it is cin. cin is an instance of iostream class

For formatted input operations, cin is used together with the extraction operator, which is written as >> (i.e., two "greater than" signs). This operator is then followed by the variable where the extracted data is stored. For example:
int age;
//declares a variable of type int called age
cin >> age;
//extracts a value to be stored in it

This operation makes the program wait for input from cin; generally, this means that the program will wait for the user to enter some sequence with the keyboard.

Extractions on cin can also be chained to request more than one datum in a single statement:
cin >> a >> b;

This is equivalent to:
cin >> a;
cin >> b;
In both cases, the user is expected to introduce two values, one for variable a, and another for variable b. Any kind of space is used to separate two consecutive input operations; this may either be a space, a tab, or a new-line character.

What is Operators types in c++


An operator is a symbol. Compiler identifies Operator and performs specific mathematical or logical operation. C provides following operators : 

 Arithmetic Operators
 Logical Operators
 Increment and Decrement Operators
 Relational Operators
 Cast Operators
 Bitwise Operators
 Assignment Operators

Arithmetic Operators

* multiplication
/ division
% remainder after division (modulo arithmetic)
+ addition
- subtraction and unary minus

Logical Operators

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

Increment and Decrement Operators
Increment and decrement operators are used to add or subtract 1 from the current value of oprand.

++ increment
-- decrement

Increment and Decrement operators can be prefix or postfix. In the prefix style the value of oprand is changed before the result of expression and in the postfix style the variable is modified after result.

For eg.
a = 9;
b = a++ + 5; /* a=10 b=14 */
a = 9;
b = ++a + 5; /* a=10 b=15 */

Relational Operators

== equal.
!= Not equal.
> < Greater than/less than
>= greater than or equal to 
<= less than or equal to 

Cast Operators

Cast operators are used to convert a value from one to another type.
(float) sum;   converts type to float
(int) fred;   converts type to int

Bitwise Operators 

Bitwise operators performs operation on actual bits present in each byte of a variable. Each byte contain 8 bits, each bit can store the value 0 or 1 

~ one's complement

& bitwise AND
^ bitwise XOR
| bitwise OR
<< left shift (binary multiply by 2)
>> right shift (binary divide by 2)

Assignment Operators

= assign
+= assign with add
-= assign with subtract
*= assign with multiply
/= assign with divide
%= assign with remainder
>>= assign with right shift
<<= assign with left shift
&= assign with bitwise AND 
^= assign with bitwise XOR
|= assign with bitwise OR

For example,
a = a + 64; is same as 
a += 64;


The sizeof operator returns the size, in bytes, of a type or a variable.
You can compile and run the following program to find out how large your data types are:
cout << "bool:\t\t" << sizeof(bool) << " bytes";
bool: 1 bytes

Condition ? X : Y

Condition operator: If Condition is true ? then it returns value X : otherwise value Y

Comma operator causes a sequence of operations to be performed. The value of the entire comma expression is the value of the last expression of the comma-separated list

. (dot) and -> (arrow)

Member operators are used to reference individual members of classes, structures, and unions.

& Pointer operator: & returns the address of an variable. For example &a; will give actual address of the variable.

* Pointer operator: * is pointer to a variable. For example *var; will pointer to a variable var.

What is Variable Storage and Classes in c++

Variable Storage Classes


The default class. Automatic variables are local to their block. Their storage space is reclaimed on exit from the block.


If possible, the variable will be stored in a processor register. May give faster access to the variable. If register storage is not possible, then the variable will be of automatic class.
Use of the register class is not recommended, as the compiler should be able to make better judgement about which variables to hold in registers, in fact injudicious use of register variables may slow down the program.


On exit from block, static variables are not reclaimed. They keep their value. On re-entry to the block the variable will have its old value.


Allows access to external variables. An external variable is either a global variable or a variable defined in another source file. External variables are defined outside of any function.
 Note: Variables passed to a function and modified by way of a pointer are not external variables

static external 

External variables can be accessed by any function in any source file which make up the final program. Static external variables can only be accessed by functions in the same file as the variable declaration.

Thursday, 14 September 2017

What is Variables of c++


A variable in C++ is a name for a piece of memory that can be used to store information.
There are many types of variables, which determines the size and layout of the variable's memory;

Variable Names

  we can use any combination of letters and numbers for Variable and function names but it must start with a letter.
We can use Underscore (_) as a letter in variable name and can begin with an underscore But Identifiers beginning with an underscore are reserved, And identifiers beginning with an underscore followed by a lower case letter are reserved for file scope identifiers Therefore using underscore as starting letter is not desirable.

Dil and dil are different identifiers because upper and lower case letters are treated as different identifiers

Variable Types

There are many 'built-in' data types in C.

short int -128 to 127 (1 byte)

unsigned short int 0 to 255 (1 byte)

char 0 to 255 or -128 to +127 (1 byte)

unsigned char 0 to 255 (1 byte)

signed char -128 to 127 (1 byte)

int -32,768 to +32,767 (2 bytes)

unsigned int 0 to +65,535 (2 bytes)

long int -2,147,483,648 to +2,147,483,647 (4 bytes)

unsigned long int 0 to 4,294,967,295 (4 bytes)

float single precision floating point (4 bytes)

double double precision floating point (8 bytes)

long double extended precision floating point (10 bytes)

Definition, Declaration & Initialization

Definition is the place where variable is created (allocated storage).

Declaration is a place where nature (type) of variable is stated, but no storage is allocated.

Initialization means assigning a value to the variable.

Variables can be declared many times, but defined only once. Memory space is not allocated for a variable while declaration. It happens only on variable definition.

Variable declaration

data_type variable_name;

int a, b, c;
char flag;

Variable initialization

data_type variable_name = value;

int a = 50;
char flag = 't';

external and static
initialisation done once only.

auto and register
initialisation done each time block is entered.

external and static variables cannot be initialised with a value that is not known until run-time; the initialiser must be a constant expression.

A variable that has not been assigned a value is called an uninitialized variable. Uninitialized variables are very dangerous because they cause intermittent problems (due to having different values each time you run the program). This can make them very hard to debug.

Variable scope

refers to where variables is declared.

It can be Inside a function or a block which is called local variables, In the definition of function parameters which is called formal parameters or Outside of all functions which is called global variables.

Global variables

Global variable are declared outside any functions, usually at top of program. they can be used by later blocks of code:
int g; //global
int main(void)
g = 0;

Local variables

Variables that are declared inside a function or block are local variables. The scope of local variables will be within the function only. These variables are declared within the function and can't be accessed outside the function.
void main()
int g; //local
cout << g;

Structure and Syntax of program in c++

 Structure and Syntax of program

c++ Program | Syntax 

This Program we show prints Hello World on screen 

#include <iostream.h>
using namespace std;
int main ()
cout << "Hello World! Syntax ";
return 0;

1 . /* This program ... */
The symbols/* and*/ used for comment. This Comments are ignored by the compiler, and are used to provide useful information about program to humans who use it.

2. #include <iostream.h>

This is a preprocessor command which tells compiler to include iostream.h file.

3. using namespace std; 

All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare Syntax with this expression that we will be using these entities. This line is very frequent in C++ programs Syntax that use the standard library.

4. main()

C++ programs consist of one or more functions. There must be one and only one function called main. The brackets following the word main indicate that it is a function and not a variable .

5. { }
Syntax braces surround the body of the function, which may havee one or more instructions/statements.

6. Cout<<

it is a library function that is used to print data on the user screen.

7. ''Hello World'' is a string that will be displayed on user screen 

8. ; a semicolon ends a statement Syntax .

9. return 0; return the value zero to the Operating system.

Wednesday, 13 September 2017

What is c++

 Introduction of c++

C++ is a general-purpose programming language that supports procedural, and it is object oriented programming language
C++ is consider as a middle-level language,  it comprises a included  of both high-level and low-level language.

C++ was developed by Bjarne Stroustrup of AT&T Bell Laboratories in the early 1980's, and is based on the C language. The "++" is a syntactic construct used in C (to increment a variable), and C++ is earmarketed as an incremental improvement of C. Most of C is a subset of C++, so that most C programs can be compiled (i.e. converted into a series of low-level instructions that the computer can execute directly) using a C++ compiler. 
C++ is a superset of C, and that virtually any legal C program is a legal C++ program.

Object-Oriented Programming

C++ fully supports object-oriented programming, including the four pillars of object-oriented development:

1- Encapsulation
2- Data hiding
3- Inheritance
4- Polymorphism

Use of C++

C++ is used by hundreds of thousands of programmers in essentially every application domain.

- In Adobe Systems All major applications are developed in C++: Photoshop & ImageReady, Illustrator, Acrobat, InDesign, GoLive

C++ is widely used for teaching and research because it is clean enough for successful teaching of basic concepts.

Anyone who has used either an Apple Macintosh or a PC running Windows has indirectly used C++ because the primary user interfaces of these systems are written in C++., Facebook, Google, HP, IBM, Microsoft, Mozilla, Nokia & many more companies uses C++ language.

1.2 Advantages And Disadvantages


1. vendor-neutral: the C++ standard is the same in any platform or compiler
2. industrial (as opposed to academic): evolved to satisfy the needs of software engineers, not computer scientists

3. efficient. Compiles into highly optimized CPU-specific machine code with little or no runtime overhead.

4. multi-paradigm: allows the use and penalty-free mixing of procedural, OOP, generic programming, functional programming, etc

5. strictly statically typed a large amount of logic (and sometimes even calculations) can be proved and performed at compile time, by the type checking/inferring system.

6. has deterministic memory management (as opposed to Java, C#, and other languages with garbage collectors): the life time of every object is known with absolute precision, which makes destructors useful and RAII possible.


1. very complex! The learning curve is steep and takes a long time to climb, especially for those who know C or C# or other superficially similar languages

2. has the concept of undefined behavior(just like C) -- a large class of errors that neither compiler nor the runtime system is required to diagnose.
3. has some design flaws, although they are largely fixed by boost libraries and the new language standard.

4. lacks network and async I/O, graphics, concurrency, serialization, and many other facilities expected of modern languages, although that is also largely fixed by boost libraries and (as far as concurrency is concerned) the new language standard.