Sunday 10 December 2017

What is control structure and how many types are ?

Control structure:

Control structure defines the flow of execution of a program.
There are following control structures:
1.      Sequential
2.      Iteration
3.      Conditional
4.      Functions

           Sequential

The sequential control structure is a type which defines that the flow of execution will in a sequence.

Here is an example to understand it
#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  cout << “Hello world” ;
  getch();
}
The above program is the example of sequential control structure. It means that firstly clrscr(); function will be executed and then cout<< “Hello world” ; statement will be executed and then getch(); function will be executed. It is the normal flow of the execution of a program.

           Iteration

Iteration means repetition. Iteration means repeating a block of statement instead of writing the same statement many times.

Here is an example to understand:
#include<iostream.h>
#include<conio.h>
void main()
{
            clrscr();
            int c=0;
            while(c<=5)
                        cout << “I love Pakistan” << endl ;
            getch();
}
            Output:

                        I love Pakistan
I love Pakistan
I love Pakistan
I love Pakistan
I love Pakistan

The above example is the example of iteration control structure. It will execute until value of c doesn’t reached to 5. Means it repeats cout << “I Love Pakistan” << endl ; five times.
The above output can be generated by the following program.
#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  cout << “I love Pakistan” << endl ;
  cout << “I love Pakistan” << endl ;
  cout << “I love Pakistan” << endl ;
  cout << “I love Pakistan” << endl ;
  cout << “I love Pakistan” << endl ;
  getch();
}
            There are the main types of loops:
1.      while loop
2.      do-while loop
3.      for loop
Later we will discuss about the types of loop in details.

            Conditional

Conditional control structure defines the flow of execution of a program in a condition.
Here is an example to understand the conditional control structure.
#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int marks;
  cout<< “Enter marks: ” ;
  cin>> marks;
  if (marks > 40)
       cout<< “ Congratulation! You have passed ” ;
  getch();
}
In the above example if you input marks to 40 the output will be “Congratulation! You have passed” otherwise there will be nothing in the output of the program.

           Function:

A Function is a set of instructions which is used to perform a specific task. Every C++ program has at least one function which is main(). You can divide your program info different function depending on you.
Example:
A simple example of the function is the “clrscr();” function which is used to clear the output window.

What are operators in C++ and types of operators ?

Operators:

Operators are symbols which are used to perform arithmetic, conditional and logical operations or manipulations.
There are the following operators in C++:

1.      Arithmetic operators
2.      Comparison operators
3.      Logical operators
4.      Other operators

1.     Arithmetic operators:

Arithmetic operators are used to performing arithmetic operations such as addition, subtraction, multiplication and division.

Operator Name
Description
Operation
+
Used to add operands.
2 + 3 = 5
-
Used to subtract operands.
4 - 2 = 2
*
Used to find product of operands.
7 * 3 = 21
/
Used to perform division.
12 / 4 = 3
%
Provide the reminder of the division.
12 / 4 = 0

2.     Comparison operators:

Comparison operators compare two values or operands to check whether it is greater, less or equal.

Operator name
Description
Operation
< 
Used to find the smaller value
IF( A < B ) then  TRUE
> 
Used to find the greater value
IF( A > B) then TRUE
<=
Used to find less than or equal value
IF(A <= B) then TRUE
>=
Used to find greater than or equal value
IF( A >= B ) then TRUE
!=
Used to find not equal value
IF (A != B) then TRUE
==
Used to find equal value
IF ( A == B) then TRUE

3.     Logical Operators:

Logical operators are used to apply logical functions which are:
  1. AND ( && )
  2. OR ( || )
  3.  NOT ( ! )

AND:
AND Operator works with at least two operands:
X
Y
X AND Y
0
0
0
1
0
0
0
1
0
1
1
1

It evaluates the result of two conditions if the both conditions are true otherwise false.
Here is an example to understand it:

If ((A > B) && (A > C))
            cout << A << ” is greater ”;

In the above example if the value of A is greater than B and greater than C the result will be A.

OR:
OR operator is also a logical operator which evaluates the result if one of them conditions is true

X
Y
X OR Y
0
0
0
1
0
1
0
1
1
1
1
1

Here is an example to understand it:

If ((A > B ) || ( A < C))
            cout << A << ” is greater ”;

In the above example the result will be value of “A” if A is greater than B or C.



NOT
NOT operator is the negation of the result it shows the result true if condition is false and wise versa.
X
!(X)
0
1
1
0
0
1
1
0

Here is an example to understand it:

If ( !( A > B ) )
            cout << B << ” is greater ”;

In the above example if the value of A is greater than B the NOT operator will change the result and shows that B is greater than A.

4.     Other Operators:
These are the some other operators which are also used:
·         Assignment operator
·         Compound assignment operator
·         Increment/decrement
·         Ternary operator “?”

Assignment operator:
      Assignment operator is used to assign a value to a variable.
      Int A=10;

Compound assignment operator:
Compound assignment operators are used to assign value as well as operation with value.
A+=10 à  A = A + 10;

      Increment/decrement
Increment operator increases the value of a variable by 1 and decrement decreases the value of the operator by 1
A=10;

B = ++A;
Here the value of B is 11 because firstly it is incremented by 1 and then assigned to B.
C = --A;
Here the value of C is 9 because firstly it is decremented by 1 and then assigned to C.
X = A--;
Here the value of X is 10 because firstly the value of A is assigned to X and then    decremented by 1.
Y= A++;
Here the value of Y is 10 because firstly the value of A is assigned to Y and then incremented by 1.

       Ternary operator
The ternary operator is used to evaluating a condition. It returns one value of result depends on the condition.
Condition ? “result1” : “result2” ;
If the condition is true the output will result1 otherwise result2.
Example:
A = ( 7 > 2 ) “True” : “False” ;
The value of A is true because the condition is true and output is “True”.