Reputation: 57
In the book "The C programming language" its written that the last else statement in else-if construction can be used for error checking to catch an impossible condition.
Can anyone provide a simple example for this?
Upvotes: 4
Views: 651
Reputation:
As an exercise, suppose we expect to read an integer using digits entered as characters. This will be done using the getchar
function. There are three cases to consider:
The end of the file is reached or a read error occurs, so we must quit reading digits. This means getchar
will return EOF
.
The character we read isn't a digit. Note that this may be a newline \n
. There is no need to check for the newline separately since it isn't a digit, except for output purposes perhaps.
Adding another digit may cause an overflow. For example, if you have 214748364 stored in an int that can only hold a number up to 2147483647, and the next digit would make it 2147483648, this is an overflow.
Here is the code:
#include <stdio.h>
#include <ctype.h>
#include <limits.h>
int main(void)
{
int n = 0;
int c;
while ((c = getchar()) != EOF && isdigit(c) &&
(INT_MAX - (c - '0')) / 10 >= n) {
n *= 10;
n += c - '0';
}
if (c == EOF)
printf("End of file or an error was encountered");
else if (c == '\n')
printf("Newline encountered");
else if (!isdigit(c))
printf("Non-digit character %c encountered", c);
else if ((INT_MAX - (c - '0')) / 10 < n)
printf("Overflow would be caused by appending %c", c);
else
printf("unexpected logic error");
printf(" after %d\n", n);
return 0;
}
Notice how each of the three cases outlined above are handled separately in the if statement? In addition, special output is used for the case where a newline is encountered. But what about the else? It is for a case we didn't catch. If no other condition was true, the code associated with the else condition is executed.
Upvotes: 1
Reputation: 121387
Another simple example is to grade marks (mark should be between 0 - 100):
if (mark < 50 && mark >=0)
{/* Fail */ }
else if (mark >=50 && mark <60)
{/* E grade */}
else if (mark >=60 && mark <70)
{/* D grade */}
else if (mark >=70 && mark <80)
{/* C grade */}
else if (mark >=80 && mark <90)
{/* B grade */}
else if (mark >=90 && mark <=100)
{/* A grade */}
else
{/* This is the impossible "error" condition that you look for! */}
Upvotes: 1
Reputation: 106012
You can test it by writing two simple programs as;
#include <stdio.h>
int main(void)
{
int x;
scanf("%d", &x);
if(x == 1)
printf("ON");
else if(x == 0)
printf("OFF");
}
If user inputs other than 0
or 1
program will terminate without showing any message with exit status 0
. Now consider the program
#include <stdio.h>
int main(void)
{
int x;
scanf("%d", &x);
if(x == 1)
printf("ON");
else if(x == 0)
printf("OFF");
else
printf("Unexpected Input");
}
In this case the last else
is used to catch the possibility that user can enter the value other than 0
or 1
and show you a message.
Upvotes: 1
Reputation: 25908
"Impossible" here means "shouldn't happen", not literally "can't happen", it being obviously pointless to check for the latter. In other words, you think you've written your code so that a variable can, at a given time, only have the values 1
, 2
or 3
, and you write your if
statements to handle them, but then add a final else
clause that does something to grab your attention so that if you're wrong, and your code is not as watertight as you thought, then you'll be able to see, and the bug won't just escape your attention.
For instance:
if ( val == 1 ) {
/* Do something normal */
} else if ( val == 2 ) {
/* Do something else normal */
} else if ( val == 3 ) {
/* Do something else else normal */
} else {
/* We shouldn't get here, but just in case we do... */
printf("ERROR - val has an unexpected value!\n");
exit(EXIT_FAILURE);
}
Upvotes: 2
Reputation: 1345
An else will match any condition not matched by any if/else if. Thus, your are guaranteed your code will always enter either one of the "if/else ifs" blocks, or the else. If you look up the switch statement, an else can be compared to the "default" and each if/else if to a case.
Example:
int age = 18;
if (age < 18)
printf("You are under 18");
else if (age == 18)
printf("You are 18 years old");
else
printf("You are older than 18");
Upvotes: 1
Reputation: 95958
Maybe something like this:
int manipulteBit(int bit) {
if(bit == 0) {
//it's 0
//...
} else if(bit == 1) {
//it's 1
//...
} else {
//error, I expect 0 or 1
}
}
If you wrote a function that takes a number and should decide it it's 1 or 0, it should know how to deal with "invalid" input.
Upvotes: 2