Sunday, September 8, 2013

In this C++ programming tutorial we will look at loops.

There are situations were you want to do the same thing many times. For instance you want to
print the same words ten times. You could type ten cout statements, but it is easier to use a loop, such as a “for loop” or a “while loop.” The only thing you have to do is to setup a loop that execute the same cout statement ten times.
There are three basic types of loops which are:
  • “for loop”
  • “while loop”
  • "do while loop"

for loop

  for (St
The “for loop” loops from one number to another number and increases by a specified value each time.
The “for loop” uses the following structure:
for(start value;condition;increase value)
for eg::--
#include<iostream>
 using namespace std;
  int main()
 {
  int i;
  for (i = 0; i < 10; i++)
 {
   cout << "Hello" << "\n";
   cout << "There" << "\n";
 }
  return 0;
 }              }




 


































Note: A single instruction can be placed behind the “for loop” without the curly brackets.
Lets look at the “for loop” from the example: We first start by setting the variable i to 0. This is  where we start to count. Then we say that the for loop must run if the counter i is smaller then ten. Last we say that every cycle i must be increased by one (i++).
In the example we used i++ which is the same as using i = i + 1. This is called incrementing. The instruction i++ adds 1 to i. If you want to subtract 1 from i you can use i–. It is also possible to use ++i or -i. The difference is is that with ++i the one is added before the “for loop” tests if i < 10. With i++ the one is added after the test i < 10.

2. while loop

The while loop can be used if you don’t know how many times a loop must run.

Here is an example:

#include<iostream> using namespace std; int main() { int counter, howmuch; cout<<" enter some number"; cin >> howmuch; counter = 0; while ( counter < howmuch) { counter++; cout << counter << '\n'; } return 0; }

the prototype

do-while loop

Syntax of do-while loop
do
{
  statements;
} while (condition);
Note : That the loop body is always executed at least once. One important difference between the while loop and the do-while loop the relative ordering of the conditional test and loop body execution. In the while loop, the loop repetition test is performed before each execution the loop body; the loop body is not executed at all if the initial test fail. In the do-while loop, the loop termination test is Performed after each execution of the loop body. hence, the loop body is always executed least once.






Jump Statements

The jump statements unconditionally transfer program control within a function.
  • goto statement
  • break statement
  • continue statement
The goto statement goto allows to make jump to another point in the program. goto pqr; pqr: pqr is known as label. It is a user defined identifier. After the execution of goto statement, the control transfers to the line after label pqr.
The break statement The break statement, when executed in a switch structure, provides an immediate exit from the switch structure. Similarly, you can use the break statement in any of the loop. When the break statement executes in a loop, it immediately exits from the loop.
The continue statement The continue statement is used in loops and causes a program to skip the rest of the body of the loop. while (condition)             {   Statement 1;      If (condition)       continue;          statement;  } The continue statement skips rest of the loop body and starts a new iteration.
The exit ( ) function The execution of a program can be stopped at any point with exit ( ) and a status code can be informed to the calling program. The general format is exit (code) ; where code is an integer value. The code has a value 0 for correct execution. The value of the code varies depending upon the operating system.

0 comments:

Post a Comment