Using the last else declaration in an Else-If construct

The book "C Programming Language" says that the last else statement in the if-if construct can be used to check for errors to catch an impossible condition.

Can someone provide a simple example for this?

+4
source share
6 answers

“Impossible” here means “should not happen”, and not literally “cannot happen”, which, obviously, it makes no sense to check for the latter. In other words, you think that you wrote your code so that the variable could only have values ​​at a certain point 1, 2or 3, and you write your statements ifto process them, but then add a final sentence elsethat will do something to attract your attention, so if you make a mistake and your code is not as waterproof as you thought, then you can see, and the error won’t just avoid your attention.

For example:

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);
}
+2
source

Maybe something like this:

int manipulteBit(int bit) {
   if(bit == 0) {
      //it 0
      //...
   } else if(bit == 1) {
      //it 1
      //...
   } else {
      //error, I expect 0 or 1
   }
}

, , 1 0, , "" .

+2

, if/else if. , , if if else, else. switch, else "default", if/else - .

:

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");
+1

, :

#include <stdio.h>
int main(void)
{
    int x;
    scanf("%d", &x);

    if(x == 1)
        printf("ON");
    else if(x == 0)
        printf("OFF");
}  

, 0 1, , 0.

#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");
}  

else , , , 0 1, .

+1

( 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! */}
+1

, , , , . getchar. :

  • , . , getchar EOF.

  • , , . , \n. , , , , .

  • . , 214748364, int, 2147483647, 2147483648, .

:

#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;
}

, if? , , . ? , . , , else.

+1

Source: https://habr.com/ru/post/1526029/


All Articles