1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands...
-
Upload
lynette-richardson -
Category
Documents
-
view
222 -
download
1
Transcript of 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands...
![Page 1: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/1.jpg)
1
if and if-else statements
![Page 2: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/2.jpg)
2
Relational Operators
x < y
< is an operator x and y are its operands
( x < y ) is called a logical expression. Logical as in “pertaining to logic” Yields a value of either true or false
1 for true 0 for false
![Page 3: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/3.jpg)
3
Relational Operators
The complete set of relational operators:
x < y less than x <= y less than or equal x > y greater than x >= y greater than or equal x == y equal x != y not equal
![Page 4: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/4.jpg)
4
Relational Operators
Relational operators are often used in an "if" statement
#include <stdio.h>
int main()
{
double t; /* elapsed time */
printf ("Please enter elapsed time in seconds: ");
scanf("%lf", &t);
/* Check for valid data */
if (t <= 0)
{
printf ("Please enter a time value greater than 0\n");
return 1;
}
printf ("t = %f\n", t);
return 0;
}
This block is executed only if the condition following “if” is true.
![Page 5: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/5.jpg)
5
The Simple if Statement
Things to notice
if (t < 0)
{
printf ("Please enter a time value greater than 0\n");
exit (1);
}
The condition MUST be enclosed in parentheses.
Curly brackets delimit the block to be executed if the condition is true.
No semicolon following the if statement.
No semicolon following final curly bracket.
![Page 6: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/6.jpg)
6
Some matters of programming style
Zyante puts the opening curly bracket on the same line as the if statement.
if (t < 0) {
printf ("Please enter a time value greater than 0\n");
exit (1);
}
This is widely accepted style for “if” statements. I prefer to align the curly brackets as shown in
the previous example. Easier to read. Consistent with style used for functions. e.g., main()
You will undoubtedly see both styles in my slide
![Page 7: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/7.jpg)
7
Some matters of programming style
If there is only one statement to be conditionally executed, the curly brackets may be omitted.
if (t < 0) printf ("Please enter a value greater than 0\n");
Or even
if (t < 0) printf ("Please enter a value greater than 0\n");
Don’t do either of these(even if I occasionally do it myself!)
Easy for a reader to be confused. Easy to introduce errors later when you need to
add more statements to the conditional part.
![Page 8: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/8.jpg)
8
Some matters of programming style
Take it as an ironclad rule:
Every “if” must be followed by a block delimited by curly brackets
even if there is only one statement.
if (condition)
{
/* Stuff to do if condition is true */
}
The code inside the curly brackets is indented three spaces beyond the brackets.
![Page 9: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/9.jpg)
9
Some matters of programming style
Be aware: Programming style is important!
Points will be deducted on projects and exams for violations even if the program is functionally correct.
You will probably have to adapt to different coding standards in other courses and throughout your career.
End of Section
![Page 10: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/10.jpg)
10
Logical Values
In C every number has a logical value, in addition to its numerical value. 0 represents false. Any nonzero value is considered true.
scanf("%lg", &t); if (t) { printf ("t is true\n"); return 1; }
This is legal C, and a widely used idiom. But not good programming.
It is better to use a logical expression with “if”.
Example: if (t != 0)
![Page 11: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/11.jpg)
11
Logical Values
In C89, numerical values are the only way to represent truth value.
C99 added the _Bool type for this purpose. Discussed in the textbook.
A _Bool variable is an integer type, but can only take on the values 0 and 1.
Only mentioned for your information. We will NOT use C99 in this class
![Page 12: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/12.jpg)
12
Logical Values in C99
C99 also provides a standard header file <stdbool.h>
Defines with #define bool as _Bool true as 1 false as 0
![Page 13: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/13.jpg)
13
Recommendation
Stick to the old way.
Your program will compile correctly on systems using older compilers.
_Bool is ugly Not a real boolean type, as in Java and
C# Still works as an int
![Page 14: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/14.jpg)
14
Using Logical Expressions
The most common use of logical expressions is in conditional statements like “if”.
if (x < y){
/* Do something */}
![Page 15: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/15.jpg)
15
Relational Operators
A frequent mistake : confusing = and ==
if (x = 1){
/* Do something */}
This compiles without error, and gives no error indication at run time.
But it’s not what you meant!
The conditional block will always be executed!
This sets x to 1 and yields a value of 1, which means “true” to the if.
![Page 16: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/16.jpg)
16
Relational Operators
Should have said:
if (x == 1){
/* Do something */}
![Page 17: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/17.jpg)
Example-bash-3.00$ cat test.c
#include <stdio.h>
int main()
{
int a = 1;
int b = 2;
printf ("a is %d and b is %d\n", a, b);
if (a = b)
{
printf ("a and b are equal\n");
}
else
{
printf ("a and b are unequal\n");
}
return 0;
}
-bash-3.00$ gcc -Wall *.c
test.c: In function ‘main’:
test.c:9: warning: suggest parentheses around assignment used as truth va
-bash-3.00$./a.out
a is 1 and b is 2
a and b are equal
-bash-3.00$
![Page 18: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/18.jpg)
Example-bash-3.00$ cat test.c
#include <stdio.h>
int main()
{
int a = 1;
int b = 0;
printf ("a is %d and b is %d\n", a, b);
if (a = b)
{
printf ("a and b are equal\n");
}
else
{
printf ("a and b are unequal\n");
}
return 0;
}
-bash-3.00$ gcc -Wall *.c
test.c: In function ‘main’:
test.c:9: warning: suggest parentheses around assignment used as truth va
-bash-3.00$./a.out
a is 1 and b is 0
a and b are unequal
-bash-3.00$
![Page 19: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/19.jpg)
19
Using Logical Expressions
But the expression (x < y) has a value. 0 or 1
The value can be stored in an integer variable.
int x_ok;...x_ok = (x < y);
![Page 20: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/20.jpg)
20
Using Logical Expressions
x_ok = (x < y);
is equivalent to
if (x < y){ x_ok = 1;
}else{ x_ok = 0;
}
![Page 21: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/21.jpg)
21
Logical Operators
You can then say:
if (x_ok){
/* Do something */
}
![Page 22: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/22.jpg)
22
Logical Values
Remember:
== compares numerical values
not logical values!
End of Section
![Page 23: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/23.jpg)
23
Logical Operators
Operators that “do logic”
&& AND || OR ! NOT
Permit us to combine logical expressions like (a < b) and (b < c) into a single bigger expression.
![Page 24: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/24.jpg)
24
Logical Operator &&
Example:
if ((x < y) && (y < z)){/* Do something. */
}
The conditional block will be executed if x is less than y AND y is less than z.
Otherwise it will be skipped.
![Page 25: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/25.jpg)
25
Example
Determine if x, y, and z are in increasing order:
if ((x < y) && (y < z))
![Page 26: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/26.jpg)
Example#include <stdio.h>
int main()
{
int x,y,z;
printf ("Please enter integers x, y, and z in increasing order\n");
printf ("x: ");
scanf("%d", &x);
printf ("y: ");
scanf("%d", &y);
printf ("z: ");
scanf("%d", &z);
if ((x < y) && (y < z))
{
printf ("x, y, and z are in increasing order\n");
}
else
{
printf ("x, y, and z are not in increasing order\n");
}
return 0;
}
![Page 27: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/27.jpg)
27
Logical Operator ||
Example:
if ((x < y) || (y < z)){/* Do something. */
}
The block will be executed if x is less than y OR y is less than z(including the case where both are
true.) If neither condition is true, it will be
skipped.
![Page 28: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/28.jpg)
28
Logical Operator !
Example:
if (!x_ok){/* Do something. */
}
The conditional block will be executed if x_ok is false.
If x_ok is true it will be skipped.
![Page 29: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/29.jpg)
29
Logical Operators
! is a unary operator.
Like all unary operators it has very high precedence. Unary operators stick to the thing
beside them. All apply to the thing to their right
except x++ and x-- You don’t need parentheses to make
the meaning clear.
![Page 30: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/30.jpg)
30
Logical Operators
&& and || are binary operators. Have relatively low precedence
All arithmetic and relational operators are applied first. && and || are applied to the results.
a < b && b < c means (a < b) && (b < c)
Use parentheses for readability!
![Page 31: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/31.jpg)
31
Logical Operators
&& has higher precedence than ||
a < c || b < c && c < dmeans
(a < c) || ((b < c) && (c < d))
This would not be obvious to most people.Use parentheses to make the meaning clear.
End of Presentation
![Page 32: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/32.jpg)
32
The if ... else Statement
You can write an alternative block of code to be executed if the condition is not true.
if (condition){
/* Stuff to do if condition is true */}else{
/* Stuff to do if condition is NOT true */}
Use this alignment style rather than the one shown in the textbook.
Align the opening and closing curly brackets.
Indent everything inside the brackets.
![Page 33: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/33.jpg)
33
A Series of if ... else Statements
We often have several conditions Need to execute one of several
corresponding blocks of code according to which condition is true.
![Page 34: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/34.jpg)
34
A Series of if ... else Statements
if (condition_1){
/* Stuff to do if condition_1 is true */}else if (condition_2){
/* Stuff to do if condition_1 is false and condition_2 is true. */}else{
/* Stuff to do neither condition_1 nor condition_2 is true. */}
Exactly one of these code blocks will be executed.
![Page 35: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/35.jpg)
35
A Series of if ... else Statements
if (condition_1){
/* Stuff to do if condition_1 is true */}else if (condition_2){
/* Stuff to do if condition_1 is false and condition_2 is true. */}else{
/* Stuff to do neither condition_1 nor condition_2 is true. */}
If condition_1 is true, condition_2 has no effect.Only the first block is executed.
![Page 36: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/36.jpg)
36
A Series of if ... else Statements
Note that (in general) this is not the same as a series of independent if statements.
Different results if more than one of the conditions can be true at the same time.
![Page 37: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/37.jpg)
37
Nested “if” Statements
We can put any legal C statements inside the code block following an “if” statement Including other “if” statements.
The nesting can continue indefinitely But should not go beyond three levels. The C compiler can handle deep nesting
but humans cannot.
![Page 38: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/38.jpg)
38
Example: Which Quadrant?
The x-y plane can be divided into four quadrants.
http://en.wikipedia.org/wiki/Cartesian_coordinate_system#Two-dimensional_coordinate_system
![Page 39: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/39.jpg)
39
Example: Which Quadrant?
Given a point, (x,y), determine its quadrant. Include zeroes with the positive values.
![Page 40: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/40.jpg)
40
quadrant.c
#include <stdio.h>
int main()
{
double x = 0.0;
double y = 0.0;
int quadrant = 0;
printf ("X: ");
scanf ("%lg", &x);
printf ("Y: ");
scanf ("%lg", &y);
![Page 41: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/41.jpg)
Nested “if” Statements
if (x >= 0.0)
{
if (y >= 0.0)
{
quadrant = 1;
}
else
{
quadrant = 4;
}
}
else
{
if (y >= 0)
{
quadrant = 2;
}
else
{
quadrant = 3;
}
}
printf ("Point (%lg,%lg) is in quadrant %d\n", x, y, quadrant);
return 0;
}
![Page 42: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/42.jpg)
42
Program quadrant.c Running
![Page 43: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/43.jpg)
43
The "Dangling Else" Problem
Nested “if” statements can lead to confusion about the “else”
if (x > 0)
if (y > 0)
sum = x + y;
else
printf ("Invalid input value\n");
Which "if" does the "else" go with?
Answer: The inner one
Avoid confusion by always putting curly brackets after the "if".
![Page 44: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/44.jpg)
44
The "Dangling Else" Problem
if (x > 0)
{
if (y > 0)
{
sum = x + y;
}
else
{
printf ("Invalid input value\n");
}
}
If this is what you mean.
![Page 45: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/45.jpg)
45
The "Dangling Else" Problem
Orif (x > 0)
{
if (y > 0)
{
sum = x + y;
}
}
else
{
printf ("Invalid input value\n");
}
If this is what you mean.
![Page 46: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/46.jpg)
46
The "Dangling Else" Problem
If you want the error message if either condition is false, write
if ((x > 0) && (y > 0))
{
sum = x + y;
}
else
{
printf ("Invalid input value\n");
}
![Page 47: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/47.jpg)
47
Lazy Evaluation
if ((x > 0) && (y > 0))
{
/* Do something. */
}
If x is not greater than 0, we don’t need to look at y.
We already know that ((x > 0) && (y > 0)) is false
regardless of the value of y.
![Page 48: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/48.jpg)
48
Lazy Evaluation
The C compiler takes advantage of this. Skip the evalutation of the second
expression if the first one is false.
Saves some CPU time. A convenient way to avoid dividing by 0
if ( (x != 0) && (b/x < 0.1) )
{
/* Do something. */
}
![Page 49: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/49.jpg)
49
Lazy Evaluation
Consider the case:
if ((x < 0) && (y++ < 0)){
/* Do something. */
}
Will y be incremented?Only if x is less than 0!
An example of how side effects can cause trouble.
![Page 50: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/50.jpg)
50
Lazy Evaluation
In the case of ||, the second expression will not be evaluated if the first one is true.
Summary – Lazy Evaluation
Work left to right, and only evaluate what you have to evaluate in order to determine the overall result.
End of Section
![Page 51: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/51.jpg)
51
The Conditional Operator
An expression that will have one of two possible values depending on a condition.
C's only ternary (three operand) operator
Example: (i >= j) ? i : j
Condition
Value if condition is true
Value if condition is false
![Page 52: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/52.jpg)
52
The Conditional Operator
x = (i >= j) ? i : j;
is equivalent toif (i >= j)
{
x = i;
}
else
{
x = j;
}
First form: concise but less understandable.End of Section
![Page 53: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/53.jpg)
53
The switch Statement
Gauge Rho
12 5.211
14 8.285
16 13.170
18 20.950
An optimized form when you need to check for one of several values.
Merge multiple if ... else ... statements into a single compound statement.
![Page 54: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/54.jpg)
54
The switch Statement
A switch statment can replace a series of “if” ... “else”... statements
switch (gauge){
case 12:rho = 5.211; break;
case 14: rho = 8.285; break;case 16: rho = 13.17; break;case 18:
rho = 20.95; break;default: printf (“Invalid gauge\n”);
}
![Page 55: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/55.jpg)
55
The switch Statement
The compiled code compares gauge to each “case” in turn.
When an equal value is found, execute the corresponding code.
If no equal value if found, execute the default code. If no default, just continue below the block.
![Page 56: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/56.jpg)
56
The switch Statement
Example: Suppose gauge is 16.
switch (gauge){
case 12:rho = 5.211; break;
case 14: rho = 8.285; break;case 16: rho = 13.17; break;case 18:
rho = 20.95; break;default: printf (“Invalid gauge\n”);
}
/* Continue here. */
![Page 57: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/57.jpg)
#include <stdio.h>
int main()
{
int gauge;
double rho;
printf ("Enter wire gauge (12, 14, 16, or 18): ");
scanf("%d", &gauge);
switch (gauge)
{
case 12: rho = 5.211; break;
case 14: rho = 8.285; break;
case 16: rho = 13.17; break;
case 18:
rho = 20.95; break;
default:
printf ("Invalid gauge %d\n", gauge);
return 1;
}
printf ("rho is %7.3f\n", rho);
return 0;
}
![Page 58: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/58.jpg)
58
Program wire2.c Running
![Page 59: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/59.jpg)
59
A common mistake:
switch (gauge){case 12:
rho = 5.211;case 14: rho = 8.285;case 16: rho = 13.17;case 18:
rho = 20.95;default: printf (“Invalid gauge\n”); return 1;
}
No break
![Page 60: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/60.jpg)
60
The switch Statement
Fall Through
What happens if there is no break at the end of the code for a case?
Answer: the code in the following case statement will be executed
This will continue until we reach a break statement or until we reach the end of the switch statement.
![Page 61: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/61.jpg)
61
The switch Statement
Fall through:switch (gauge){
case 12: rho = 5.211;case 14: rho = 8.285;case 16: rho = 13.17;case 18: rho = 20.95; break;default: printf (“Invalid gauge\n”);
return 1;}
If gauge has value 12, 14, 16 or 18, rho will end up with value 20.95
No break
![Page 62: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/62.jpg)
62
The switch Statement
If the switch statement is inside a function, you can use “return” instead of “break”.
switch (gauge){case 12: rho = 5.211; return rho;case 14: rho = 8.285; return rho;case 16: rho = 13.17; return rho;case 18: rho = 20.95; return rho;default: printf (“Invalid gauge\n”);
abort();}/* Never reach this point. */
![Page 63: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/63.jpg)
63
The switch Statement
Programming Style Issues
A switch statement should look like a table.
The entire block should should be visible on your screen at the same time.
Keep it simple.
You can include multiple statements for a case.But only a few.Use a function if you need to do more.
![Page 64: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/64.jpg)
The switch Statement
switch (gauge){
case 12:rho = 5.211;phi = 3.457;break;
case 14: rho = 8.285;
phi = 4.657;break;
case 16: rho = 13.17;
phi = 8.381;break;
case 18: rho = 20.95;
phi = 12.45;break;
default:printf (“Invalid gauge\n”); abort();
}
![Page 65: 1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.](https://reader031.fdocuments.in/reader031/viewer/2022013004/56649eb35503460f94bbab28/html5/thumbnails/65.jpg)
Good use of follow-through
int score;printf(“Enter an integer score between 0 and 10”);scanf(“%d”,&score);
printf(“Your letter grade is “);switch(score) {
case 10:case 9: printf(“A”);case 8:
printf(“B”);case 7: printf(“C”);case 6:
printf(“D”);default:
print(“F”);}printf(“\n”);
Challenge: rewrite this code so that the scores are from 0 to 100 and letter grades are 90-100: A; 80-89: B; 70-79: C60-69: D; 0-59: F
Your code must use the same number of cases as the code shown here.