Answer to Question #248246 in C++ for Tege

Question #248246

Decision and repetition statement with example

1,If statement

2,switch statement

3,for loop,for each loop

4,while loop, do while loop


1
Expert's answer
2021-10-07T14:05:55-0400

1, If statement

int number;
cout << "Enter an integer: ";
cin >> number;
// checks if the number is positive
if (number > 0) {
    cout << "You entered a positive integer: " << number << endl;
}
cout << "This statement is always executed.";

2, switch statement

char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
    case '+':
        cout << num1 << " + " << num2 << " = " << num1 + num2;
        break;
    case '-':
        cout << num1 << " - " << num2 << " = " << num1 - num2;
        break;
    case '*':
        cout << num1 << " * " << num2 << " = " << num1 * num2;
        break;
    case '/':
        cout << num1 << " / " << num2 << " = " << num1 / num2;
        break;
    default:
        // operator is doesn't match any case constant (+, -, *, /)
        cout << "Error! The operator is not correct";
        break;
}

3, for loop, for each loop

for (int i = 1; i <= 5; ++i) {
    cout <<  "Hello World! " << endl;
}

4, while loop, do-while loop

int i = 1; 
// while loop from 1 to 5
while (i <= 5) {
   cout << i << " ";
   ++i;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS