Tuesday, 28 May 2013

For Loop

For Loop


We have to write the following program...

Use For Loops to create a program that prints 
out a structure that looks like this:


*
**
***
****
*****
****
***
**
*

So far all I have is this code...


# include <iostream>
using namespace std;

void main ()
{

int star;


for (int star = 1; star <=5; star = star + 1) 
cout << " * " << endl;
}
Not sure where to go from here..

Thanks

Sunday, 26 May 2013

Arrays Example

// arrays example#include <iostream>using namespace std; int billy [] = {16, 2, 77, 40, 12071};int n, result=0; int main (){ for ( n=0 ; n<5 ; n++ ) { result += billy[n]; } cout << result; return 0;}

Under Standing for Array


Array classArrays are fixed-size sequence containers: they hold a specific number of elements ordered in a strict linear sequence.Internally, an array does not keep any data other than the elements it contains (not even its size, which is a template parameter, fixed on compile time). It is as efficient in terms of storage size as an ordinary array declared with the language's bracket syntax ([]). This class merely adds a layer of member and global functions to it, so that arrays can be used as standard containers.

Thursday, 16 May 2013

WHILE AND DO WHILE LOOP

//WHILE LOOP 
#include <iostream>

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
  }
  cin.get();
}
//DO WHILE LOOP 
#include <iostream>

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 );
  cin.get();
}
 
The main difference between WHILE & DO WHILE LOOP is that while loop will execute
only if the condition is true else it will never execute, AND DO WHILE LOOP will
execute for at least one time even if the condition is false.

Under Standing of "FOR LOOP"

#include <iostream>

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;
  }
  cin.get();
}