Hydrothermal Synthesis of Nanomaterials Andrew van Bommel January 18 th, 2006.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon...
-
Upload
jane-davis -
Category
Documents
-
view
213 -
download
0
Transcript of CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon...
![Page 1: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/1.jpg)
CSci 125
Lecture 10
Martin van Bommel
![Page 2: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/2.jpg)
Simple Statements
• Expression followed by semicolon
• Assignments
total = n1 + n2;• Function calls
printf(”Hello.\n”);• Useless statements
n1 + n2;
![Page 3: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/3.jpg)
Embedded Assignments
• Assignment expression can be used as part of a larger expression in a statement
• Its value is the value assigned
z = (x = 6) + y;• x is assigned value 6, then z assigned 6 + y• Difficult to read• Used rarely and only when makes sense
![Page 4: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/4.jpg)
Multiple Assignments
• Embedded assignments useful to set several variables to the same value
n1 = n2 = n3 = 0;• Assignment operator evaluated right to left
• Avoid mixed types; e.g. double d and int i
d = i = 1.5;• Assigns i value 1, thus 1 assigned to d
![Page 5: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/5.jpg)
Statement Blocks
• Sequence of statements that specify a coherent unit enclosed in curly braces
• E.g. if, for, while statement bodies
{
statement1
statement2
. . .
}
![Page 6: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/6.jpg)
Boolean Data
• Conditionals test expressions whose values are either TRUE or FALSE
• George Boole - developed algebra for T/F
• Standard C has no Boolean type
• We use the integer 0 for FALSE and the integer 1 for TRUE
![Page 7: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/7.jpg)
Relational Operators
• Compare two atomic values (not strings)
• Precedence after +/-
> Greater than < Less than
>= Greater or equal <= Less or equal
• Next level of precedence
== Equal != Not equal
![Page 8: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/8.jpg)
Logical Operators
• Operate on other Boolean values (in order of precedence)
! Not (TRUE if operand FALSE)
&& And (TRUE if both TRUE)
|| Or (TRUE if either TRUE)
![Page 9: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/9.jpg)
Example 1
• “x is not equal to either 2 or 3”
if (x != 2 || x != 3)• No, it should be
if (!(x == 2 || x == 3))• or
if (x != 2 && x != 3)
![Page 10: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/10.jpg)
Example 2
• “x is in the range from 0 to 10 exclusive”
if (0 < x < 10)• No, it should be
if (0 < x && x < 10)
![Page 11: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/11.jpg)
Short-Circuit Evaluation
• Evaluating
exp1 && exp2 or
exp1 || exp2
evaluates expressions from left to right
• If answer known before both evaluated, second expression not evaluated
(x != 0) && (y % x == 0)
![Page 12: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/12.jpg)
Flags
• Variables used for testing truth values
int done;• Assign value to indicate state of flag
done = 0;
or
done = 1;
![Page 13: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/13.jpg)
Setting Flags
• To set a flag on a condition, could useif (input == -1) {
done = 1;} else {
done = 0;}
• Better to simply use
done = (input == -1);
![Page 14: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/14.jpg)
Testing a Flag
• To test whether done has value TRUE, could use
if (done == 1)• Better to simply use
if (done)• Test for truth value 1 is redundant
![Page 15: CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);](https://reader036.fdocuments.in/reader036/viewer/2022082817/56649e455503460f94b3a474/html5/thumbnails/15.jpg)
Boolean Example
• Leap year every fourth year, except centuries, then just every fourth century– year is divisible by 4 but not by 100, or– year is divisible by 400
• Try((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)