Flow Control
Mao
● The bool Data Type ● Logical Operators ● getchar() and putchar() ● Selection ● Switch Statements ● Conditional Expressions ● Operator Precedence Chart
● Loops
○ The while Loop ○ The do-while Loop ○ The for Loop ○ Nested Loops
● Keywords break and continue
The bool data type declares a variable with the value either true or false.
The equality testing operator is two equal signs (==), not a single equal sign (=). The latter symbol is used for assignment.
A variable that holds a Boolean value is known as a Boolean variable. The bool data type is used to declare Boolean variables. For example,
bool lightsOn = true;
Internally, C uses 1 to represent true and 0 for false. If you display a bool value to the console, 1 is displayed if the value is true and 0 if it is false.
Logical Operators:
Logical operators normally take relational expressions as operands. The ! operator takes one operand. The rest take two—one to the left, one to the right.
Logical Expressions:
expression1 && expression2 is true if and only if both expressions are true. expression1 || expression2 is true if either one or both expressions are true. !expression is true if the expression is false, and vice versa.
Operator precedence and associativity determine the order in which operators are evaluated.
What is the value of the following expression?
int i = 3 + 4 * 4 > 5 * (4 + 3) - 1 && (4 - 3 > 5); printf(“%d”, i);
It’s better to use parentheses to make it more readable.
int i = (3 + 4 * 4 > 5 * (4 + 3) - 1) && (4 - 3 > 5); printf(“%d”, i);
getchar() and putchar()
The getchar() function takes no arguments, and it returns the next character from input. For example, the following statement reads the next input character and assigns its value to the
variable ch :
ch = getchar();
This statement has the same effect as the following statement:
scanf("%c", &ch);
getchar() and putchar()
The putchar() function prints its argument. For example, the next statement prints as a character the value previously assigned to ch :
putchar(ch);
This statement has the same effect as the following:
printf("%c", ch);
To test different character, #include <ctype.h> can be added into a C program.
C provides several types of selection statements: one-way if statements, two-way if-else statements, nested if statements, switch statements, and conditional expressions.
A one-way if statement executes an action if and only if the condition is true. The syntax for a one-way if statement is shown here:
if (boolean-expression) { statement(s); }
The braces can be omitted if they enclose a single statement. For example, the following statements are equivalent.
if (i > 0) {
printf("i is positive" );
}
if (i > 0)
printf("i is positive" );
The simple form of an if statement gives you the choice of executing a statement (possibly compound) or skipping it.
A two-way if-else statement specifies different actions, depending on whether the condition is true or false. Here is the syntax for a two-way if-else statement:
if (boolean-expression) {
statement(s)-for-the-true-case;
statement(s)-for-the-false-case;
} else {
}
The previous program finds that all_days is not equal to 0 , it should know that days must be 0 without retesting, and it does. With if else , you can take advantage of that knowledge by rewriting the fragment this way:
An if statement can be inside another if statement to form a nested if statement. The inner if statement can contain another if statement; in fact, there is no limit to the depth of the nesting. For example, the following is a nested if statement:
if (i > k) {
if (j > k)
printf("i and j are greater than k" );
printf("i is less than or equal to k");
} else
If there are multiple branches, else if can be used as follows:
if (condition1) {
// block of code to be executed if condition1 is true } else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true }else if (condition3) {
// block of code to be executed if the condition1 and 2 are false and condition3 is true }else {
// block of code to be executed if all the previous conditions are false
}
Personal income tax brackets and rates - 2021 tax year (BC, Canada )
| Taxable Income - 2021 Brackets | Tax Rate |
| $0 to $42,184 | 5.06% |
| $42,184.01 to $84,369 | 7.70% |
| $84,369.01 to $96,866 | 10.50% |
| $96,866.01 to $117,623 | 12.29% |
| $117,623.01 to $159,483 | 14.70% |
| $159,483.01 to $222,420 | 16.80% |
| Over $222,420 | 20.5% |
Personal Income Tax Rates - Province of British Columbia (gov.bc.ca)
A switch statement executes statements based on the value of a variable or an expression. Here is the full syntax for the switch statement:
switch (switch-expression) {
case value1:
case value2:
...
case valueN:
default:
}
statement(s)1; break; statement(s)2; break;
statement(s)N; break; statement(s)-for-default;
The switch statement observes the following rules:
● The switch-expression must yield an integral value and always be enclosed in
parentheses.
● The value1, . . . , and valueN are integral constant expressions, meaning that they cannot contain variables, such as 1 + x. These values are integers and cannot be floating-point values. ● When the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached. ● The default case, which is optional, can be used to perform actions when none of the
specified cases matches the switch-expression.
● The keyword break is optional. The break statement immediately ends the switch
statement.
A conditional expression evaluates an expression based on a condition. Conditional expressions have a completely different structure and do not include an explicit if. The syntax is shown below:
boolean-expression ? expression1 : expression2;
For example:
==
A loop can be used to tell a program to execute statements repeatedly.
Write a program to display the following message on the console 10 times .
"Welcome to programming!"
A while loop executes statements repeatedly while the condition is true.
The syntax for the while loop is
while (loop-continuation-condition) {
// Loop body Statement(s);
}
The while loop repeatedly executes the statements in the loop body when the loop-continuation-condition evaluates to true.
Part of the code is as follows:
int count = 0; while (count < 10) // why < not <= {
printf ( "Welcome to programming"); count++;
}
This type of loop is known as a counter-controlled loop.
What happens if the loop is written as follows?
vs
A do-while loop is the same as a while loop except that it executes the loop body first and then checks the loop continuation condition. Its syntax is as follows:
do
{
// Loop body; Statement(s);
} while (loop-continuation-condition);
The do-while loop executes the loop body first, then checks the loop-continuation-condition to determine whether to continue or terminate the loop.
A for loop has a concise syntax for writing loops. In general, the syntax of a for loop is as shown below:
for (initial-action; loop-continuation-condition; action-after-each-iteration) {
// Loop body; Statement(s);
}
A for loop performs an initial action once, then repeatedly executes the statements in the loop body, and performs an action after an iteration when the loop-continuation-condition evaluates to true.
for loop
Algorithms and Data Structures - Mao © 2022 There is no failure just changeable code.
while loop
The loop control variable can be declared and initialized in the for loop. Here is an example:
for (int i = 0; i < 10; i++) {
printf ( "Welcome to programming");
}
If there is only one statement in the loop body, as in this example, the braces can be omitted.
Do the following two loops result in the same value in sum?
Which Loop to Use?
In general,
● a for loop may be used if the number of repetitions is known in
advance;
● a while loop may be used if the number of repetitions is not fixed; ● a do-while loop can be used to replace a while loop if the loop body has to be executed before the continuation condition is tested.
Which Loop to Use?
Nested loops consist of an outer loop and one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, and started anew.
Be aware that a nested loop may take a long time to run. Consider the following loop nested in three levels:
for (int i = 0; i < 10000; i++)
for (int j = 0; j < 10000; j++)
for (int k = 0; k < 10000; k++)
statement(s); // {} will be needed for statements.
Keywords break and continue
The break and continue keywords provide additional controls in a loop. The continue keyword breaks out of an iteration, while the break keyword breaks out of a loop.
● Use break in a loop to immediately
terminate the loop.
● use continue in a loop to end the current iteration. Program control goes to the end of the loop body.
***
***
***
# ## ###
*
*
*
● Stephen Prata. C Primer Plus (6th Edition). Addison-Wesley
Professional, 2013.
Algorithms and Data Structures - Mao © 2022 There is no failure just changeable code.