Thursday, 1 December 2011

Hey This one for the "FOR LOOP PROGRAMMING" it is help full

This C++ for loop tutorial is written for beginning C++ students without previous C++ programming experience. There are C++ questions about the for loop inside the tutorial, for the students to answer and many code examples and explanations about the conditions of a C++ for loop. Some of the for loop examples have an if/else structure within the for loop structure...

The format of a for loop is:

for(initializing expressions)
{
   statements; // what the loop will do
}
 
Prior to the for loop, declare a variable, such as an integer. This variable will be used for thethree initializing conditions of the for loop:
· Telling the compiler at which value to begin
· Telling the compiler at which value to stop
· Incrementing or decrementing the loop (makes the loop
   move to the next value for code processing)

 

Counting from 0 to 10
// Beginning Students
// Code Example:
// Counting From 0 To 10

#include<iostream.h>
using namespace std;

void main()
{
  // declare a variable
  int i;
  for(i= 0; i < 11; i++)
    cout << i << endl;
}
/* This opens the comment:
This is the generated output:
0
1
2
3
4
5
6
7
8
9
10


*/ This closes the comment
using namespace std; is for the cout statement so you do not have to type std::cout everytime that cout is used in a program. void main() was used since I am not returning a value.
The letter i is the one I normally use when using an integer in a for loop but any other letter or variable name will work just as well.
The reason that I like to use the i, is just from habit and seeing it most often used in textbooks for the for loop conditions. This makes for easier readability of my code. Using a longer variable name like number, seems a bit awkward in a for loop's condition statements:


for(number = 0; number < 11; number++)
just seems "cluttered with longer variable name". It is still a matter of preference and it would not have been wrong to declare int number instead of int i.
·
i= 0 told the compiler you are starting at 0.
·
i < 11 told the compiler where to stop the loop
·
i++ means i = i + 1 (which increments the loop, moves the loop   to the next i value.
Counting backwards with a for Loop
· Tell the compiler where to start
· Tell the compiler when to stop
· Decrement the loop:
j = j - 1 in its shorter form is j--
 

#include<iostream.h>
using namespace std;

void main()
{
  // declare a variable before starting
  int i;
  for(i=10; i > -1; i--)
  {
    if(i == 0)
      cout << "Blast Off!" << endl;
    else
      cout << i << endl;
  }
  // do not return anything when
  // void main() is used
}

 
The above loop starts counting backwards at 10; loop stops when the i value hits -1;
Each step of the loop is decremented by -1, so after 10 is output, then 9 is output.
If the i value hits 0, instead of printing 0 on the console, you will see the words "Blast Off!" [and end the line] because of the if/else statement.
You can output a string to the console when the integer object has a zero value. It says, if i is 0, then print "Blast Off!", otherwise output i and end the line.
You do not need to use the {} curly braces for the if statement or the else statement when only one statement is used.
The for loop used the {} curly braces because that loop has more than one statement.

Don't confuse conditions with the for loop statements. The conditions are inside the parentheses and tell how the loop will run and the statements outside the parentheses tell what the loop will do to the object types, in this case the integer object i


Shortcuts for coding increments and decrements
j = j + 2 can be written j+=2;   (adds 2 to the value of j)
j = j - 2 can be written j-=2;   (substracts 2 from the value of j)
k = k + 1 can be written k++;   (adds 1 to the value of k)
k = k - 1 can be written k--;   (substracts 1 from the value of k
m = m + 5 can be written m+=5   (adds 5 to the value of m)

Analyze This Loop:


int k;
for (k = 0; k < 51; k +=10)
  cout << k << endl;

 

Some Question Friends for Practice:-

· At what value of k does the for loop begin?
· What is last value of k the for loop will output?
· By what value of k does the for loop increment each step?

· Write the output of the for loop.

- Write the Output of the Following Code:


 

int n;
int stop = 3;
int start = 8;
cout << "start on 8" << endl;
for(n = (start-1); n >= stop; n--)
{
  if(n=stop)
    cout << "stop on " << n << endl;
  else
    cout << "down to " << n << endl;
}

 

Compiler Errors, Infinite Loops:-
Typical student errors in the for loop structure are not putting in the proper semicolon (;) after the first and second conditions, inside the parentheses or accidentally reversing the < or > symbols When a loop is counting forward, it makes sense that the loop should stop somewhere less than some value, or it will count forever… an infinite loop.

For example,

Count from 20 to 45 by 5's:

int k = 0;
for(k = 20; k < 50; k+=5)
cout << k << endl;
Each number will be on a separate line because of the endl [end line] statement. cout << endl; is an empty line but cout << k << endl; is the output of the value of k, then a newline. Think of endl; as a line break. Nothing comes after it on that line so any other new output must be on another line.

If by accident, a student had coded:

for(k = 20; k > 50; k+=5)

where would the loop stop?
It begins at 20, adds 5 so k=25, and next time k=30, then k=35 and when k=50 the stopping place is k "greater than" 50, so k gets 5 more added to it... forever. (This is called an
infinite loop.)

The way to stop an infinite loop varies between compilers. When I was using VC++ 6.0 on a Windows 98SE machine, I could stop an infinite loop by pressing the Break or Pause key but not all compilers work the same way to stop an infinite loop. Check with your instructor about your own particular compiler and ask how to stop infinite loops.
 
When a for loop is incrementing (adding), make sure that the middle condition that stops the loop is NOT a > greater than symbol.

Now consider a decrementing loop. Count from 50 to 20 by 5's:

int k;
for(k=50; k>19; k--)
cout << k << endl;

 
This code will count from 50 backwards by 5's and stop at 20 because the middle condition is k greater than (>) 19 for where the backwards counting should stop.
So, when a for loop is decrementing (subtracting), make sure that the middle condition which stops the loop has NO < less than symbol, but is a greater than > symbol, so that the loop will stop.


For Loop

The for loop is used to repeat code a certain number of times between 2 numbers. You have to use a loop variable to go through each loop. The first part of a for loop initializes the loop variable to the number from which the loop starts. The second part is the condition that the loop must meet to exit the loop. The third part is used to increment the value of the loop variable. Here is an example of how to print the word Hello 10 times.
int x;
for (x = 1; x <= 10; x++)
   cout << "Hello\n";

The x++ part is something you haven't seen yet. Adding ++ to the front or back of a variable will increment it by 1. -- is used to decrement by 1. Putting the ++ before the variable increments it before the condition of the loop is tested and putting it after increments it after the loop condition is tested.
If you want to use more than one line of code in a loop then you must use curly brackets just like with an if statement.
int x;
for (x = 1; x <= 10; x++)
{
   cout << "Hello\n";
   cout << "There\n";
}

While Loop

The while loop repeats code until a condition is met. You don't have to use a loop variable but if you do then you need to initialize it before running the loop. You also need to increment the loop variable inside the loop.
int x = 1;
while (x <= 10)
{
   cout << "Hello\n";
   x = x + 1;
}

Do While Loop

The do while loop is like the while loop except that the condition is tested at the bottom of the loop.
int x = 1;
do
{
   cout << "Hello\n";
   x = x + 1;
}
while (x <= 10);

Break

The break command can be used to exit a loop at any time. Here is one of the above examples that will only print Hello once and then break out of the loop.
int x;
for (x = 1; x <= 10; x++)
{
   cout << "Hello\n";
   break;
}

Continue

The continue command lets you start the next iteration of the loop. The following example will not print Hello because the continue command goes back to the beginning of the loop each time.
int x;
for (x = 1; x <= 10; x++)
{
   continue;
   cout << "Hello\n";
}




FOR - for loops are the most useful type. The syntax for a for loop is:-

 
for ( variable initialization; condition; variable update ) {
  Code to execute while the condition is true
}
The variable initialization allows you to either declare a variable and give it a value or give a value to an already existing variable. Second, the condition tells the program that while the conditional expression is true the loop should continue to repeat itself. The variable update section is the easiest way for a for loop to handle changing of the variable. It is possible to do things like x++, x = x + 10, or even x = random ( 5 ), and if you really wanted to, you could call other functions that do nothing to the variable but still have a useful effect on the code. Notice that a semicolon separates each of these sections, that is important. Also note that every single one of the sections may be empty, though the semicolons still have to be there. If the condition is empty, it is evaluated as true and the loop will repeat until something else stops it.

Example:

 
#include <iostream.h>

using namespace std; // So the program can see cout and endl

int main()
{
  // The loop goes while x < 10, and x increases by one every loop
  for ( int x = 0; x < 10; x++ ) {
    // Keep in mind that the loop condition checks 
    //  the conditional statement before it loops again.
    //  consequently, when x equals 10 the loop breaks.
    // x is updated before the condition is checked.    
    cout<< x <<endl;
  }
  getch();
}
This program is a very simple example of a for loop. x is set to zero, while x is less than 10 it calls cout<< x <<endl; and it adds 1 to x until the condition is met. Keep in mind also that the variable is incremented after the code in the loop is run for the first time.

WHILE - WHILE loops are very simple. The basic structure is

while ( condition ) { Code to execute while the condition is true } The true represents a boolean expression which could be x == 1 or while ( x != 7 ) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x ==5 || v == 7) which says execute the code while x equals five or while v equals 7. Notice that a while loop is the same as a for loop without the initialization and update sections. However, an empty condition is not legal for a while loop as it is with a for loop.

Example:

 
#include <iostream.h>

using namespace std; // So we can see cout and endl

int main()
{ 
  int x = 0;  // Don't forget to declare variables
  
  while ( x < 10 ) { // While x is less than 10 
    cout<< x <<endl;
    x++;   // Update x so the condition can be met eventually
  }
  getch();
}
This was another simple example, but it is longer than the above FOR loop. The easiest way to think of the loop is that when it reaches the brace at the end it jumps back up to the beginning of the loop, which checks the condition again and decides whether to repeat the block another time, or stop and move to the next statement after the block.

DO..WHILE - DO..WHILE loops are useful for things that want to loop at least once. The structure is

 
do {
} while ( condition );
Notice that the condition is tested at the end of the block instead of the beginning, so the block will be executed at least once. If the condition is true, we jump back to the beginning of the block and execute it again. A do..while loop is basically a reversed while loop. A while loop says "Loop while the condition is true, and execute this block of code", a do..while loop says "Execute this block of code, and loop while the condition is true".

Example:

 
#include <iostream.h>

using namespace std;

int main()
{
  int x;

  x = 0;
  do {
    // "Hello, world!" is printed at least one time
    //  even though the condition is false
    cout<<"Hello, world!\n";
  } while ( x != 0 );
  getch();
}
Keep in mind that you must include a trailing semi-colon after the while in the above example. A common error is to forget that a do..while loop must be terminated with a semicolon (the other loops should not be terminated with a semicolon, adding to the confusion). Notice that this loop will execute once, because it automatically executes before checking the condition.  

Sunday, 13 November 2011

My new c++ Programming

#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i,j,temp;
cout<<"Enter an array"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
cout<<"The reverse array is"<<endl;
for(i=0,j=9;i<=j;i++,j--)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
for(i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}
getch();
}



//another way to reverse an array
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],arr1[10],i,j;
cout<<"Enter an array"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
cout<<"The reverse array is"<<endl;
for(i=0,j=9;i<10;i++,j--)
{
arr1[i]=arr[j];
}
for(i=0;i<10;i++)
{
cout<<arr1[i]<<endl;
}
getch();
}

Prashant Kumar Rai: Hiiiii

Prashant Kumar Rai: Hiiiii: Hey!!!!Friends hiii i am ur friend Prashant from k.v pushp vihar....follow me....Are You Searching Me Here And There....Ya friends this time to some thing interesting in this blog.....so are you ready....