Not operator in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Not operator in c

https://code.sololearn.com/c2QVVBBEXtCf/#c I am having a great confusion in not operator.. Can anyone explain me clearly about it that how it evaluates in the above program.

15th Oct 2018, 4:54 AM
Dhiviya Thirumavalavan
Dhiviya Thirumavalavan - avatar
3 Answers
+ 1
First you have to understand what % is doing. It finds the modulo(reminder of a division) of a number. In C any non-zero value is considered true. So, if(!(2%2)) is true so the loop continues same for if(!4%2) is true and the loop continues without increasing the value of count. 4%2 = 0 2%2 = 0 which in boolean logic false but the condition is if(not (0)) continue; so it continues. if(not (nonzero)) is false- this line may not make sense at first so stick with the earlier explanation.
15th Oct 2018, 5:32 AM
Akib
Akib - avatar
0
Akib Reza After your explanation.. I understanded it as if(!(0%2)) In case of modulo it takes the remainder value so 0%2 is 0 so if(!0) means the value is not zero , so it is a nonzero value like 1 or -1.. Nonzero value means it is true so the loop continues.. The above condition is applicable 0,2,4 and 6 In case of 1,3,5...how I need to consider??? if(!(1%2)) so the remainder as 1 if(!1) whether it becomes 0 or anyother value ??
15th Oct 2018, 7:20 AM
Dhiviya Thirumavalavan
Dhiviya Thirumavalavan - avatar
0
Its just boolean. if(!1) is false hence the count++; You should study basic boolean algebra and logic gates. It will clear things up more. if just checks whether the condition is true or false. If its true it continues else doesn't. if(0) is false so nothing works inside condition if(!nonzero) is also false and doesn't do anything
15th Oct 2018, 9:32 AM
Akib
Akib - avatar