Tutorial: Statements and Loops

If statement:

The if statement checks if the condition is true before executing the statement.
if (condition) {statement}

#include <iostream>
using namespace std;
 
int main()
{
  bool a = true;
  if (a == true)
  {
      cout << "a is true";
  }
  return 0;
}

We can also specify what we want to happen if the condition not is true with the else keyword.

#include <iostream>
using namespace std;
 
int main()
{
  bool a = true;
  if (a == true)
  {
      cout << "a is true";
  }
  else
  {
      cout << "a is false";
  }
  return 0;
}

While Loop:

The while loop executes the statement while the expression is true.
while (expression) {statement}

#include <iostream>
using namespace std;
 
int main()
{
  int i = 0;
  while (i<10)
  {
      i+=1;
      cout << i << endl;
  }
  return 0;
}

Do-while loop

This will give the same result as the while loop.
do {statement} while (condition)

#include <iostream>
using namespace std;
 
int main()
{
  int i = 0;
  do
  {
      i+=1;
      cout << i << endl;
  }
  while (i<10);
  return 0;
}

For Loop:

The for loop works like this:
Firstly the initialization is executed, then if the condition is true, increase and statement will be executed.
This continues until condition is false.
for (initialization;condition;increase) {statement}

#include <iostream>
using namespace std;
 
int main()
{
  for (int i=0;i<2;i++)
  {
      cout << "Hip, " << endl;
  }
  cout << "Horray!" << endl;
  return 0;
}

Switch Statement

The switch statement will give the same results as using several if statements.
It will check the value of expression and in case expression is value1 it will execute statement1.
In case the expression is a value that isn't listed bellow it will execute statement3.

switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
default:
statement3;
}

#include <iostream>
using namespace std;
 
int main()
{
  int i = 2;
  switch (i)
  {
      case 1:
      cout << "i is 1";
      break;
      case 2:
      cout << "i is 2";
      break;
      default:
      cout << "i is neither 1 or 2";
  }
  return 0;
}

Continue Statement:

The continue statement checks the condition, if it's true it will skip the rest of the statement block.
if (condition) continue;

#include <iostream>
using namespace std;
 
int main()
{
  for (int i=0; i<10; i++)
  {
    if (i==1) continue;
    if (i==3) continue;
    if (i==5) continue;
    if (i==7) continue;
    if (i==9) continue;
    cout << i << endl;
  }
  cout << endl << "1, 3, 5, 7 and 9 skipped." << endl;
  return 0;
}

Goto Statement:

Checks a condition, if the condition is true it will jump to the label.
if (condition) goto label;

#include <iostream>
using namespace std;
 
int main()
{
  int i = 0;
  label:
  i++;
  cout << i << endl;
  if (i<10) goto label;
  return 0;
}

Break Statement:

The break statement checks a condition, if the condition is true it will break the loop.
if (condition) break;

#include <iostream>
using namespace std;
 
int main()
{
  for (int i=0;i<10;i++)
  {
    cout << i << endl;
    if (i==5)
    {
      cout << "The for loop have been breaked." << endl;
      break;
    }
  }
  return 0;
}

Exit:

This will exit the program and return a given integer.
exit(integer);

exit(3);
Categories: Beginner Tutorials : Tutorials
page_revision: 4, last_edited: 1245766511|%e %b %Y, %H:%M %Z (%O ago)