Thursday, May 2, 2019

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