COS 132
CHAPTER 4 – MAKING DECISIONS
UNIT 4.1- RELATIONAL OPERATORS
Relational operators allow you to compare numeric and char values and determine whether one is greater than, less
than, equal to, or not equal to another.
All of the relational operators are binary, which means they use two operands. Statements such as x > y is called a
relational expression.
THE VALUE OF A RELATIONSHIP
Relational expressions are also known as Boolean expressions, which means their value can be true or false.
WHAT IS TRUTH?
How does a comp store true and false in memory? It converts these two abstract states to numbers, true is represented
by the number 1 and false is represented by the number 0.
UNIT 4.2 – THE IF STATEMENT
The if statement can cause other statements to execute only under certain conditions. The if statement falls under the
category of a decision structure.
An action is only performed if a certain criteria is met/true, the action is conditionally executed bc it is performed only
when a certain condition exists.
BE CAREFUL W SEMICOLONS
If you place a semicolon after the if(expression) statement, the compiler will assume you are placing a null statement
there, the null statement is an empty statement that does nothing, this will terminate the if statement.
1|P a g e
, COS132 Chapter 4
PROGRAMMING STYLE AND THE IF STATEMENT
There are two important style rules:
The conditionally executed statement should appear on the line after the if statement.
The conditionally executed statement should be indented one ‘level’ from the if statement
COMPARING FLOATING-POINT NUMBERS
Because of the way floating-point numbers are stored in memory, rounding errors sometimes occur. This is because
some fractional numbers cannot be exactly represented using binary. To prevent round-off errors from causing this type
of problem, you should stick w greater than and less than comparisons w floating-point numbers.
AND NOW, BACK TO TRUTH
0 is still considered false, but values other than 0 are considered true, this means that any value, even a negative
number represents true.
Summary of rules we have looked at so far:
- When a relational expression is true – has the value of 1
- When a relational expression is false – has the value of 0
- Any expression that has the value of 0 is false, and vise versa
- Any expression that has the value of 1 is true, and visa versa
If (value)
Cout << “it is true!”
This if statement tests the contents of a variable, if the variable contains any number other than 0 the message will be
displayed.
UNIT 4.3 – EXPANDING THE IF STATEMENT
The if statement can conditionally execute a block of statements enclosed in brackets.
If (expression)
{
Statements; //block of code
Statements;
}
2|P a g e
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller jennamortonx. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $5.07. You're not tied to anything after your purchase.