C/C++: Syntactic Sugar for If Statement


A standard If Statement in C/C++ can become an one line statement by using the following structure

variable = (STATEMENT)?(RETURN VALUE IF TRUE):(RETURN VALUE IF FALSE)

which is the same as

if (STATEMENT){
  variable = (RETURN VALUE IF TRUE);
}else{
  variable = (RETURN VALUE IF FALSE);
}

This post is also available in: Greek

Leave a Reply