Professional Documents
Culture Documents
CP 04 Control Flow
CP 04 Control Flow
CP 04 Control Flow
Swaroop Joshi
2023
Control flow
Control flow
fl
Control flow
fl
Is rst >
second?
Yes No
✤ Given two integers, return the int max(int first, int second) {
greater of the two.
Is rst >
second?
Yes No
}
max = rst max = second
fi
fi
✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
Is rst >
second?
Yes No
return max;
}
max = rst max = second
fi
fi
✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
if (first > second) {
Is rst >
}
second?
Yes No
return max;
}
max = rst max = second
fi
fi
✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
if (first > second) {
Is rst >
}
second?
Yes No
return max;
}
max = rst max = second
fi
fi
✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
if (first > second) {
Is rst > max = first;
}
second?
Yes No
return max;
}
max = rst max = second
fi
fi
✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
if (first > second) {
Is rst > max = first;
} else {
second?
max = second;
Yes No }
return max;
}
max = rst max = second
fi
fi
✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
if (first > second) {
Is rst > max = first;
} else {
second?
max = second;
Yes No }
return max;
}
max = rst max = second
fi
fi
✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
if (first > second) {
Is rst > max = first;
} else {
second?
max = second;
Yes No }
return max;
}
max = rst max = second
fi
fi
prev_stmt prev_stmt
if (test) {
test
if_block
T F
} else {
if_block else_block
else_block
}
next_stmt
next_stmt
if (test) {
test
if_block
T F
} else {
if_block else_block
else_block
}
next_stmt
next_stmt
if (test) {
Zero or more
test statements
if_block
T F
} else {
if_block else_block
else_block
}
next_stmt
next_stmt
if (test) {
Zero or more
test statements
if_block
T F
} else {
Zero or more
if_block else_block
else_block statements
}
next_stmt
next_stmt
if (test) {
Zero or more
test statements
if_block
T F
} else {
Zero or more
if_block else_block
else_block statements
Program State
Tracing an if-else statement Trace only the portions of the
statement that get executed
Program State
rst = 1, second = 0
fi
Tracing an if-else statement Trace only the portions of the
statement that get executed
Program State
rst = 1, second = 0
int max = 0;
fi
Tracing an if-else statement Trace only the portions of the
statement that get executed
Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
fi
fi
Tracing an if-else statement Trace only the portions of the
statement that get executed
Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
fi
fi
Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
fi
fi
fi
Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
} else {
max = second;
fi
fi
fi
Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
} else {
max = second;
fi
fi
fi
Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
} else {
max = second;
}
fi
fi
fi
Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
} else {
max = second;
}
rst = 1, second = 0, max = 1
fi
fi
fi
fi
Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
} else {
max = second;
Else-block skipped for these values
}
rst = 1, second = 0, max = 1
fi
fi
fi
fi
Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
} else {
max = second;
Else-block skipped for these values
} Must show values here
after the entire if-else rst = 1, second = 0, max = 1
fi
fi
fi
fi
} else {
max = second;
fi
fi
} else {
max = second;
rst = -1, second = -1, max = 0 -1
fi
fi
fi
} else {
max = second;
rst = -1, second = -1, max = 0 -1
}
fi
fi
fi
} else {
max = second;
rst = -1, second = -1, max = 0 -1
}
rst = -1, second = -1, max = -1
fi
fi
fi
fi
} else {
max = second;
rst = -1, second = -1, max = 0 -1
}
rst = -1, second = -1, max = -1
fi
fi
fi
fi
} else {
max = second;
rst = -1, second = -1, max = 0 -1
} Must show values here
after the entire if-else rst = -1, second = -1, max = -1
fi
fi
fi
fi
✤ marks > 90
Boolean expr: evaluate to TRUE or FALSE
✤ marks > 90
✤ grade == ‘E’
Boolean expr: evaluate to TRUE or FALSE
✤ marks > 90
✤ grade == ‘E’
✤ 4
Boolean expr: evaluate to TRUE or FALSE
✤ marks > 90
✤ grade == ‘E’
In C, any non-zero
✤ 4 value is TRUE
Boolean expr: evaluate to TRUE or FALSE
✤ marks > 90
✤ grade == ‘E’
In C, any non-zero
✤ 4 value is TRUE
Boolean expr: evaluate to TRUE or FALSE
✤ grade == ‘E’
In C, any non-zero
✤ 4 value is TRUE
Boolean expr: evaluate to TRUE or FALSE
✤ marks != 75
Evaluate each of the following expressions
Evaluate each of the following expressions
✤ x <= 0 || x >= 10
Evaluate each of the following expressions
✤ x <= 0 || x >= 10
✤ (x – 1) == ((y / 5) + (y % 5))
Evaluate each of the following expressions
✤ x <= 0 || x >= 10
✤ (x – 1) == ((y / 5) + (y % 5))
✤ (x != y) || !(x == y)
Write a boolean expression for each of these
✤ Given two integer variables: n and m
✤ n is equal to 3 or 5
✤ n is below 15 or above 42
✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Write a boolean expression for each of these
✤ Given two integer variables: n and m
✤ n is below 15 or above 42
✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Write a boolean expression for each of these
✤ Given two integer variables: n and m
✤ n is below 15 or above 42
✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Write a boolean expression for each of these
✤ Given two integer variables: n and m
✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Write a boolean expression for each of these
✤ Given two integer variables: n and m
✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Write a boolean expression for each of these
✤ Given two integer variables: n and m
✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Write a boolean expression for each of these
✤ Given two integer variables: n and m
prev_stmt
prev_stmt
test
if (test) {
F
T if_block
if_block
}
next_stmt
next_stmt
int max(int first, int second) { Does this code work as expected?
int max = first; Trace it with the test examples!
if (second > max) {
max = second;
}
return max;
}
prev_stmt
prev_stmt
if (test_1) {
test_1
T
F if_block_1
} else if (test_2) {
test_2 if_block_2
if_block_1 } else {
T F
else_block
if_block_2 else_block }
next_stmt
next_stmt
prev_stmt
prev_stmt
if (test_1) {
test_1
T
F if_block_1
} else if (test_2) {
test_2 if_block_2
if_block_1 } else {
T F
else_block
if_block_2 else_block }
next_stmt
Any number of if’s can be
next_stmt chained like this
grade='O' marks>80
grade='E' marks>70
grade='A'
grade='P'
return grade
char grade
char grade