COS132 Chapter 4
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
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