3.Conditions
03 Sep 2019 | C++
Example 1
#include <iostream>
using namespace std;
main()
{
double var1, var2;
beginning: // it kind of check point
system("cls"); // clear terminal
cout << "Enter first number: " << endl;
cin >> var1;
cout << "Enter second number: " << endl;
cin >> var2;
cout << "What do you want to do with that numbers?" << endl;
cout << "+ - add" << endl;
cout << "- - substract" << endl;
cout << "* - multiply" << endl;
cout << "/ - divide" << endl;
char decision;
cin >> decision;
system("cls"); // clear terminal
switch(decision)
{
case '+':
cout << var1 << " + " << var2 << " = " << (var1 + var2) << endl;
break;
case '-':
cout << var1 << " - " << var2 << " = " << (var1 - var2) << endl;
break;
case '*':
cout << var1 << " * " << var2 << " = " << (var1 * var2) << endl;
break;
case '/':
if (var2) //var2 != 0
cout << var1 << " / " << var2 << " = " << (var1 / var2) << endl;
else
cout << "You can't divide by 0" << endl;
break;
default:
cout << "You typed wrong character";
}
char decision2;
cout << "Do you want to continue that program? (Y/N)" << endl;
cin >> decision2;
if (decision2 == 'y' || decision2 == 'Y')
goto beginning;
}
Example 2
#include <iostream>
using namespace std;
main()
{
int a = 10;
int b = 20;
// CONDITION "?" so instruction that will be executed if condition is true : if condition is not true;
string message = (a > b) ? "a > b" : "a <= b";
cout << ((a > b ? a : b) + 10)<< endl; // if a>b is true it took a value, but is not true take b value
}
Example 3
#include <iostream>
using namespace std;
main() {
int minValue, maxValue, elementToCheck;
bool isGreaterThanMinValue, isLowerThanMaxValue;
cout << "Please input the minimum value: ";
cin >> minValue;
cout << "Please input the maximum value: ";
cin >> maxValue;
cout << "Please input the number to check: ";
cin >> elementToCheck;
isGreaterThanMinValue = (minValue <= elementToCheck);
isLowerThanMaxValue = (elementToCheck <= maxValue);
cout << "Is the value " << elementToCheck << " greater or equal to " << minValue << " ? " << isGreaterThanMinValue << endl;
cout << "Is the value " << elementToCheck << " lower or equal to " << maxValue << " ? " << isLowerThanMaxValue << endl;
}
Example 4
#include <iostream>
using namespace std;
main()
{
char x = 'a';
/*
4.13412341234123412348888888
4.13412341234123412345555555
*/
switch (x)
{
case 'd':
cout << "this is a place for the instructions that will be executed when switched value is equal to 0" << endl;
break;
case 25:
cout << "this is a place for the instructions that will be executed when switched value is equal to 25" << endl;
break;
case 50:
cout << "this is a place for the instructions that will be executed when switched value is equal to 50" << endl;
break;
default: // this one kind of "else" in python
cout << "this is a place for the instructions that will be executed when value of switched variable is not equal to any case" << endl;
}
}
Example 5
#include <iostream>
using namespace std;
main ()
{
int age;
cout << "Please input your age: ";
cin >> age;
string isAdult = (age >= 18)
? "You are an adult please enter !"
: "You are not an adult !";
// ? is the condition if true a in "?a:b" if not b in "?a:b"
cout << isAdult;
}
Example 6
#include <iostream>
using namespace std;
main ()
{
int numberOfPoints;
cout << "Enter the number of points you have achieved: ";
cin >> numberOfPoints;
if ((numberOfPoints >= 0) && (numberOfPoints <= 100))
{
string outputMessage = (numberOfPoints > 50)
? "You have passed the exam. Congratulations."
: "You didn't pass the exam.";
cout << outputMessage;
}
else
cout << "ERROR. The number of points is invalid !";
}
Example 7
#include <iostream>
using namespace std;
main ()
{
int choice;
cout << "1. January" << endl;
cout << "2. February" << endl;
cout << "3. March" << endl;
cout << "4. April" << endl;
cout << "5. May" << endl;
cout << "6. June" << endl;
cout << "7. July" << endl;
cout << "8. August" << endl;
cout << "9. September" << endl;
cout << "10. October" << endl;
cout << "11. November" << endl;
cout << "12. December" << endl;
cout << "Choose the month: ";
cin >> choice;
switch (choice)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
cout << "This month has 31 days" << endl;
break;
case 4:
case 6:
case 9:
case 11:
cout << "This month has 30 days" << endl;
break;
case 2:
{
int year;
cout << "Input the year: ";
cin >> year;
bool isLeapYear = (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0));
if (isLeapYear)
cout << "The month february has 29 days in year " << year << endl;
else
cout << "The month february has 28 days in year " << year << endl;
}
default:
cout << "ERROR. Invalid choice.";
}
}
Example 1
#include <iostream>
using namespace std;
main()
{
double var1, var2;
beginning: // it kind of check point
system("cls"); // clear terminal
cout << "Enter first number: " << endl;
cin >> var1;
cout << "Enter second number: " << endl;
cin >> var2;
cout << "What do you want to do with that numbers?" << endl;
cout << "+ - add" << endl;
cout << "- - substract" << endl;
cout << "* - multiply" << endl;
cout << "/ - divide" << endl;
char decision;
cin >> decision;
system("cls"); // clear terminal
switch(decision)
{
case '+':
cout << var1 << " + " << var2 << " = " << (var1 + var2) << endl;
break;
case '-':
cout << var1 << " - " << var2 << " = " << (var1 - var2) << endl;
break;
case '*':
cout << var1 << " * " << var2 << " = " << (var1 * var2) << endl;
break;
case '/':
if (var2) //var2 != 0
cout << var1 << " / " << var2 << " = " << (var1 / var2) << endl;
else
cout << "You can't divide by 0" << endl;
break;
default:
cout << "You typed wrong character";
}
char decision2;
cout << "Do you want to continue that program? (Y/N)" << endl;
cin >> decision2;
if (decision2 == 'y' || decision2 == 'Y')
goto beginning;
}
Example 2
#include <iostream>
using namespace std;
main()
{
int a = 10;
int b = 20;
// CONDITION "?" so instruction that will be executed if condition is true : if condition is not true;
string message = (a > b) ? "a > b" : "a <= b";
cout << ((a > b ? a : b) + 10)<< endl; // if a>b is true it took a value, but is not true take b value
}
Example 3
#include <iostream>
using namespace std;
main() {
int minValue, maxValue, elementToCheck;
bool isGreaterThanMinValue, isLowerThanMaxValue;
cout << "Please input the minimum value: ";
cin >> minValue;
cout << "Please input the maximum value: ";
cin >> maxValue;
cout << "Please input the number to check: ";
cin >> elementToCheck;
isGreaterThanMinValue = (minValue <= elementToCheck);
isLowerThanMaxValue = (elementToCheck <= maxValue);
cout << "Is the value " << elementToCheck << " greater or equal to " << minValue << " ? " << isGreaterThanMinValue << endl;
cout << "Is the value " << elementToCheck << " lower or equal to " << maxValue << " ? " << isLowerThanMaxValue << endl;
}
Example 4
#include <iostream>
using namespace std;
main()
{
char x = 'a';
/*
4.13412341234123412348888888
4.13412341234123412345555555
*/
switch (x)
{
case 'd':
cout << "this is a place for the instructions that will be executed when switched value is equal to 0" << endl;
break;
case 25:
cout << "this is a place for the instructions that will be executed when switched value is equal to 25" << endl;
break;
case 50:
cout << "this is a place for the instructions that will be executed when switched value is equal to 50" << endl;
break;
default: // this one kind of "else" in python
cout << "this is a place for the instructions that will be executed when value of switched variable is not equal to any case" << endl;
}
}
Example 5
#include <iostream>
using namespace std;
main ()
{
int age;
cout << "Please input your age: ";
cin >> age;
string isAdult = (age >= 18)
? "You are an adult please enter !"
: "You are not an adult !";
// ? is the condition if true a in "?a:b" if not b in "?a:b"
cout << isAdult;
}
Example 6
#include <iostream>
using namespace std;
main ()
{
int numberOfPoints;
cout << "Enter the number of points you have achieved: ";
cin >> numberOfPoints;
if ((numberOfPoints >= 0) && (numberOfPoints <= 100))
{
string outputMessage = (numberOfPoints > 50)
? "You have passed the exam. Congratulations."
: "You didn't pass the exam.";
cout << outputMessage;
}
else
cout << "ERROR. The number of points is invalid !";
}
Example 7
#include <iostream>
using namespace std;
main ()
{
int choice;
cout << "1. January" << endl;
cout << "2. February" << endl;
cout << "3. March" << endl;
cout << "4. April" << endl;
cout << "5. May" << endl;
cout << "6. June" << endl;
cout << "7. July" << endl;
cout << "8. August" << endl;
cout << "9. September" << endl;
cout << "10. October" << endl;
cout << "11. November" << endl;
cout << "12. December" << endl;
cout << "Choose the month: ";
cin >> choice;
switch (choice)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
cout << "This month has 31 days" << endl;
break;
case 4:
case 6:
case 9:
case 11:
cout << "This month has 30 days" << endl;
break;
case 2:
{
int year;
cout << "Input the year: ";
cin >> year;
bool isLeapYear = (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0));
if (isLeapYear)
cout << "The month february has 29 days in year " << year << endl;
else
cout << "The month february has 28 days in year " << year << endl;
}
default:
cout << "ERROR. Invalid choice.";
}
}
Comments