الأربعاء، 9 مارس 2016
الثلاثاء، 8 مارس 2016
الاثنين، 7 مارس 2016
السبت، 5 مارس 2016
السبت، 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
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
الجمعة، 19 فبراير 2016
cin and the Extraction Operator >>
<<cin
•The syntax of an input statement using cin and the extraction operator >> is:
cin >> variable 1>>variable2;
•The extraction operator >> is binary
−Left-side
operand is an input stream variable
•Example:
cin
−Right-side
operand is a variable
•No difference between a single cin with multiple variables and multiple cin statements with one variable
•When scanning, >> skips all whitespace
−Blanks
and certain nonprintable characters
>> distinguishes between character 2 and number 2 by the right-side operand of >>
−If
type char or int (or double),
the 2 is treated as a character or as a number 2
I/O Streams and Standard I/O Devices
I/O: sequence of bytes (stream of
bytes)
from source to destination
Bytes
are usually characters, unless program requires other types of information
Stream: sequence of characters from
source to
destination
Input
stream: sequence of characters
from an input
device to the computer
Output
stream: sequence of
characters
from the
computer to an output
device
•Use iostream header file to
extract (receive) data
from keyboard
and send output to the screen
−Contains
definitions of two data
types:
•istream -
input stream
•ostream -
output stream
−Has
two variables:
•cin -
stands for common input
•cout -
stands for common output
•To use cin and cout, the
preprocessor directive
#include
<iostream>
must
be used
•Variable declaration is
similar to:
−istream cin;
−ostream cout;
•Input
stream variables:
type
istream
•Output
stream variables:
type ostream
chapter 3 part 3 :Arithmetic Operator and Operator Precedence
Arithmetic Operators and
Operator Precedence
C++ arithmetic operators:
−+
addition
−-
subtraction
−*
multiplication
−/
division
−%
modulus operator
+, -, *, and / can be used with integral
and floating-point data types
Operators can be unary or binary
Order
of Precedence
All operations inside of () are
evaluated
first
*, /, and % are at the same level of
precedence and are evaluated
next
+ and – have the same level of
precedence
and are evaluated last
When operators are on the same
level
−Performed
from left to right
(associativity)
3
* 7 - 6 + 2 * 5 / 4 + 6
means
(((3 * 7) – 6) + ((2 * 5) /
4
)) +
6
Expressions
•If all operands are integers
−Expression
is called an integral expression
•Yields
an integral result
•Example:
2 + 3 * 5
•If all operands are floating-point
−Expression
is called a floating-point expression
•Yields
a floating-point result
•Example:
12.8 * 17.5 - 34.50
Mixed
Expressions
•Mixed expression:
−Has
operands of different data types
−Contains
integers and floating-point
•Examples of mixed expressions:
2 + 3.5
6
/ 4 + 3.9
chapter 3 part2
int Data Type
•Examples:
-6728
0
78
+763
•Positive integers do not need a + sign
•No commas are used within an integer
−Commas
are used for separating items in a list
bool Data Type
•bool type
Two
values: true and false
Manipulate
logical (Boolean)
expressions
true and false are called logical
values
bool, true, and false are
reserved words
char Data Type
The smallest integral data type
الاشتراك في:
الرسائل (Atom)