WEB DEVELOPER SITE
PYTHONPHPJAVA
 

C++ Do..while Loop


do...while loop checks its condition at the bottom of the loop.



But A do...while loop is similar to a while loop. The one difference is that the do...while loop is guaranteed to execute at least one time.

Syntax:

do {

statements
} while (condition);

Do while..loop Example

#include 
using namespace std;

int main()
{
    int a = 2;
    do {
        cout << a << endl;
        a++;
    } while(a < 8);

    return 2;
}
//output
2
3
4
5
6
7

switch statements

A switch can contain any number of case statements, which are followed by the value in question and a colon.

syntex

switch (expression) {
  case value1:
    statement(s);
    break;
  case value2:
    statement(s);
    break;
    ...
  case valueN:
    statement(s);
    break;
}

Example

int main()
{
    int age = 8;
    switch (age) {
        case 12:
            cout << "Teen agers";
            break;
        case 8:
            cout << "child";
            break;
        case 30:
            cout << "adult";
            break;
    }

    return 0;
}

Output child

Default switch statements

int age = 25;
switch (age) {
  case 12:
    cout << "Teen agers";
    break;
  case 8:
    cout << "child";
    break;
  case 30:
    cout << "adult";
    break;
  default:
    cout << "This is the default case";
}

output
This is the default case