Home / code / cpp

C++ functions

The FOR-statement

Example:

# include <iostream.h>;

void main()
{
  for (int ; i<= 10; i++)
  {
	cout << "10 times the same" << endl;
  }
  cin.get();
}

FOR is a loop that starts from int i=1 and run as long as i<=10. And for every pass it does i++(increase i in this case).
For every pass it executes the body(everything between {} ).

Also cool to know is that, if there is only 1 line in the body you don't need to place the {}.
The following code does exactly the same:

# include <iostream.h>;

void main()
{
  for (int ; i<= 10; i++)
	cout << "10 times the same" << endl;
  cin.get();
}

The IF-statements

Example:

# include <iostream.h>;

void main()
{
  int number;

  cout << "Give a number" << endl;
  cin >> number;
  cin.get();

  if ( number>6 )
  {
	cout << "This number is greater then six." << endl;
  }
  else
  {
	cout << "This number is smaller then six or equal to 6." << endl;
  }

  cout <<"Thank you";
  
  cin.get();
}

In all cases Thank you is printed. But when the number is greater then 6 you also get: This number is greater then six.. When it is not greater then six, you get the else statement: This number is smaller then six or equal to 6..

So when the condition number>6 is TRUE the first instruction is executed. If the condition is not true, the else-instruction is executed.

You don't need to add the else-part. If nothing has to happen when the condition is FALSE, you don't add the else part.

The WHILE-loop

Example:

# include <iostream.h>;

void main()
{
  double x, sum=0.0;

  cout << "Enter a number " << endl;
  cout << "Enter a zero (0) to stop " << endl;
  cin >> x;

  while (x !=0.0)
  {
    sum+=x;
    cout << "Enter a number " << endl;
    cout << "Enter a zero (0) to stop " << endl;
    cin >> x;
  }
  cin.get();
  cout << "The total is << sum << endl;
  cin.get();
}

The body {...} is executed until the result of the test while (...) is not true (0).
So you have to put something in the body that changes the test. In the example this is x.

DO-loop

This is closely related with the WHILE-loop, with the difference that the body is run at least once.

Example:
# include <iostream.h>;
# include <conio.h>;  // to use the function getche()

void main()
{
  char ch;

  cout << "Enter characters and press enter " << endl << endl;

  do
  {
	ch = getche();
  } while ( ch != '\r');

  cout << endl << "That was the enter button";
  cin.get();
}

This is was a small example.
getche() is a function that stores the key that is pressed and also send it to the screen.

Break-statement

The break-statement breaks a loop. Simple as that.

Example:
# include <iostream.h>;

void main()
{
  char ch;
  double cijfer, sum=0.0, average;
  int number=0;

  do
  {
    cout << "Enter a number: ";
    cin >> cijfer;
    cin.get();
    if ( cijfer < 1.0 || cijfer > 10.0)
    {
	cout << endl;
	cout << "This was not a valid number" << endl;
	break; //break-statement
    }

    number++;
    sum += cijfer;

    cout << "Enter more numbers ? (y or n)";
    cin >> ch;
    cout << endl;
    cout << endl;
  }while(ch !="n");

  cout << "There are " << number << " valid numbers entered." << endl;
  if ( number > 0)
  {
    average=sum/number;
    cout << "The average is " << average;
  }

  cin.get();
}

This program checks if a the entered number is between 1 and 10. If the number is not, it ends the loop and calculates the total numbers entered and the average value.

Continue -statement

The continue statement interrupts the body of the loop but not the loop itself. With the continue statement you jump back to the test of the function.

Switch -statement

With if else you can only separate 2 cases (true or not true). With the switch statement you can do much more.

The code:
switch (variable)
{
  case 1:	instruction
		break;
  case 2:	instruction
		break;
  case 3:	instruction
		break;
  case 4:	instruction
		break;
  default:	instruction
}

You can add as many cases as you want.
What happens here is that the variable is compared with each case. If it is a match the instruction is behind the case is executed.
The break is always necessary, if there is no break every instruction below the match is executed.
The default: instruction is executed if there is no match.

You can also compare characters. case 'a':

What you can't do with switch is compare doubles.
case variable >8 : //Not allowed

 

TOP

Latest script:

 

Books: