+ 4

Can someone explain why the not function is useful?

I can't understand a circumstance where having the not (the inverted) function would be more useful than the affirmative version. can someone give a good example where the not function is crucial to the success of a code?

28th Feb 2018, 5:16 PM
mellocellofello
3 Answers
+ 17
example : u want statments to execure for no not div. by 13 , then u can write if (x%13!=0) {//statement (s) }
28th Feb 2018, 5:42 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 7
I'd be happy to help you.....NOT. As far as I can tell, it does wonders for sarcasm. ;) I'm only joking. Basically, it's what you'll use when you're wanting to do something based on something "not happening" instead of when something "is happening." Maybe there is nothing that you want to happen if it exists, but if it doesn't exist, you want something to happen. For example, maybe check if a file exists on a file system. You could check if the file does NOT exist, and if it doesn't create the file and prepare it for further access. It's a means of simplifying the code really. ::: EXAMPLE :::: bool isOn = false; if(isOn == true) { // do this if true } else { // do that if false } if(isOn) { // only do this if true, do nothing if false } if(!isOn) { // only do this if false, do nothing if true } ^That's just a really simple example of it using a boolean. It shortens your condition and allows you to more easily address either scenario even if you're not wanting to do something for both. You can also use this for the other types also. Maybe you want to allow all input except for the number 13 (because it's bad luck or something?). You could use NOT in this situation also. int num = 0; if(num != 13) { // do this for all numbers except 13 } else { // do this if it's the number 13 } ^See what I mean? It's just a means of convenience really and you can go about the same thing in others ways also. This is just more efficient for most things.
28th Feb 2018, 5:32 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
wow @Jakob. your not leaving any explanation left for others who want to answer the questions. can't say anything but well done.! ;)
28th Feb 2018, 7:03 PM
Farshaad Heydari
Farshaad Heydari - avatar