04.MakingDecisions
Course Information
Course Title: Computer Science I
Course Code: COSC 1020
Institution: Georgetown University
Chapter 4: Making Decisions
Overview: Understanding flow charts and decision structures.
Key Topics
Making Decisions
Relational operators (4.1)
Logical operators (4.7)
The if statement (4.2)
The if/else statement (4.3)
The if/else if statement (4.4)
Nested if statements (4.6)
More about blocks and scope (4.9)
User Interfaces
Menu-driven programs (4.5)
Validating user inputs (4.8)
Validating chars and strings (4.10)
The conditional operator (4.11)
The switch statement (4.12)
Enumerated data types (4.13)
Relational Operators
Function: Test the relationship between two values, often referred to as comparison operators.
Types of Relational Operators:
Testing Equality:
Equals:
==
Not Equals:
!=
Testing Order (Inequality Operators):
Strict Inequality:
>
(greater than),<
(less than)Nonstrict Inequality:
>=
(greater than or equal to),<=
(less than or equal to)
Returns: Boolean value (true/false)
Common Errors with Relational Operators
Assignment vs. Comparison:
Single equal sign
=
is for assignment.Double equal sign
==
is for comparison.
Floating-point Comparison:
Precision issues due to rounding can lead to unexpected false results.
Use ranges to check closeness:
n
is close to1.1
if((n – 1.1) <= 1e-5)
.
Logical Operators
Usage: Act on boolean values and expressions.
Types:
AND (
&&
)OR (
||
)NOT (
!
)
Writing Logical Expressions
Must check complete Boolean expressions.
Example: To check a range, you use:
not (a < x < b)
but instead do(a < x && x < b)
.
Logical Operator Precedence
Favor clarity with parentheses.
Precedence sequence:
NOT - evaluated first
AND - evaluated left to right
OR - evaluated left to right
The
if Statement
Structure:
A condition (boolean expression).
A block of statements executed if the condition is true.
Syntax:
if (boolean_expression) {
// statements if true
}
The if/else Statement
Concept: Mutually exclusive execution: either block 1 or block 2 runs, but never both.
Structure:
If condition is true, execute block 1.
If false, execute block 2.
The if/else if Statement
Allows checking multiple mutually exclusive conditions.
Example:
if (first_condition) {
// true block
} else if (second_condition) {
// second block
} else {
// fallback block
}
Nested if Statements
Used for follow-up questions based on prior answers.
Example: Check user ID for age verification.
The Switch Statement
A control structure for making choices among integer options.
Includes cases and can have a default case.
Usage Example:
switch(variable) {
case 'a':
//...
break;
default:
//...
}
Enumerated Data Types
Definition: A programmer-defined data type with specific values.
Allows for distinct variables that are meaningful.
Declaration Example:
enum Color {red, green, blue};
Color favorite = green;
User Inputs and Enumerations
User inputs are translated into enumerated values for processing.
Offers flexibility in implementing logic based on user preferences.
Conclusion
Decision-making structures like if, switch, and enumerations help design interactive and flexible programs.