CSC 1100 Lecture Notes - Lecture 8: Collation, Toro Rosso Str2, Switch Statement

11 views9 pages
Chapter 4:
Control Structures I (Selection)
Objectives
In this chapter, you will:
Learn about control structures
Examine relational operators
Discover how to use the selection control structures if, if…else
Examine int and bool data types and logical (Boolean) expressions
Examine logical operators
Objectives (cont’d.)
Explore how to form and evaluate logical (Boolean) expressions
Learn how relational operators work with the string type
Become aware of short-circuit evaluation
Learn how the conditional operator, ?:, works
Learn how to use pseudocode to develop, test, and debug a program
Objectives (cont’d.)
Discover how to use a switch statement in a program
Learn how to avoid bugs by avoiding partially understood concepts
Learn how to use the assert function to terminate a program
Control Structures
A computer can proceed:
In sequence
Selectively (branch): making a choice
Repetitively (iteratively): looping
By calling a function
Two most common control structures:
Selection
Repetition
Control Structures (cont’d.)
Selection: if and if...else
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 9 pages and 3 million more documents.

Already have an account? Log in
Execution of selection or repetition requires execution of a logical expression:
Evaluates to true or false
“8 is greater than 3”
Relational Operators (cont’d.)
Relational Operators and Simple Data Types
Conditional statements: only executed if certain conditions are met
Condition: represented by a logical (Boolean) expression that evaluates to a logical (Boolean)
value of true or false
Relational operators:
Allow comparisons
Require two operands (binary)
Evaluate to true or false
Relational Operators and Simple Data Types (cont’d.)
Relational operators can be used with all three simple data types:
8 < 15 evaluates to true
6 != 6 evaluates to false
2.5 > 5.8 evaluates to false
5.9 <= 7.5 evaluates to true
Comparing Characters
Expression of char values with relational operators
Result depends on machine’s collating sequence
ASCII character set
Logical (Boolean) expressions
Expressions such as 4 < 6 and 'R' > 'T’
Returns an integer value of 1 if the logical expression evaluates to true
Returns an integer value of 0 otherwise
One-Way Selection
One-way selection syntax:
Statement is executed if the value of the expression is true
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 9 pages and 3 million more documents.

Already have an account? Log in
Statement is bypassed if the value is false; program goes to the next statement
Expression is called a decision maker
One-Way Selection (cont’d.)
Two-Way Selection
Two-way selection syntax:
If expression is true, statement1 is executed; otherwise, statement2 is executed
statement1 and statement2 are any C++ statements
Two-Way Selection (cont’d.)
The int Data Type and Logical (Boolean) Expressions
Earlier versions of C++ did not provide built-in data types that had Boolean values
Logical expressions evaluate to either 1 or 0
Logical expression value was stored in a variable of the data type int
Can use the int data type to manipulate logical (Boolean) expressions
bool Data Type and Logical (Boolean) Expressions
The data type bool has logical (Boolean) values true and false
bool, true, and false are reserved words
The identifier true has the value 1
The identifier false has the value 0
Logical (Boolean) Operators and Logical Expressions
Logical (Boolean) operators: enable you to combine logical expressions
Logical (Boolean) Operators and Logical Expressions (cont’d.)
Logical (Boolean) Operators and Logical Expressions (cont’d.)
Logical (Boolean) Operators and Logical Expressions (cont’d.)
Order of Precedence
Relational and logical operators are evaluated from left to right
The associativity is left to right
Parentheses can override precedence
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 9 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Notes+

Unlimited access to class notes and textbook notes.

YearlyBest Value
75% OFF
$8 USD/m
Monthly
$30 USD/m
You will be charged $96 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.