Conditional

advertisement
Sequential Programming Limitation
Sequential Programming
„
Conditional Statements in C
Computer executes instructions in the order in which
they are stated
Limitations of Sequential Programming
„
„
Cannot choose whether or not to perform a
command/instruction
Cannot perform the same command more than once
Based on slides © McGraw-Hill
Additional material © 2004/2005 Lewis/Martin
Modified by Diana Palsetia
CIT 593
1
CIT 593
Control Structures
“if” statement
Conditional
„
„
„
if (condition){
statement(s)
}
Making decision about which code to execute, based
on evaluated expression
if
if-else
If the condition is true (expression is evaluated to nonzero), then the statement(s) (i.e. instructions) will be
executed. Otherwise, it/they won’t.
Iteration
„
„
„
„
CIT 593
2
Executing code multiple times, ending based on
evaluated expression
while
for
do-while
//Assume x is an integer
if(x > 10) {
x = x * 2;
}
3
CIT 593
4
1
“if” statement (contd..)
Example if Statements
if (x <= 10)
y = x * x + 5;
{ } indicates the block of code that will get executed given
the condition is true
You can avoid the curly brace after condition if only one statement is to
be performed
//Assume x is an integer
if(x < 0)
x = x * -1;
CIT 593
if (x <= 10)
y = x * x + 5;
z = (2 * y) / 3;
only first statement is conditional;
second statement is
always executed
if (x <= 10) {
y = x * x + 5;
z = (2 * y) / 3;
}
5
compound statement;
both executed if x <= 10
CIT 593
6
“if-else” statement
Assignment vs. Equality
if (condition){
statement(s)
}
else{
statement(s)
}
Don't confuse equality (==) with assignment (=)
int x = 9;
i t y = 10
int
10;
if (x == y) {
printf(“not executed\n”);
}
if (x = y) {
printf(“x = %d y = %d”, x, y);
}
Result: “x = 10 y = 10” is printed. Why?
Compiler will not stop you! (What happens in Java?)
CIT 593
Style: avoid singleton
if statements
(Often cause of errors)
7
//Assume x is an integer
if(x > 0) {
x = x * 2;
}
else {
x = x * -1;
}
CIT 593
8
2
If-else Flow chart
Style Rule: Indentation and Spacing
Style
true
condition?
Statement(s)
„
Recommended indentation is from 2 to 4 spaces, but must be
consistent throughout the program
„
Single
g space
p
around every
y binary
y operator,
p
, including
g comparisons
p
and
assignment (=)
false
Statement(s)
Indentation
CIT 593
9
< 10) {
= x + 1;
{
= x - 1;
Spacing
CIT 593
10
Cascading/Chaining If’s and Else’s Example 2
Cascading “if-else” Example
if(month == 4 || month == 6 || month == 9 || month
== 11) {
printf(“Month has 30 days.\n”);
}
Example
//Assume variable score is entered by user
if (score > 90)
printf(“Grade A\n”);
else if (score > 80)
printf(“Grade B\n”);
else if (score > 65)
printf(“Grade C\n”);
.
.
else
printf(“F\n”);
else if (month == 1 || month == 3 ||
month == 5 || month == 7 ||
month == 8 || month == 10 ||
month == 12) {
printf(“Month has 31 days.\n”);
}
else if (month == 2) {
printf(“Month
printf(
Month has 28 or 29 days
days.\n
\n”);
);
}
//Note: You can avoid the curly brace after condition if
only one statement is to be performed
CIT 593
if (x
___x
}
else
l
___x
}
11
else {
printf(“Don’t know that month.\n”);
}
CIT 593
12
3
Difference between Chaining if-else vs. just using if
int x = -5;
if(x < 0){
x = -x;
}
else if(x >= 1 && x <= 10){
x = x * 2;
}
else if(x > 10){
x = x * 2 - 1;;
}
Nested if-statements
int x = -5;
if(x < 0){
x = -x;
}
if(x >= 1 && x <= 10){
x = x * 2;
}
if(x > 10){
x = x * 2 - 1;;
}
An if within an if
if (condition1){
if (condition2){
( ) A
statement(s)
}
else{
statement(s) B
}
}
else {
statements(s) C
}
Truth Table
What values must the
conditions have in order for
block A to run? B? C?
A
condition1
B
C
T
condition2
If a series of standard "if" statements were used, it would be possible for all
of the statements to be executed, which is not what was intended.
CIT 593
13
CIT 593
Matching Else with If
Switch
Else is always associated with closest unassociated if
if (x != 10)
if (y > 3)
z = z / 2;
else
z = z * 2;
is the same as...
if (x != 10) {
if (y > 3){
z = z / 2;
}
else{
z = z * 2;
}
}
is NOT the same as...
if (x != 10) {
if (y > 3)
z = z / 2;
}
Solution: always use braces
else
(avoids
the problem entirely)
z = z * 2;
CIT 593
14
15
switch (integer_expression){
case const1:
statement(s);
break;
case const2:
statement(s);
break;
default:
statement(s);
break;
}
evaluate
expression
= const1?
t1?
T
action1
ti 1
F
= const2?
T
action2
F
Alternative to long if-else chain.
If break is not used, then
case "falls through" to the next.
CIT 593
action3
16
4
Switch Example
/* same as month example from cascaded if-else */
switch (month) {
case 4: case 6: case 9:case 11:
printf(“Month has 30 days.\n”);
p
y
break;
case 1:case 3:
/* some cases omitted for brevity...*/
printf(“Month has 31 days.\n”);
break;
case 2:
printf(“Month
printf(
Month has 28 or 29 days
days.\n
\n”);
);
break;
default:
printf(“Don’t know that month.\n”);
}
CIT 593
17
5
Download