vendredi 20 août 2021

if statement negation in C

Hello I'm currently doing a programming exercice and there is something I want to be sure about.

It is about the if statement negation like so : if(!(condition)), what's the difference between the if statement with negation and not ? Here an example of a program that has give me the result i want with if(!(condition)) and not with if(condition).

Here is the function, the pupose of this function is as you can see checking if a string is a number or not. this function works I've tested it.

bool check_key(char* key){
    for(int i =0; i <strlen(key); i++)
    {
        if(!isdigit(key[i]))
            return false;
    }
    return true;
}

But this version doesn't work why ?

bool check_key(char* key){
    for(int i =0; i <strlen(key); i++)
    {
        if(isdigit(key[i]))
            return true;
    }
    return false;
}

have I missed something ?

sorry if my english is bad I'm not native.

Aucun commentaire:

Enregistrer un commentaire