Course Title: Computer Science I
Course Code: COSC 1020
Institution: Georgetown University
Overview: Understanding flow charts and decision structures.
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)
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)
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 to 1.1
if ((n – 1.1) <= 1e-5)
.
Usage: Act on boolean values and expressions.
Types:
AND (&&
)
OR (||
)
NOT (!
)
Must check complete Boolean expressions.
Example: To check a range, you use:
not (a < x < b)
but instead do (a < x && x < b)
.
Favor clarity with parentheses.
Precedence sequence:
NOT - evaluated first
AND - evaluated left to right
OR - evaluated left to right
if Statement
Structure:
A condition (boolean expression).
A block of statements executed if the condition is true.
Syntax:
if (boolean_expression) {
// statements if true
}
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.
Allows checking multiple mutually exclusive conditions.
Example:
if (first_condition) {
// true block
} else if (second_condition) {
// second block
} else {
// fallback block
}
Used for follow-up questions based on prior answers.
Example: Check user ID for age verification.
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:
//...
}
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 are translated into enumerated values for processing.
Offers flexibility in implementing logic based on user preferences.
Decision-making structures like if, switch, and enumerations help design interactive and flexible programs.