In programming, decision making is a fundamental concept. Often you have to make choices based on certain conditions.
This is the moment when conditional statements, such as “if” and “else,” play a big role. These statements allow you to execute different blocks of code depending on whether a certain condition is true or false.
if ( condition ) {
;
statements_for_true_condition }
else {
;
statements_for_false_condition }
You can also use multiple “if-else” statements and combine them to create complex decision structures as in the examples below.
Write a program to calculate the sum of all even two-digit numbers. The calculated sum is printed on the screen.
Solution:
#include <iostream>
using namespace std;
int main () {
int i = 10, sum = 0;
while (i <= 98) {
= sum + i;
sum +=2;
i}
<< sum << endl;
cout return 0;
}
Write a program to calculate the sum of all odd two-digit numbers. The program prints the sum on the screen in the following format:
11 + 13 + 15 + 17 + … + 97 + 99 = 2475
Solve the program without using the if
statement
Solution 1:
#include <iostream>
using namespace std;
int main () {
int i = 11, sum = 0;
<< i;
cout = i;
sum =i+2;
iwhile (i <= 99){
<< " + " << i;
cout = sum + i;
sum +=2;
i}
<< " = " << sum << endl;
cout return 0;
}
Solution 2:
#include <iostream>
using namespace std;
int main () {
int i = 11, sum = 0;
while (i <= 97) {
<< i << " + ";
cout = sum + i;
sum +=2;
i}
= sum + i;
sum << i << " = " << sum << endl;
cout return 0;
}
Write a program to calculate \$y = x^n\\$ for a given natural number n, n >= 1 and real number x.
Solution 1:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int counter = 0, n;
float x, y = 1;
<< "x: ";
cout >> x;
cin << "n: ";
cout >> n;
cin while (counter < n) {
*= x;
y ++;
counter}
<< x << "^" << n << " = " << y << endl;
cout return 0;
}
Solution 2:
#include <iostream>
using namespace std;
int main() {
int counter = 0, n;
float x, y = 1;
<< "x: ";
cout >> x;
cin << "n: ";
cout >> n;
cin do {
*= x;
y ++;
counter} while (counter < n);
<< x << "^" << n << " = " << y << endl;
cout return 0;
}
Solution 3:
#include <iostream>
using namespace std;
int main() {
int counter, n;
float x, y;
<< "x: ";
cout >> x;
cin << "n: ";
cout >> n;
cin for(counter = 1, y = x; counter < n; counter++) {
*= x;
y }
<< x << "^" << n << " = " << y << endl;
cout return 0;
}
Solution 4:
#include <iostream>
using namespace std;
int main() {
int counter = 0, n;
float x, y = 1;
<< "x: ";
cout >> x;
cin << "n: ";
cout >> n;
cin for( ; counter < n; counter++) {
*= x;
y }
<< x << "^" << n << " = " << y << endl;
cout return 0;
}
Write a program that will determine the number of numbers divisible by 3, when divided by 3 have a remainder of 1, or 2, out of n numbers (entered from the keyboard).
Solve the task with while, do…while
and
for
Solution with while
:
#include <iostream>
using namespace std;
int main() {
int n = 1, i = 0, number, div, r1, r2;
= r1 = r2 = 0; /* counters */
div >> n; /* input numbers */
cin while (i < n) {
>> number;
cin if (number % 3 == 0)
++;
divelse if (number % 3 == 1)
++;
r1else r2++;
++;
i}
<< div << endl;
cout << r1 << endl;
cout << r2 << endl;
cout return 0;
}
Решение со do… while
:
#include <iostream>
using namespace std;
int main() {
int n = 1, i = 0, number, div, r1, r2;
= r1 = r2 = 0; /* counters */
div >> n; /* input numbers */
cin do {
>> number;
cin if (number % 3 == 0)
++;
divelse if (number % 3 == 1)
++;
r1else r2++;
++;
i} while (i < n);
<< div << endl;
cout << r1 << endl;
cout << r2 << endl;
cout return 0;
}
Solution with for
:
#include <iostream>
using namespace std;
int main() {
int n = 1, i = 0, number, div, r1, r2;
= r1 = r2 = 0; /* counters */
div >> n; /* input numbers */
cin for (i = 0; i < n; ++i) {
>> number;
cin if (number % 3 == 0)
++;
divelse if (number % 3 == 1)
++;
r1else r2++;
}
<< div << endl;
cout << r1 << endl;
cout << r2 << endl;
cout return 0;
}
For three entered line segments, determine whether it is possible to construct a triangle and whether the triangle is right-angled, acute-angled, or obtuse-angled.
For a given center of a circle and its radius, determine which quadrants the circle passes through.