Introduction to Conditional Conjunctions
Introduction to Conditional Conjunctions
Fundamentals of Conditional Conjunctions
Overview
In previous lessons, all the conditions we studied were dealing with one issue. Sometimes, you want to combine two or more conditions that address a common issue.
Practical Learning: Introducing Conditional Conjunctions
#include <iostream>
using namespace std;
int main()
{
cout << "========================================================" << endl;
cout << " - Georgia - State Income Tax -" << endl;
cout << "========================================================" << endl;
double addedAmount;
double taxRate;
cout << "Enter the information for tax preparation" << endl;
cout << ""Gross Salary: " << endl;
double grossSalary = double.Parse(ReadLine());
// Georgia
char filingStatus = "Married Filing Joint or Head of Household";
if(grossSalary >= 10000)
{
addedAmount = 340;
taxRate = 5.75;
}
else if(grossSalary >= 7000)
{
addedAmount = 190;
taxRate = 5.00;
}
else if(grossSalary >= 5000)
{
addedAmount = 110;
taxRate = 4.00;
}
else if(grossSalary >= 3000)
{
addedAmount = 50;
taxRate = 3.00;
}
else if(grossSalary >= 1000)
{
addedAmount = 10;
taxRate = 2.00;
}
else // if grossSalary < 1000:)
{
addedAmount = 0;
taxRate = 1.00;
}
double taxAmount = addedAmount + (grossSalary * taxRate / 100.00);
double net_pay = grossSalary - taxAmount;
cout << "========================================================" << endl;
cout << "- Georgia - State Income Tax -" << endl;
cout << "--------------------------------------------------------" << endl;
cout << "Gross Salary: {grossSalary:f}" << endl;
cout << "Filing Status: {filingStatus}" << endl;
cout << "Tax Rate: {taxRate:f}%" << endl;
cout << "Tax Amount: {taxAmount:f}" << endl;
cout << "Net Pay: {net_pay:f}" << endl;
cout << "========================================================" << endl;======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 999.99 ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 999.99 Filing Status: Married Filing Joint or Head of Household Tax Rate: 1.00% Tax Amount: 10.00 Net Pay: 989.99 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 1000 ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 1000.00 Filing Status: Married Filing Joint or Head of Household Tax Rate: 2.00% Tax Amount: 30.00 Net Pay: 970.00 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5726.87 ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5726.87 Filing Status: Married Filing Joint or Head of Household Tax Rate: 4.00% Tax Amount: 339.07 Net Pay: 5387.80 ======================================================== Press any key to close this window . . .
#include <iostream>
using namespace std;
int main()
{
cout << "========================================================" << endl;
cout << " - Georgia - State Income Tax -" << endl;
cout << "========================================================" << endl;
double addedAmount;
double taxRate;
cout << "Enter the information for tax preparation" << endl;
cout << ""Gross Salary: " << endl;
double grossSalary = double.Parse(ReadLine());
//Georgia
char filingStatus = "Single";
if( grossSalary >= 7000)
{
addedAmount = 230;
taxRate = 5.75;
}
else if(grossSalary >= 5250)
{
addedAmount = 143;
taxRate = 5.00;
}
else if(grossSalary >= 3750)
{
addedAmount = 83;
taxRate = 4.00;
}
else if(grossSalary >= 2250)
{
addedAmount = 38;
taxRate = 3.00;
}
else if(grossSalary >= 750)
{
addedAmount = 8;
taxRate = 2.00;
}
else // if grossSalary < 750:)
{
addedAmount = 0;
taxRate = 1.00;
}
double taxAmount = addedAmount + (grossSalary * taxRate / 100.00);
double net_pay = grossSalary - taxAmount;
cout << "========================================================" << endl;
cout << "- Georgia - State Income Tax -" << endl;
cout << "--------------------------------------------------------" << endl;
cout << "Gross Salary: {grossSalary:f}" << endl;
cout << "Filing Status: {filingStatus}" << endl;
cout << "Tax Rate: {taxRate:f}%" << endl;
cout << "Tax Amount: {taxAmount:f}" << endl;
cout << "Net Pay: {net_pay:f}" << endl;
cout << "========================================================" << endl;======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 749.99 ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 749.99 Filing Status: Single Tax Rate: 1.00% Tax Amount: 7.50 Net Pay: 742.49 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 750 ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 750.00 Filing Status: Single Tax Rate: 2.00% Tax Amount: 23.00 Net Pay: 727.00 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5726.87 ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5726.87 Filing Status: Single Tax Rate: 5.00% Tax Amount: 429.34 Net Pay: 5297.53 ======================================================== Press any key to close this window . . .
Nesting a Conditional Statement
A conditional statement has a body, which is from where the condition is defined to where its behavior ends. In the body of the conditional statement, you can create another conditional statement. This is referred to as nesting the condition. The condition nesting can be formulated as follows:
if( condition1 ) // The nesting, main, parent, first, or external condition
if( condition2 ) // The nested, child, second, or internal condition
statement(s)
If the code of a condition involves many lines of code, you can delimite its body with curly brackets:
if( condition1 )
{
if( condition2 )
{
statement(s)
}
}
In the same way, you can nest one conditional statement in one, then nest that new one in another conditional statement, and so on. This can be formulated as follows:
if( condition1 )
if( condition2 )
if( condition3 )
statement(s)
If a section of code is long, you can include its child code in curly brackets:
if( condition1 )
{
if( condition2 )
{
if( condition3 )
{
statement(s)
}
}
}
Practical Learning: Nesting a Conditional Statement
#include <iostream>
using namespace std;
int main()
{
cout << "========================================================" << endl;
cout << " - Georgia - State Income Tax -" << endl;
cout << "========================================================" << endl;
char filingStatus;
double taxRate, addedAmount;
cout << "Enter the information for tax preparation" << endl;
cout << ""Gross Salary: " << endl;
double grossSalary = double.Parse(ReadLine());
cout << "Filing Status" << endl;
cout << "s - Single" << endl;
cout << "c - Married Filing Joint or Head of Household" << endl;
cout << ""Enter Filing Status: " << endl;
string answer = ReadLine();
if(answer == 's')
{
filingStatus = "Single";
if (grossSalary >= 7000)
{
addedAmount = 230;
taxRate = 5.75;
}
else if (grossSalary >= 5250)
{
addedAmount = 143;
taxRate = 5.00;
}
else if (grossSalary >= 3750)
{
addedAmount = 83;
taxRate = 4.00;
}
else if (grossSalary >= 2250)
{
addedAmount = 38;
taxRate = 3.00;
}
else if (grossSalary >= 750)
{
addedAmount = 8;
taxRate = 2.00;
}
else // if grossSalary < 750:)
{
addedAmount = 0;
taxRate = 1.00;
}
}
else
{
filingStatus = "Married Filing Joint or Head of Household";
if (grossSalary >= 10000)
{
addedAmount = 340;
taxRate = 5.75;
}
else if (grossSalary >= 7000)
{
addedAmount = 190;
taxRate = 5.00;
}
else if (grossSalary >= 5000)
{
addedAmount = 110;
taxRate = 4.00;
}
else if (grossSalary >= 3000)
{
addedAmount = 50;
taxRate = 3.00;
}
else if (grossSalary >= 1000)
{
addedAmount = 10;
taxRate = 2.00;
}
else // if grossSalary < 1000:)
{
addedAmount = 0;
taxRate = 1.00;
}
}
double taxAmount = addedAmount + (grossSalary * taxRate / 100.00);
double net_pay = grossSalary - taxAmount;
cout << "========================================================" << endl;
cout << "- Georgia - State Income Tax -" << endl;
cout << "--------------------------------------------------------" << endl;
cout << "Gross Salary: {grossSalary:f}" << endl;
cout << "Filing Status: {filingStatus}" << endl;
cout << "Tax Rate: {taxRate:f}%" << endl;
cout << "Tax Amount: {taxAmount:f}" << endl;
cout << "Net Pay: {net_pay:f}" << endl;
cout << "========================================================" << endl;======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5368.47 Filing Status s - Single c - Married Filing Joint or Head of Household Enter Filing Status: s ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5368.47 Filing Status: Single Tax Rate: 5.00% Tax Amount: 411.42 Net Pay: 4957.05 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5368.47 Filing Status s - Single c - Married Filing Joint or Head of Household Enter Filing Status: c ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5368.47 Filing Status: Married Filing Joint or Head of Household Tax Rate: 4.00% Tax Amount: 324.74 Net Pay: 5043.73 ======================================================== Press any key to close this window . . .
#include <iostream>
using namespace std;
int main()
{
cout << "========================================================" << endl;
cout << " - Georgia - State Income Tax -" << endl;
cout << "========================================================" << endl;
char filingStatus;
double taxRate, addedAmount;
cout << "Enter the information for tax preparation" << endl;
cout << ""Gross Salary: " << endl;
double grossSalary = double.Parse(ReadLine());
cout << "Filing Status" << endl;
cout << "1 - Single" << endl;
cout << "s - Married Filing Separate" << endl;
cout << "j - Married Filing Joint or Head of Household" << endl;
cout << ""Enter Filing Status: " << endl;
string answer = ReadLine();
//Georgia
if(answer == 'j')
{
filingStatus = "Married Filing Joint or Head of Household";
if (grossSalary >= 10000)
{
addedAmount = 340;
taxRate = 5.75;
}
else if (grossSalary >= 7000)
{
addedAmount = 190;
taxRate = 5.00;
}
else if (grossSalary >= 5000)
{
addedAmount = 110;
taxRate = 4.00;
}
else if (grossSalary >= 3000)
{
addedAmount = 50;
taxRate = 3.00;
}
else if (grossSalary >= 1000)
{
addedAmount = 10;
taxRate = 2.00;
}
else // if grossSalary < 1000:)
{
addedAmount = 0;
taxRate = 1.00;
}
}
else if(answer == 's')
{
filingStatus = "Married Filing Separate";
if(grossSalary >= 5000)
{
addedAmount = 170;
taxRate = 5.75;
}
else if(grossSalary >= 3_500)
{
addedAmount = 95;
taxRate = 5.00;
}
else if(grossSalary >= 2_500)
{
addedAmount = 55;
taxRate = 4.00;
}
else if(grossSalary >= 1_500)
{
addedAmount = 25;
taxRate = 3.00;
}
else if(grossSalary >= 500)
{
addedAmount = 5;
taxRate = 2.00;
}
else // if( grossSalary < 1000)
{
addedAmount = 0;
taxRate = 1.00;
}
}
else // if(answer == Anything)
{
filingStatus = "Single";
if (grossSalary >= 7000)
{
addedAmount = 230;
taxRate = 5.75;
}
else if (grossSalary >= 5250)
{
addedAmount = 143;
taxRate = 5.00;
}
else if (grossSalary >= 3750)
{
addedAmount = 83;
taxRate = 4.00;
}
else if (grossSalary >= 2250)
{
addedAmount = 38;
taxRate = 3.00;
}
else if (grossSalary >= 750)
{
addedAmount = 8;
taxRate = 2.00;
}
else // if grossSalary < 750:)
{
addedAmount = 0;
taxRate = 1.00;
}
}
double taxAmount = addedAmount + (grossSalary * taxRate / 100.00);
double net_pay = grossSalary - taxAmount;
cout << "========================================================" << endl;
cout << "- Georgia - State Income Tax -" << endl;
cout << "--------------------------------------------------------" << endl;
cout << "Gross Salary: {grossSalary:f}" << endl;
cout << "Filing Status: {filingStatus}" << endl;
cout << "Tax Rate: {taxRate:f}%" << endl;
cout << "Tax Amount: {taxAmount:f}" << endl;
cout << "Net Pay: {net_pay:f}" << endl;
cout << "========================================================" << endl;======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5278.75 Filing Status 1 - Single s - Married Filing Separate j - Married Filing Joint or Head of Household Enter Filing Status: j ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5278.75 Filing Status: Married Filing Joint or Head of Household Tax Rate: 4.00% Tax Amount: 321.15 Net Pay: 4957.60 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5278.75 Filing Status 1 - Single s - Married Filing Separate j - Married Filing Joint or Head of Household Enter Filing Status: s ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5278.75 Filing Status: Married Filing Separate Tax Rate: 5.75% Tax Amount: 473.53 Net Pay: 4805.22 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5278.75 Filing Status 1 - Single s - Married Filing Separate j - Married Filing Joint or Head of Household Enter Filing Status: Separated from husband, living as a single person ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5278.75 Filing Status: Single Tax Rate: 5.00% Tax Amount: 406.94 Net Pay: 4871.81 ======================================================== Press any key to close this window . . .
A Logical Conjunction
Remember that you can nest one condition in another condition as in:
if( condition1 )
if( condition2 )
statement(s)
Remember that you can (and in some cases you must) use curly brackets to delimit the section of a conditional statement:
if( condition1 )
{
if( condition2 )
{
statement(s)
}
}
When you nest a condition, you are in fact indicating that "if condition1 verifies, (then) if condition2 verifies, do this...". Instead of nesting the second condition, in some (but not all) cases, you can combine the conditions. This operation is referred to as a Boolean conjunction, or a logical conjunction, or simply a conjunction.
To support Boolean conjunctions, the C++ language provides an operator named and. The formula to perform a Boolean conjunction is:
condition1 and condition2
As an alternative to the and operator, both the C and the C++ language provide an operator represented with two ampersands &&. It can be represented as follows:
condition1 && condition2
In your C++ code, you can use either the and or the && operator for Boolean conjunction.
As we saw in the previous lesson and previous sections, each condition is formulated as a Boolean operation with two operands separated by a Boolean operator:
operand1 Boolean-operator operand2
The whole expression can be written in the parenthesis of a conditional statement, as in:
if(condition1 and condition2) statement(s); if(condition1 && condition2) statement(s);
Therefore, if using a conditional statement, the whole Boolean conjunction can be formulated as follows:
if(operand1 operator1 operand2 and operand3 operator2 operand4) statement(s); if(operand1 operator1 operand2 && operand3 operator2 operand4) statement(s);
During a conjunction operation, the first condition, condition1, is checked. Then the second condition, condition2, also is checked (regardless of the result of the first condition).
You must formulate each condition to produce a true or a false result. The result is as follows:
The conjunction operation can be resumed as follows:
| Condition-1 | Contition 2 | Condition-1 && Condition-2 |
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
Conjunctions and Boolean Operators
Introduction
We mentioned that each condition in a conjunction is formulated as a Boolean expression. Here is an example:
condition1 degree == 1 && validSecurity == true condition2
This means that you will use the Boolean operators we saw already but there are some details to keep in mind.
Checking for Equality
One of the operations you can perform in a conjunction is to check for equality. Since a variable cannot be equal to two values, you should use a different variable for each condition. Here is an example:
#include <iostream>
using namespace std;
int main()
{
int degree;
bool validSecurity;
cout << "To grant you this job, we need to ask a few questions." << endl;
cout << "Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? ";
cin >> degree;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
cout << "Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): ";
cin >> validSecurity;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
if(degree == 1 && validSecurity == true)
cout << "Welcome on board." << endl;
else
cout << "We will get back to you..." << endl;
cout << "=======================================================================================================" << endl;
}
Here is an example of running the program:
To grant you this job, we need to ask a few questions. Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? 1 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): 1 ------------------------------------------------------------------------------------------------------- Welcome on board. ======================================================================================================= Press any key to close this window . . .
Here is another example of running the program:
To grant you this job, we need to ask a few questions. Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? 1 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): 2000000 ------------------------------------------------------------------------------------------------------- Welcome on board. ======================================================================================================= Press any key to close this window . . .
Here is another example of running the program:
To grant you this job, we need to ask a few questions. Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? 1 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): Not yet ------------------------------------------------------------------------------------------------------- We will get back to you... ======================================================================================================= Press any key to close this window . . .
Isolating a Condition of a Conjunction
Conditional statements can be difficult to read, especially when they are long. When it comes to conjunctions, to make your code easy to read, you can put each condition in its own parentheses. Based on this, the above code can be written as follows:
#include <iostream>
using namespace std;
int main()
{
int degree;
bool validSecurity;
cout << "To grant you this job, we need to ask a few questions." << endl;
cout << "Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? ";
cin >> degree;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
cout << "Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): ";
cin >> validSecurity;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
if( (degree == 1) && (validSecurity == true) )
cout << "Welcome on board." << endl;
else
cout << "We will get back to you..." << endl;
cout << "=======================================================================================================" << endl;
} )
Conjunctions and the Ternary Operator
Remember that if you have an if...else conditional statement with only one if statment and only one else statement, you can use a ternary operator using a combination of ? and : operators. Remember that the formula of the ternary operator is:
condition ? statement1 : statement2;
In this formula, you can use a conjunction in the condition. If the condition is true, statement1 would execute. If not, statement2 would execute. As a result, the above code can be written as follows:
#include <iostream>
using namespace std;
int main()
{
int degree;
bool validSecurity;
cout << "To grant you this job, we need to ask a few questions." << endl;
cout << "Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? ";
cin >> degree;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
cout << "Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): ";
cin >> validSecurity;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
string decision = degree == 1 && validSecurity == true ? "Welcome on board." : "We will get back to you...";
cout << decision << endl;
cout << "=======================================================================================================" << endl;
}
The above code can be difficult to read. To make it a little easier to read, you can put the conjunction in parentheses. Here is an example:
string decision = (degree == 1 && validSecurity == true) ? "Welcome on board." : "We will get back to you...";
To make the code further easier to read, you can put each par of the conjunction in its owndparentheses. Here is an example:
string decision = ((degree == 1) && (validSecurity == true)) ? "Welcome on board." : "We will get back to you...";
Negating a Conjunction
To negate an logical operation, we already know that we can use the ! operator. You can apply this operator in a conjunction. You have various options.
Because a conjunction is a whole Boolean operation by itself, you can negate the whole conjunction. To do this, put the whole operaion in parentheses and precede it with the ! operator. Here is an example:
#include <iostream>
using namespace std;
int main()
{
int degree;
bool validSecurity;
cout << "To grant you this job, we need to ask a few questions." << endl;
cout << "Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? ";
cin >> degree;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
cout << "Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): ";
cin >> validSecurity;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
if( !(degree == 1 && validSecurity == true))
cout << "Welcome on board." << endl;
else
cout << "We will get back to you..." << endl;
cout << "=======================================================================================================" << endl;
}
Here is an example of running the program:
o grant you this job, we need to ask a few questions. Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? 1 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): 1 ------------------------------------------------------------------------------------------------------- We will get back to you... ======================================================================================================= Press any key to close this window . . .
Negating a Condition of a Conjunction
We saw that, when creating a conjunction, to make your code easy to read, you can put each comparison operation in its own parentheses. Here is an example:
#include <iostream>
using namespace std;
int main()
{
int answerDegree;
bool validSecurity;
cout << "Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? ";
cin >> answerDegree;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
cout << "Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): ";
cin >> validSecurity;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
auto decision = "empty";
if( (answerDegree == 1) && (validSecurity == true) )
decision = "Welcome on board.";
else
decision = "We will get back to you...";
cout << decision << endl;
cout << "=======================================================================================================" << endl;
}
Here is an example of running the program:
Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? 1 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): 2500 ------------------------------------------------------------------------------------------------------- Welcome on board. ======================================================================================================= Press any key to close this window . . .
Here is another example of running the program:
Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? 1000 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): 2500 ------------------------------------------------------------------------------------------------------- We will get back to you... ======================================================================================================= Press any key to close this window . . .
If you want, you can negate only one of the conditions. In that case, that condition must be included in parentheses. Then, precede that condition with the ! operator. Here is an example:
#include <iostream>
using namespace std;
int main()
{
int answerDegree;
bool validSecurity;
cout << "Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? ";
cin >> answerDegree;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
cout << "Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): ";
cin >> validSecurity;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
auto decision = "empty";
if( !(answerDegree == 0) && (validSecurity == true))
decision = "Welcome on board.";
else
decision = "We will get back to you...";
cout << decision << endl;
cout << "=======================================================================================================" << endl;
}
Here is an example of running the program:
Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? 1000 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): 2000 ------------------------------------------------------------------------------------------------------- Welcome on board. ======================================================================================================= Press any key to close this window . . .
If necessary, you can negate each of both conditions. Here is an example:
#include <iostream>
using namespace std;
int main()
{
int answerDegree;
bool validSecurity;
cout << "Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? ";
cin >> answerDegree;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
cout << "Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): ";
cin >> validSecurity;
cout << "-------------------------------------------------------------------------------------------------------" << endl;
auto decision = "empty";
if( !(answerDegree == 1) && !(validSecurity == true) )
decision = "We will get back to you...";
else
decision = "Welcome on board.";
cout << decision << endl;
cout << "=======================================================================================================" << endl;
}
Here is an example of running the program:
Do you have a college degree(undergraduate or graduate) (Type 1 for Yes or 0 for No)? 1 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type any number for Yes or anything for No): 1 ------------------------------------------------------------------------------------------------------- Welcome on board. ======================================================================================================= Press any key to close this window . . .
Checking for Non-Equality
In Boolean algebra, to check whether a certain variable holds a value different from one you have, you can use the != operator. Because a variable can easily be different from two values, you can use the same variable on two conditions of a conjunction. Here are examples:
#include <iostream>
using namespace std;
int main()
{
cout << "Traffic Tickets Management" << endl;
cout << "This application allows the county to set the ticket amount from an image filmed by a traffic camera." << endl;
cout << "-----------------------------------------------------------------------------------------------------" << endl;
int ticket = 45;
cout << "Road Status" << endl;
cout << "R - Regular traffic" << endl;
cout << "S - Sensitive Area" << endl;
cout << "Z - School Zone" << endl;
cout << "U - Unknown or information not available" << endl;
cout << "Enter the road status (Type R, S, or Z)? ";
char status;
cin >> status;
bool valid = status != 'R' && status != 'U';
if (valid == true)
ticket = 90;
cout << "=====================================================================================================" << endl;
cout << "Ticket Amount: " << ticket << endl;
cout << "=====================================================================================================" << endl;
}
Here is an example of testing the program:
Traffic Tickets Management This application allows the county to set the ticket amount from an image filmed by a traffic camera. ----------------------------------------------------------------------------------------------------- Road Status R - Regular traffic S - Sensitive Area Z - School Zone U - Unknown or information not available Enter the road status (Type R, S, or Z)? R ===================================================================================================== Ticket Amount: 45 ===================================================================================================== Press any key to close this window . . .
Here is another example of testing the program:
Traffic Tickets Management This application allows the county to set the ticket amount from an image filmed by a traffic camera. ----------------------------------------------------------------------------------------------------- Road Status R - Regular traffic S - Sensitive Area Z - School Zone U - Unknown or information not available Enter the road status (Type R, S, or Z)? S ===================================================================================================== Ticket Amount: 90 ===================================================================================================== Press any key to close this window . . .
A Conjunction for Non-Equivalence
The <, <=, >, and >= operators are also available for conditional statements. You can apply any of those operators to a condition of a conjunction. You can use the same variable for each condition or you can use a different variable for each condition. Here is an example that checks that a number is in a certain range:
#include <iostream>
using namespace std;
int main()
{
cout << "To grant you this job, we need to ask a few questions." << endl;
cout << "Levels of Security Clearance:" << endl;
cout << "0. Unknown or None" << endl;
cout << "1. Non-Sensitive" << endl;
cout << "2. National Security - Non-Critical Sensitive" << endl;
cout << "3. National Security - Critical Sensitive" << endl;
cout << "4. National Security - Special Sensitive" << endl;
cout << "----------------------------------------------------------------" << endl;
cout << "Type your level(1 - 4): ";
int level;
cin >> level;
cout << "================================================================" << endl;
if (level >= 2 && level <= 4)
cout << "Welcome on board." << endl;
else
cout << "We will get back to you..." << endl;
cout << "================================================================";
}
Here is an example of running the program:
================================================================ To grant you this job, we need to ask a few questions. Levels of Security Clearance: 0. Unknown or None 1. Non-Sensitive 2. National Security - Non-Critical Sensitive 3. National Security - Critical Sensitive 4. National Security - Special Sensitive ---------------------------------------------------------------- Type your level (1-4): 2 ================================================================ Welcome on board. ================================================================ Press any key to close this window . . .
Here is another example of running the program:
================================================================ To grant you this job, we need to ask a few questions. Levels of Security Clearance: 0. Unknown or None 1. Non-Sensitive 2. National Security - Non-Critical Sensitive 3. National Security - Critical Sensitive 4. National Security - Special Sensitive ---------------------------------------------------------------- Type your level (1-4): 1 ================================================================ We will get back to you... ================================================================ Press any key to close this window . . .
In the same way, you can use a combination of the Boolean operators any appropriate way you want. Here are examples where one condition uses the == operator while the other condition uses a >= operator:
#include <iostream>
using namespace std;
int main()
{
string name = "Gabrielle Towa";
double timeWorked = 42.50;
string status = "Full-Time";
bool verdict = status == "Full-Time" && timeWorked >= 40.00;
cout << "Employee Name: " << name << endl;
cout << "Time Worked: " << timeWorked << endl;
cout << "Employment Status: " << status << endl;
cout << "Condition-1 AND Condition-2: " << verdict << endl;
cout << "--------------------------------------------------------------------------------------" << endl;
// Both conditions are True:
if (verdict == true)
cout << "The time sheet has been approved." << endl;
else
cout << "Part-time employees are not allowed to work overtime." << endl;
cout << "======================================================================================" << endl;
name = "Stephen Anders";
status = "Full-Time";
timeWorked = 38.00;
verdict = status == "Full-Time" && timeWorked >= 40.00;
cout << "Employee Name: " << name << endl;
cout << "Time Worked: " << timeWorked << endl;
cout << "Employment Status: " << status << endl;
cout << "Condition-1 AND Condition-2: " << verdict << endl;
cout << "--------------------------------------------------------------------------------------" << endl;
// The first condition is True AND the second condition is False:
if (verdict == true)
cout << "The time sheet has been approved." << endl;
else
cout << "Time Sheet under review: Every full-time employee must work at least 40 hours a week." << endl;
cout << "======================================================================================" << endl;
name = "Rose Sandt";
status = "Part-Time";
timeWorked = 44.00;
verdict = status == "Full-Time" && timeWorked >= 40.00;
cout << "Employee Name: " << name << endl;
cout << "Time Worked: " << timeWorked << endl;
cout << "Employment Status: " << status << endl;
cout << "Condition-1 AND Condition-2: " << verdict << endl;
cout << "--------------------------------------------------------------------------------------" << endl;
// The first condition is False AND the second condition is True:
if (verdict == true)
cout << "The time sheet has been approved." << endl;
else
cout << "Part-time employees are not allowed to work overtime." << endl;
cout << "======================================================================================" << endl;
name = "Joseph Lee";
status = "Part-Time";
timeWorked = 36.50;
verdict = status == "Full-Time" && timeWorked >= 40.00;
cout << "Employee Name: " << name << endl;
cout << "Time Worked: " << timeWorked << endl;
cout << "Employment Status: " << status << endl;
cout << "Condition-1 AND Condition-2: " << verdict << endl;
cout << "--------------------------------------------------------------------------------------" << endl;
// The first condition is False AND the second condition is False:
if (verdict == true)
cout << "Part-time employees work part-time." << endl;
else
cout << "Part-time employees are not allowed to work overtime." << endl;
cout << "======================================================================================";
}
This would produce:
Employee Name: Gabrielle Towa Time Worked: 42.5 Employment Status: Full-Time Condition-1 AND Condition-2: 1 -------------------------------------------------------------------------------------- The time sheet has been approved. ====================================================================================== Employee Name: Stephen Anders Time Worked: 38 Employment Status: Full-Time Condition-1 AND Condition-2: 0 -------------------------------------------------------------------------------------- Time Sheet under review: Every full-time employee must work at least 40 hours a week. ====================================================================================== Employee Name: Rose Sandt Time Worked: 44 Employment Status: Part-Time Condition-1 AND Condition-2: 0 -------------------------------------------------------------------------------------- Part-time employees are not allowed to work overtime. ====================================================================================== Employee Name: Joseph Lee Time Worked: 36.5 Employment Status: Part-Time Condition-1 AND Condition-2: 0 -------------------------------------------------------------------------------------- Part-time employees are not allowed to work overtime. ====================================================================================== Press any key to close this window . . .
In the above example, we wrote each conjunction as one unit (such as status == "Full-Time" & timeWorked >= 40.00). Remember that, when creating a logical conjunction, to make your code easy to read, you should put each condition in its own parenthesis.
Practical Learning: Introducing Boolean Conjunctions
#include <iostream>
using namespace std;
int main()
{
cout << "============================================" << endl;
cout << " - Missouri - State Income Tax -" << endl;
cout << "============================================" << endl;
double taxRate;
int amountAdded;
cout << "Enter the information for tax preparation" << endl;
cout << ""Gross Salary: " << endl;
double grossSalary = double.Parse(ReadLine());
//Missouri
if(grossSalary >= 0.00 and <= 106)
{
amountAdded = 0;
taxRate = 0.00;
}
else if (grossSalary > 106 and <= 1_073)
{
amountAdded = 0;
taxRate = 1.50;
}
else if (grossSalary > 1_073 and <= 2_146)
{
amountAdded = 16;
taxRate = 2.00;
}
else if (grossSalary > 2_146 and <= 3_219)
{
amountAdded = 37;
taxRate = 2.50;
}
else if (grossSalary > 3_219 and <= 4_292)
{
amountAdded = 64;
taxRate = 3.00;
}
else if (grossSalary > 4_292 and <= 5_365)
{
amountAdded = 96;
taxRate = 3.50;
}
else if (grossSalary > 5_365 and <= 6_438)
{
amountAdded = 134;
taxRate = 4.00;
}
else if (grossSalary > 6_438 and <= 7_511)
{
amountAdded = 177;
taxRate = 4.50;
}
else if (grossSalary > 7_511 and <= 8_584)
{
amountAdded = 225;
taxRate = 5.00;
}
else // if(grossSalary > 8_584)
{
amountAdded = 279;
taxRate = 5.40;
}
double taxAmount = amountAdded + (grossSalary * taxRate / 100.00);
double netPay = grossSalary - taxAmount;
cout << "==============================================" << endl;
cout << "- Missouri - State Income Tax -" << endl;
cout << "----------------------------------------------" << endl;
cout << "Gross Salary: {grossSalary:f}" << endl;
cout << "Tax Rate: {taxRate:f}%" << endl;
cout << "Amount Added: {amountAdded:f}" << endl;
cout << "----------------------------------------------" << endl;
cout << "Tax Amount: {taxAmount:f}" << endl;
cout << "Net Pay: {netPay:f}" << endl;
cout << "==============================================" << endl;============================================ - Missouri - State Income Tax - ============================================ Enter the information for tax preparation Gross Salary: 3688.97 ============================================== - Missouri - State Income Tax - ---------------------------------------------- Gross Salary: 3688.97 Tax Rate: 3.00% Amount Added: 64.00 ---------------------------------------------- Tax Amount: 174.67 Net Pay: 3514.30 ============================================== Press any key to close this window . . .
#include <iostream>
using namespace std;
int main()
{
cout << "============================================" << endl;
cout << " - Missouri - State Income Tax -" << endl;
cout << "============================================" << endl;
double taxRate;
int amountAdded;
cout << "Enter the information for tax preparation" << endl;
cout << ""Gross Salary: " << endl;
double grossSalary = double.Parse(ReadLine());
//Missouri
if (grossSalary is (>= 0.00 and <= 106))
{
amountAdded = 0;
taxRate = 0.00;
}
else if (grossSalary is (> 106 and <= 1_073))
{
amountAdded = 0;
taxRate = 1.50;
}
else if (grossSalary is (> 1_073 and <= 2_146))
{
amountAdded = 16;
taxRate = 2.00;
}
else if (grossSalary is (> 2_146 and <= 3_219))
{
amountAdded = 37;
taxRate = 2.50;
}
else if (grossSalary is (> 3_219 and <= 4_292))
{
amountAdded = 64;
taxRate = 3.00;
}
else if (grossSalary is (> 4_292 and <= 5_365))
{
amountAdded = 96;
taxRate = 3.50;
}
else if (grossSalary is (> 5_365 and <= 6_438))
{
amountAdded = 134;
taxRate = 4.00;
}
else if (grossSalary is (> 6_438 and <= 7_511))
{
amountAdded = 177;
taxRate = 4.50;
}
else if (grossSalary is (> 7_511 and <= 8_584))
{
amountAdded = 225;
taxRate = 5.00;
}
else // if(grossSalary > 8_584)
{
amountAdded = 279;
taxRate = 5.40;
}
double taxAmount = amountAdded + (grossSalary * taxRate / 100.00);
double netPay = grossSalary - taxAmount;
cout << "==============================================" << endl;
cout << "- Missouri - State Income Tax -" << endl;
cout << "----------------------------------------------" << endl;
cout << "Gross Salary: {grossSalary:f}" << endl;
cout << "Tax Rate: {taxRate:f}%" << endl;
cout << "Amount Added: {amountAdded:f}" << endl;
cout << "----------------------------------------------" << endl;
cout << "Tax Amount: {taxAmount:f}" << endl;
cout << "Net Pay: {netPay:f}" << endl;
cout << "==============================================" << endl;============================================ - Missouri - State Income Tax - ============================================ Enter the information for tax preparation Gross Salary: 6884.65 ============================================== - Missouri - State Income Tax - ---------------------------------------------- Gross Salary: 6884.65 Tax Rate: 4.50% Amount Added: 177.00 ---------------------------------------------- Tax Amount: 486.81 Net Pay: 6397.84 ============================================== Press any key to close this window . . .
#include <iostream>
using namespace std;
int main()
{
cout << "============================================" << endl;
cout << " - Missouri - State Income Tax -" << endl;
cout << "============================================" << endl;
double taxRate;
int amountAdded;
cout << "Enter the information for tax preparation" << endl;
cout << ""Gross Salary: " << endl;
double grossSalary = double.Parse(ReadLine());
//Missouri
if(grossSalary is (>= 0.00) and (<= 106))
{
amountAdded = 0;
taxRate = 0.00;
}
else if(grossSalary is (> 106) and (<= 1_073))
{
amountAdded = 0;
taxRate = 1.50;
}
else if(grossSalary is (> 1_073) and (<= 2_146))
{
amountAdded = 16;
taxRate = 2.00;
}
else if(grossSalary is (> 2_146) and (<= 3_219))
{
amountAdded = 37;
taxRate = 2.50;
}
else if(grossSalary is (> 3_219) and (<= 4_292))
{
amountAdded = 64;
taxRate = 3.00;
}
else if(grossSalary is (> 4_292) and (<= 5_365))
{
amountAdded = 96;
taxRate = 3.50;
}
else if(grossSalary is (> 5_365) and (<= 6_438))
{
amountAdded = 134;
taxRate = 4.00;
}
else if(grossSalary is (> 6_438) and (<= 7_511))
{
amountAdded = 177;
taxRate = 4.50;
}
else if(grossSalary is (> 7_511) and (<= 8_584))
{
amountAdded = 225;
taxRate = 5.00;
}
else // if(grossSalary > 8_584)
{
amountAdded = 279;
taxRate = 5.40;
}
double taxAmount = amountAdded + (grossSalary * taxRate / 100.00);
double netPay = grossSalary - taxAmount;
cout << "==============================================" << endl;
cout << "- Missouri - State Income Tax -" << endl;
cout << "----------------------------------------------" << endl;
cout << "Gross Salary: {grossSalary:f}" << endl;
cout << "Tax Rate: {taxRate:f}%" << endl;
cout << "Amount Added: {amountAdded:f}" << endl;
cout << "----------------------------------------------" << endl;
cout << "Tax Amount: {taxAmount:f}" << endl;
cout << "Net Pay: {netPay:f}" << endl;
cout << "==============================================" << endl;============================================ - Missouri - State Income Tax - ============================================ Enter the information for tax preparation Gross Salary: 7884.65 ============================================== - Missouri - State Income Tax - ---------------------------------------------- Gross Salary: 7884.65 Tax Rate: 5.00% Amount Added: 225.00 ---------------------------------------------- Tax Amount: 619.23 Net Pay: 7265.42 ============================================== Press any key to close this window . . .
Combining Various Conjunctions
Depending on your goal, if two conditions are not enough, you can create as many conjunctions as you want. The formula to follow is:
condition1 and condition2 and condition3 and . . . and condition_n condition1 && condition2 && condition3 && . . . && condition_n
When the expression is checked, if any of the operations is false, the whole operation is false. The only time the whole operation is true is if all the operations are true.
Of course, you can nest a Boolean condition inside another conditional statement.
Practical Learning: Ending the Lesson
| Previous | Copyright © 2001-2026, FunctionX | Thursday 25 September 2025, 19:35 | Next |