Truth tables issue

139 views Asked by At

When I check my input from a text box, I pass it into a loop to check individual characters.

I'm a little uncertain as to why this is happening, but when I input a "/", the function reads it as false?

 // Checks the Password contains the correct input 
 for (i = 0; i < passwordLength; i++)
 {
     v = Convert.ToInt32(passwordArray[i]);
     // (not 0-9 or not A-Z or not a-z) 
     // "/"(converted to 47) reads as (true)+(true)+(false) = false
     if ((v < 48 || v > 57) && (v < 65 || v > 90) && (v < 97 || v > 122))
     {
         e = 2;
     }
 }
2

There are 2 answers

2
Jade On BEST ANSWER

try this in your if condition

if (!(v >= 48 && v <= 57) && !(v >= 65 && v <= 90) && !(v >= 97 && v <= 122))
0
Ben On

this works in the same way, just a bit less confusing I think.

if( !((v >= 48 && v <= 57) || (v >= 65 && v <= 90) || (v >= 97 && v <= 122)))

However, this works also.

((v < 48 || v > 57) && (v < 65 || v > 90) && (v < 97 || v > 122))

The top function, using || (or) as a comparison, leans towards true:

(true) || (false) = (true);

But with the ! (not) added, the outcome is inverted:

!((true) || (false)) = (false);

The second function, using && (and) as a comparison, leans towards false:

(true) && (false) = (false);

@Jade thanks for helping me to get an understanding of it :)