Control Structures 1
Control Structures 1
Control Structures I
(Selection)
ESCOMP LESSON 7
Control Structures
• Relational operators:
− Allow comparisons
− Require two operands (binary)
− Return 1 if expression is true, 0 otherwise
• Comparing values of different data types may
produce unpredictable results
− For example, 8 < '5' should not be done
• Any nonzero value is treated as true
ASCII
Comparing string Types
• Relational operators can be applied to strings
• Strings are compared character by character,
starting with the first character
• Comparison continues until either a mismatch
is found, or all characters are found equal
• If two strings of different lengths are compared
and the comparison is equal to the last
character of the shorter string
− The shorter string is less than the larger string
string Comparison Example
• Suppose we have the following declarations:
string str1 = "Hello";
string str2 = "Hi";
string str3 = "Air";
string str4 = "Bill";
Logical (Boolean) Operators
• Logical (Boolean) operators enable you to
combine logical expressions
• Three logical (Boolean) operators:
! - not
&& – and
|| - or
• Logical operators take logical values as operands
and yield logical values as results
• ! is unary; && and || are binary operators
• Putting ! in front of a logical expression reverses
its value
Precedence of Operators
statement