COZYHUB
  • Home
  • Disclaimer
  • Privacy Policy
  • About
  • Contact

What is the meaning of "if (x) { … }" where x is an integer in C++? [duplicate]


I saw some c++ syntax that puzzled me

int x = 10;if(x){ //this line confused me//code }else{//code }

i don't understand how this is valid code, what does the if(x) do?

1

Best Answer


An int converts implicitly to a bool. Any int that's non-zero evaluates to true. A zero integer converts to false. In your case, that line basically tests whether x is different from zero, and it is equivalent with

if(x != 0) ...

Random Posts

Is there a tmux shortcut to create a new session?Performance issue in Golang's key-value store (Badger DB)How can I convert bool true or false to string "True" or "False"EAS ignoring local versionCode in buildeclipse console text colorsRotate a view 360 degrees infinitelyInstall ipdb for Python 3? [duplicate]python 'int' object has no attribute 'sort'Generate random 1000 profile picturesif/else constructs inside and outside functionsHow to pass multiple parameters in a querystringhow to delete 2 alias entries from keystore?Replace values in R, "Yes" to 1 and "No" to 0How do I add multiple "NOT LIKE '%?%' in the WHERE clause of sqlite3?The total number of locks exceeds the lock table sizeWhat’s the difference between Process and ProcessStartInfo in C#?How to mock es6 class using JestHow to drop a list of rows from Pandas dataframe?What does the $SHELL command do besides output the shell when I type it at the command prompt?How to call shell commands from Ruby
Designed by COZYHUB