Thursday, May 2, 2019

Introduction of c++

C++ Tutorial – Learn C++ Programming with examples




C++ is developed by Bjarne Stroustrup starting in 1979 at Bell Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. This tutorial adopts a simple and practical approach to describe the concept.

Surprisingly not all computer programming languages are for programmers. Consider the classic examples of non-programmer languages, COBOL and BASIC. COBOL was designed not to better the programmer’s lot, not to improve the reliability of the code produced, and not even to improve the speed with which code can be written. Rather, COBOL was designed, in part, to enable non-programmers to read and presumably (however unlikely) to understand the program. BASIC was created essentially to allow non-programmers to program a computer to solve relatively simple problems.

 In contrast, C++ is well fitted for programmer it is middle level language and easy to learn. C++ was built upon the foundation of C. In fact,C++ includes the entire C language, and (with minor exceptions) all C programs are also C++ programs.  When C++ was invented, the C language was used as the starting point. To C were added several new features and extensions designed to support object oriented programming (OOP).

  
                                                                   

Function in c++

Function 

A function is a group of statements that is executed when it is called from some point of the program.It is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions.
 The following is its format:

return type name ( parameter1, parameter2, ...)
 {
 statements
 } 
where:
return type: return type is the data type specifier of the data returned by the function.
name: name is the identifier by which it will be possible to call the function.
parameters (as many as needed): Each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas.
s statement is the function's body. It is a block of statements surrounded by braces { }. 

Here you have the first function example:

#include <iostream>
 using namespace std;
int addition (int x, int Y) 
{
int r; 
r=X+Y; 
return (r);
}
int main ()
 {
int z; 
z = addition (5,3); 
cout << "The result is " << z;
 return 0;
}

Function Declarations

A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.
A function declaration has the following parts:
return_type function_name( parameter list )

Example:
 int max(num1,  int num2);                                        

Parameter names are not important in function declaration only their type is required,

 so following is also valid declaration:
int max(int,int);                                        

Function Arguments

If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.


Nested loop

Nested loop

A loop can be nested inside of another loop. C++ allows at least 256 levels of nesting.

Syntax

 The syntax for a nested for loop statement in C++ is as follows:

 statement in switch case. See the example below:
for ( init; condition; increment ) 
{
          for ( init; condition; increment ) 
{
             statement(s);
          statement(s);             // you can put more statements.
}


The syntax for a nested while loop statement in C++ is as follows:

while(condition) 
{
while(condition) 
{
statement(s);
}
   statement(s);         // you can put more statements.
}
The syntax for a nested do...while loop statement in C++ is as follows: 


          for ( init; condition; increment ) 
{
             statement(s);
          statement(s);             // you can put more statements.
}


The syntax for a nested while loop statement in C++ is as follows:


do {
statement(s); // you can put more statements. do {
statement(s); 
}
while( condition ); 
}
while( condition );

Example 

The following program uses a nested for loop to find the prime numbers from 2 to 100:


#include <iostream> 
using namespace std;
int main () 
{
int i, j;
for(i=2; i<100; i++)
 { 
    for(j=2; j <= (i/j); j++)
        if(!(i%j)) 
         break; // if factor found, not prime 
      if(j > (i/j))
    cout << i << " is prime\n";
} return 0;


This would produce the following result:

2 is prime
 3 is prime 
5 is prime
 7 is prime
 11 is prime 
13 is prime 
17 is prime
 19 is prime
 23 is prime 
29 is prime 
31 is prime 
37 is prime 
41 is prime
 43 is prime 
47 is prime
 53 is prime 
59 is prime 
61 is prime 
67 is prime 
71 is prime 
73 is prime 
79 is prime 
83 is prime 
89 is prime 
97 is prime

Loop Statement

Loop statement
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:
C++ programming language provides the following type of loops to handle looping requirements

While Loop :

A while loop statement repeatedly executes a target statement as long as a given condition is true.

Syntax

 The syntax of a while loop in C++ is:


while(condition) {
statement(s); }
Here statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following the loop.
Example:
#include <iostream>
 using namespace std;
int main () 
{
int n; 
cout << "Enter the starting number > ";
 cin >> n;
while (n>0)
 {
 cout << n << ", "; --n;
}
cout << "FIRE!\n"; 
 return0;
}

for Loop

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax 

For (init;condition;incr/decrd)
{
   Statement(s);
}

 Here is the flow of control in a for loop:
1. The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
2. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
3. After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
4. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

Example:
#include <iostream> 
using namespace std; 
int main ()
 {
for (int n=10; n>0; n--) 
{ cout << n << ", ";
} cout << "FIRE!\n";
 return 0;
}

do…while Loop

Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop. A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

Syntax

 The syntax of a do...while loop in C++ is:
do
{
statement(s);
}
while(condition);
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.

Example:
#include <iostream> 
using namespace std;
int main ()
 {
unsigned long n;
 do
 { cout << "Enter number (0 to end): "; 
cin >> n;
 cout << "You entered: " << n << "\n";

while (n != 0);
 return 0;
}

Jump statements

Jump statements. 

The break statement

Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end.
For example:
we are going to stop the count down before its natural end (maybe because of an engine check failure?).

#include <iostream> 
using namespace std;
int main ()
 {
int n; 
for (n=10; n>0; n--) {
cout << n << ", ";
 if (n==3) {
cout << "countdown aborted!"; 
break;
}
}
 return 0;
}


The continue statement

The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration.
For example:
 w are going to skip the number 5 in our countdown:


#include <iostream> 
using namespace std;
int main () 
{
for (int n=10; n>0; n--)
 {
 if (n==5) continue;
 cout << n << ", ";
}
 cout << "FIRE!\n"; 
return 0;
}

The goto statement

 goto allows to make an absolute jump to another point in the program. You should use this feature with caution since its execution causes an unconditional jump ignoring any type of nesting limitations. The destination point is identified by a label, which is then used as an argument for the goto statement. A label is made of a valid identifier followed by a colon (:).
Generally speaking, this instruction has no concrete use in structured or object oriented programming aside from those that low-level programming fans may find for it.
For example:
here is our countdown loop using goto:

#include <iostream>
 using namespace std;
int main () 
{
int n=10; 
loop:
 cout << n << ", ";
 n--;
 if (n>0) goto loop; 
cout << "FIRE!\n"; 
return 0;
}

The exit function

 exit is a function defined in the cstdlib library. The purpose of exit is to terminate the current program with a specific exit code. Its prototype is: void exit (int exitcode);
The exitcode is used by some operating systems and may be used by calling programs. By convention, an exit code of 0 means that the program finished normally and any other value means that some error or unexpected results happened.


void exit (int exitcode);


C++ switch case

Switch Statement

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
Flow
Syntax The syntax for a switch statement in C++ is as follows:

The syntax of Switch case statement:
switch (expression)
 {
 case constant1: 
    group of statements 1;
 break;
 case constant2: 
    group of statements 2;
 break;
 . 
 .
 .
 default:
 default group of statements
}

The following rules apply to a switch statement:

◾ The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
◾ You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
◾ Th constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
◾ When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
◾ When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
◾ Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
◾ A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Both of the following code fragments have the same behavior: 



The switch statement is a bit peculiar within the C++ language because it uses labels instead of blocks. This forces us to put break statements after the group of statements that we want to be executed for a specific condition. 
Otherwise the remainder statements -including those corresponding to other labels- will also be executed until the end of the switch selective block or a break statement is reached.

Wednesday, May 1, 2019

C++ if-else statement

C++ if-else statement


The if keyword is used to execute a statement or block only if a condition is fulfilled. 

Its form is:
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 right after this conditional structure.

 For example:

 the following code fragment prints x is 100 only if the value stored in the x variable is indeed 100:
if (x == 100)
 cout << "x is 100";

If we want more than a single statement to be executed in case that the condition is true we can specify a block using braces { }:
if (x == 100) {
 cout<< "x is "; cout << x;
}

We can additionally specify what we want to happen if the condition is not fulfilled by using the keyword else.

 Its form used in conjunction with if is:
if (condition) statement1 else statement2.

For example:
i (x == 100) cout << "x is 100";
else cout << "x is not 100";

prints on the screen x is 100 if indeed x has a value of 100, but if it has not -and only if not- it prints out x is not 100.

The if + else structures can be concatenated with the intention of verifying a range of values. The following example shows its use telling if the value currently stored in x is positive, negative or none of them (i.e. zero):

if (x > 0) cout << "x is positive";
else if (x < 0) cout << "x is negative";
else cout << "x is 0";

Remember that in case that we want more than a single statement to be executed, we must group them in a block by enclosing them in braces { }.

Array

Arrays  An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by ad...