السبت، 20 فبراير 2016

Control Structures

Control Structures



A condition is represented by a logical (Boolean) expression that can be true or false
Relational operators:
Allow comparisons
Require two operands (binary)
Evaluate to true or false



Relational Operators and Simple Data Types
You can use the relational operators 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 Floating-Point Numbers for Equality
Comparison of floating-point numbers for equality may not behave as you would expect
Example:
1.0 == 3.0/7.0 + 2.0/7.0 + 2.0/7.0 evaluates to false
Why?  3.0/7.0 + 2.0/7.0 + 2.0/7.0 = 0.99999999999999989
Solution: use a tolerance value
Example: fabs(x – y) < 0.000001

Example


Comparing Characters

Relational Operators and the
string Type
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

Suppose we have the following declarations:
string str1 = "Hello";
string str2 = "Hi";
string str3 = "Air";
string str4 = "Bill";
string str4 = "Big";






Logical (Boolean) Operators and Logical Expressions 
Order of Precedence
Relational and logical operators are evaluated from left to right
The associativity is left to right
Parentheses can override precedence

Example


ليست هناك تعليقات:

إرسال تعليق