Search code examples
cif-statementlogical-orlogical-and

If statement in a do while loop being ignored


I am trying to make a program that detects a user input and prints stuff based on that input. The input needs to have limits set however the if statements I set up are being ignored and I'm not quite sure why

#define _CRT_SECURE_NO_DEPRECATE 1
#define _CRT_NONSTDC_NO_DEPRECATE 1
#include <stdio.h>
#include <conio.h>
#include <Windows.h>

int main(void) 
{
    int num;
    int exit; 

    exit = 0;

  printf("\nLab5 p1 by Chung - En Hou\n");

    num = 0;
    do{
        printf("\nEnter a number between 0 and 255:");
        //input
        scanf("%d", &num);
        //ignored if statement?
        if (num < 256 || num >= 0) {

            Sleep(250);
            printf("%d\n", num);
            Sleep(250);
            printf("%x\n", num);
            Sleep(250);
            printf("%c\n", num);

            printf("Press any button to Run the program again or press Esc to exit");

            exit = getch();
        }
        //else also ignored
        else {
            printf("\nthat number is turbo cringe please try again\n");
            printf("\nPress any button to Run the program again or press Esc to exit\n");
            exit = getch();
        }
    } while (exit != 27);


      return 0;
}

Solution

  • I think you mean

        if (num < 256 && num >= 0) {
    

    instead of

        if (num < 256 || num >= 0) {
    

    That is the if statement checks whether the value of num is in the range [0, 255]

    As for the expression in the if statement shown in the question then it will evaluate to true for any integer value of the variable num.