Electronics · Ch 12 — C Programming
Selective Control Statements
Selective Control Statements
By default a C program runs its statements one after another, from top to bottom. This straight-line flow is called sequential execution. Many problems, however, need the program to choose between alternatives, skip some statements, or repeat a block. The statements that let a program depart from the plain top-to-bottom order are called control statements (also called control structures); they define the order in which the statements of a program are executed.
C's control statements fall into two broad groups:
- Conditional (selective) control statements — they carry out a logical test whose result is either true or false, and then transfer control to different statements depending on that result. Because the program makes a decision and branches accordingly, this is described as decision making and branching.
- Iterative (looping) statements — they repeat a block of statements a number of times (covered in the next section).
This section covers the four conditional control statements on the Karnataka 2nd PUC Electronics syllabus: the if, if-else, nested-if and switch statements. In every case the condition is a logical expression that yields true or false, and the statement it controls may be a single (simple) statement or a compound statement — a group of two or more statements enclosed in braces.
[!NOTE]
Several of the program listings printed in this KTBS textbook carry typographical slips (missing or stray semicolons, a mismatched quote) and a few logic slips; many of these are fixed in the board's official corrigendum. The code shown below is written in corrected, compilable form, and wherever a correction changes what the program actually does, it is pointed out.
if-statement
The if-statement executes a statement or a block of statements only when a condition is satisfied. Since control takes just one of two paths — perform the block, or skip it — it is called one-way branching.
if (condition)
{
statement;
}
The condition is a logical expression that results in true or false. If the condition is true the statement runs; if it is false, control passes straight to the next statement in the program. The statement may be simple (one statement) or compound (several statements in braces).
For example, to print a number only when it is even, the test should ask whether the remainder after dividing by 2 is zero:
if ((numb % 2) == 0)
printf("%d is an even number\n", numb);
[!NOTE]
The textbook's own version of this example writes the test as (numb % 2) != 0 while still printing "even number". That condition is actually true for odd numbers, so the printed logic is inverted. The board's corrigendum corrects the test to check for a zero remainder (an even number), as shown above.
if-else statement
When one of two alternative actions is to be carried out, the if-else statement is used — two-way branching.
if (condition)
{
statement1;
}
else
{
statement2;
}
If the logical test is true, statement1 executes; otherwise statement2 executes. After whichever branch runs, control continues with the statement that follows the if-else. A typical use is to check whether two numbers are equal, or to accept a character and respond differently for one particular key.
[!NOTE]
In the textbook's "check whether two numbers are equal" example the true branch mistakenly repeats the input prompt, printf("Enter the values of M, N\n");, instead of announcing the result — yet its sample run correctly shows "M and N are equal". The listing and its output disagree. The corrigendum corrects the true branch to printf("M and N are equal\n");.
Nested-if statement
When there are more than two alternatives to select from, one if-statement is placed inside another, most often as an else-if ladder. A nested-if is simply an if statement enclosed within another if statement.
if (condition1)
{
if (condition2)
statement1;
else
statement2;
}
else if (condition3)
statement3;
else
statement4;
Reading the ladder: statement1 runs when both condition1 and condition2 are true; statement2 runs when condition1 is true but condition2 is false; if condition1 is false, control drops to condition3, running statement3 when it is true and statement4 otherwise. Nested-if is how a program picks the largest of three integers, or sorts a measured value (such as a person's height) into one of several ranges.
switch statement
The switch statement selects one task from among several, based on the value of an integer or character expression — multiple-way branching. It is usually clearer than a long else-if ladder when the choice depends on a single variable.
switch (expression)
{
case label1:
block1;
break;
case label2:
block2;
break;
/* ... more cases ... */
default:
default_block;
}
``` …
Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.
Redraw of the if-statement flowchart printed on textbook page 502. Flow enters a diamond decision box (the condition). The 'True' exit leads down to a rectangle labelled 'Statement' and then to 'Next statement'; the 'False' exit bypasses the statement box and rejoins directly at 'Next statement'. It …
Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.
Redraw of the if-else flowchart printed on textbook page 504. Flow enters a central decision diamond (the condition). The 'true' exit goes to a rectangle 'statement1'; the 'false' exit goes to a rectangle 'statement2'. Both boxes converge downward into a single following statement, illustr …
Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.
Redraw of the nested-if flowchart printed on textbook page 506. The top decision (condition1) has a True branch leading to a second decision (condition2) and a False branch leading to a third decision (condition3); each inner decision routes to its own action block, and all branches converge to a single exit. Depicts th …
Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.
Redraw of the switch flowchart printed on textbook page 509. A decision on the expression value fans out through a horizontal distribution bar to a row of parallel blocks (block1, block2, block3, ..., block n) and a 'default' block, each reached according to the matching case label. …
| Letter Grade | Remarks |
|---|---|
| A | BEST |
| B | VERY GOOD |
| C | FAIR |
| D | ABSENT |
| E | FAIL |
(Data reproduced verbatim from the textbook's grade table on page 511. Note the program's switch code separately keys case 'F' rather than 'E' — an inconsistency in the printed lis …