Jump statements are used to interrupt the normal flow of a program. Jump statements transfers controls from one point to another point in the program due to some specified code while executing.

There are three jump statements in c++

  1. Break
  2. Continue
  3. Goto

Break  Statement 

Break statement is used to break the flow of loop. When break statement is found inside a loop the loop is immediately exited and program continues with other statement below the loop.

When the loops are nested, the break would only exit from the loop containing it.

Example:

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

{

      cout<<i;

      break;

}

Output:

1

Continue Statement

The continue statement breaks one iteration in a loop if a specified condition occurs and continue with the next iteration.

Example:

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

{

       if(i==5)

           continue;

           cout<<i;

}

output:

01234678910

Goto Statement

It is used to transfer control to some other part of a program.

Syntax:

label_1_name:

     statement;

label_2_name

     statement;

goto label_name;

Here,

Label is a identifier, when goto lbl_name is found the control is jumps to lbl_name and execute code below it.

Example:

  label :

cout<<"hello";

cout<<"Do you want to try again?";

cin>>i;

if(i=="y")

        goto label;


Loop means iteration. In c++ loops are used to repeat certain block of code until a condition is satisfied. 

In c++ we use three loop statements:

  1. while
  2. do-while
  3. for

While Loop

While Loop is a condition based and entry controlled (means the condition is tested before entering the loop body) loop. It repeats a group of statements while a given condition is true.

Syntax

    while(condition) 
    {
       statement;
    }
Example
    a=0; 
    While(a<=10)
    {
        cout<<a;
        a++;
    }

do-while 

do-while Loop is also a condition based but exit controlled (means the condition is tested at the bottom of the loop) loop. It repeats a group of statements while a given condition is true. 

But the major difference between while and do-while loop is that, do while loop is guaranteed to execute at least one time.

Syntax

do 

{
   statement;
}
while( condition );

Example

a=0;

do

{

   cout<<a;

   a++;

}

while(a<=0);

For Loop

This is range based and entry controlled loop that provide a simple loop structure.

Syntax

for(initialization;condition;inc/dec)

{

      statements;

}

Here:

Initialization: The initialization statement is executed only once. Initialize variable using assignment statement such as i=1.

Condition: This is an expression that returns either true or false. If it returns true the body of for loop executed otherwise the loop is terminated.

Update(inc/dec): Update the value of initialized variable and check the condition. It is also done using assignment operators such as i=i+1 or i++.

Example

int i;

for(i=0;i<=10;i++)
{
      cout<<i;
}

Decision making statements are those statements which are used to write conditions or decisions to control the flow of program.

Ex: if-statement, switch, while etc.

if - statement

if-statement is simple decision making statement. It is used to control the flow and execute any particular statement depending on the logical condition.

Syntax

if(condition)
{
      statements;
}
 
Example
if(a>b)
{
      cout<<a;
}
 
In above example, if the condition (a>b) returns true then block of statements (inside the curly braces) will execute otherwise not. 
 

if - else statement

In if-statement the block of code will execute if condition is true but what if when we want to execute some code if condition is false. Here else comes. 

else-statement is always used with if-statement if we want to execute block of code when condition is false. else runs on same condition of if-statement so it doesn't require any condition.

syntax

if(condition)
{
    statement;  //execute if condition is true
}
else
{
    statement; //execute if condition is false
}

Example

if(a>b)
{
     cout<<a<<" is greater number";
}
else
{
     cout<<b<<" is greater number;

Nested if

When if statement is used inside another if or else statement, then this is called nested if statement.
 
Syntax
if(condition1)
{
    if(condition2)
    {
         statement;
    }
    else
    {
         statement;
    }
}
else
{
      statement;
}
 
Example
if(a>b)
{
     if(a>c)
    {
        cout<<a<<" is greater no";
    }
    else
    {
         cout<<c<<" is greater no";
    }
}
else
{
     if(b>c)
    {
        cout<<b<<" is greater no";
    }
    else
    {
         cout<<c<<" is greater no";
    }
}
         

else if

else if is just like another if condition in a program. It is used when we have multiple decisions. These codes are executed from top to bottom. As soon as one condition is true the statements associated with that if is executed and rest of the else-if statement is skipped.

Syntax
if(condition)
{
      statement;
}
else if(condition)
{
    statement;
}
....
....
else
{
    statement;
}
 
Example
if(age<18)
{
    cout<<"Child";
}
else if(age<40)
{
    cout<<"Young";
}
else
{
     cout<<"old";
}
 

 

 
 
 

In C++ Operators can be of many types some commonly used operators are given below

Arithmetic Operators

 Arithmetic operators are used to perform mathematical operations

  1. + (Addition or Unary plus)
  2. - (Subtraction or Unary minus)
  3. * (Multiplication)
  4. / (Division)
  5. % (Modulo Division)

Ex: If a and b are integers, then for a=24 and b=9  the results will be:

a+b=33

a-b= 15

a*b=216

a/b=2

a%b= 6

Relational Operators

These operators are used to compare two values and the result given by relational expression is either true or false.

  1. < (Less Than)
  2. <= (Less Than or Equal to)
  3. > (Greater Than)
  4. >= (Greater Than or Equal to)
  5. == (Equal to)
  6. != (Not equal to)

Ex: 10>5 TRUE,  10<=10 TRUE, 3<1 FALSE etc.

Relational expressions are used in decision making statements such as if and while.

Logical Operators

Logical operators used to add two or more relational expression together to construct more complex decision making expression. There are three logical operators:

  1. && (Logical AND): exp1 && exp2 - This will return true iff both expressions will be true.
  2. || (Logical OR): exp1 || exp2 - This will return true either exp1 or exp2 will be true.
  3. ! (Logical NOT): !exp - Reverse the result, returns false if the result is true.

These are too used in decision statements. 

Assignment Operators

C++ offers "=" as assignment operator to assign a value to an identifier. C++ also offers some shorthand assignment operators to simplify the assignment statement.

  1. += :- Ex- a+=b; (This is equivalent to a=a+b)
  2. -= :- Ex- a-=b; (This is equivalent to a=a-b)
  3. *= :- Ex- a*=b; (This is equivalent to a=a*b)
  4. /= :- Ex- a/=b; (This is equivalent to a=a/b)
  5. %= :- Ex- a%=b; (This is equivalent to a=a%b)

Increment and Decrement Operator

The increment operator (++) adds 1 to its operand and decrement operator (--) subtract 1 from its operand. In other words 

a=a+1 is same as ++a or a++

a=a-1; is same as --a or a--

Pre-Increment (Prefix ++)  and Post-Increment (Postfix ++)

In the Pre-Increment, value is first incremented and then used inside the expression. Whereas in the Post-Increment, value is first used inside the expression and then incremented. 

Ex: The value of b if a=10.

Postfix- b=a++; //b=10

Prefix- b=++a; //b=11

Pre-Decrement (Prefix --)  and Post-Decrement (Postfix --)

In the Pre-Decrement, value is first decremented and then used inside the expression. Whereas in the Post-Decrement, value is first used inside the expression and then decremented. 

Ex: The value of b if a=10.

Postfix- b=a--; //b=10

Prefix- b=--a; //b=9

Conditional Operator or Ternary Operator

The form of conditional operator is:
exp1 ? exp2 : exp3
Where exp1 is logical condition which is either true or false. If exp1 returns true, then the value of whole expression is the value of exp2. Otherwise, the value of whole expression is the value of exp3. 
For Ex: greater=a>b?a:b (where a and b are integer variables)
Here, if exp1 i.e. a>b is true the value of greater is a (greater=a) otherwise the value of greater is b (greater=b).


Basic Structure:
#include<header file> // Preprocessor Directive
void main()    //Program execution starting point
 {
     statement;
 } 

Example:
#include<iostream.h>
#include<conio.h>
void main()
{
      cout<<"hello";
}
 

Preprocessor Directive

Before a C++ program is compiled in a compiler, source code is processed by preprocessor. Commands used in preprocessor are called preprocessor directives and they begin with “#” symbol.
Ex: #include, #define etc.

Header File

A header file is a file with extension ".h" and contains some declarations of library functions. We use these header files in program by using preprocessor "#include".
Ex: #include<iostream.h>, #include<conio.h> etc.

main()

The main() function is the point from where all c++ programs start their execution.

Input Output (I/O) operations In C++ 

Input & Output operations are supported by the istream (input stream) and ostream (output stream) classes.
The predefined stream objects for input, output are :
(i) The cout Object:- The cout is a predefined object of ostream class that represents the standered output stream. cout stands for console output. cout sends all output to the standard output device i.e. monitor by using output operator (<<).
Syntax:- cout<< data; (Where data may be a variable or constant or string etc.)
Ex: cout<< avg;
       cout<<"Hello world";

(ii) The cin Object :- The cin object is an istream class object. cin stands for console input. cin object used to get input from the keyboard by using input operator(>>). When a program reaches the line with cin, the user at the keyboard can enter values directly into variables.
Syntax: cin>> variablename;
Ex: cin>> ch; ( here ch can be any variable)
 
Note: To use both cin and cout in a program a header file 'iostream.h' (input output stream) need to be include.

 

 

Variable

A variable is an identifier that denotes a storage location, which content can be varied during program execution. A variable is a character or group of characters and It is used to store data. Example: Total, Emp_no, avg etc.

Data Types 

Data Type means type of data that variable can store. We use data types during declaration of variable or constant.
In C++ There are three types of data types:
1) Primary or Fundamental or Built-in Data Types
2) Derived Data Types
3) User-Defined Data Types
 

Primary Data Type

These are built in data types and can be used directly by the user.
  • int: It is used to declare integer variables. Ex- int total;
  • char: It is used to declare character variables. Ex- char c;
  • bool: It is used to declare boolean variables. A boolean variable can store either true or false. Ex- bool a;
  • float: It is used to declare floating point variables. Ex- float avg;
  • double: It is used to declare double precision floating point variables. Ex- double avg;
  • void: It means no value. Void data type is used for functions which does not returns any value. Ex- void avg();

Derived Data Type

The data-types that are derived from primary data types are known as Derived Data Types. 
  • Array
  • Function
  • Pointer

User Defined Data Type

These data types defined by user itself.
  • Structure
  • Class

Data Type Modifier

Modifiers are used to modify the size of a data type. 4 modifiers in C++ are Signed, Unsigned, Long and Short.

The modifiers signed, unsigned, long, and short can be applied to integer types. signed and unsigned can be applied to char, and long can be applied to double.

The modifiers signed and unsigned can also be used as prefix to long or short modifiers. For example, unsigned long int.

 


C++ is Object oriented programing language. It was developed at AT & T Bell laboratories in the early 1980 in USA by Bjarne stroustrup. C++ derived from the C language . The most important elements added to C to create C++ are concerned with Class and Object. Since the Class was a major addition to the original C language.

c++ Introduction, Cpp Introduction, Cpp Keywords, charecter set, token

 

C++ Character Set

Letters: A-Z, a-z 
Digits: 0-9
Special Symbols: Space + - * / % ^ \ () [] {} =! < >. " $ , ; : & ? _ # @
White Spaces: Blank Spaces, New Lines, Tabs
Other Characters: Any of the 256 ASCII Characters
 

Token

The smallest individual unit in a program is known as token. Tokens used in C++ are: Keywords, Identifiers, Constants, Operators
 
Keyword:  Keywords are reserve words that have some meaning and these meanings can not be changed. All keywords must be written in lowercase.
Cpp Keywords list

 
 
Identifier: Identifiers are user-defined names of variables, functions and arrays.
     Rules for Identifiers
1) 1st character must be an alphabet or underscore.
2)  Must consist of only letters, digits, or underscore.
3)  Cannot use a keyword.
4)  Must not contain white space. 
 

Constant: Constants are the fixed values that do not change during the execution of a program. 

 i) Integer Constant :- The numbers which has not any fractional part.

Ex : 20, -65, 256, 50 etc.

ii) Floating Point Constant :- The numbers which has integer part as well as fractional part.

Ex:- 12.5, -2.6, 0.65, 0.0 etc.

iii) Character Constant :- A character constant is a single alphabet, a single digit or a single special symbol enclosed within pair of single quotes. 

Ex :- 'a', '5', '?', '#' etc.

iv) String Constant :- A string constant is a character or group (sequence) of characters enclosed within pair of double quotes.
Ex:- "a", "5", "ab", "54", "?", "Hello", "Today is Monday" etc.

v) Backslash Character Constant :- These are used in output functions.

 

        '\a'    -    Alert or Alarm or Bell

        '\b'    -    Backspace

        '\f'     -    Form feed

        '\n'    -    New Line

        '\r'    -    Carriage return

        '\t'    -    Horizontal Tab

        '\v'    -    Vertical Tab

        '\''    -    Single Quote

        '\"'    -    Double Quote

        '\?'    -    Question Mark

        '\\'    -    Backslash    

        '\0'    -    Null

Const: A const keyword is used to define a constant that value can not be changed.

Ex: const float pi=3.14;

Operators :- Operator is a symbol that is used to perform some mathematical and logical operations.

EX: +, -, *, /, <, > etc.