Jump to content

today in class the teacher mentioned the problem with pascal languaje where with two consecutive if clauses on the lenguaje definition there is no way to know if the else clause belongs ither to the first of the second if. it goes more or less like this:

if (condition) then if (condition2) <expresion> else <expresion>

 

this problem was solved with a complicated grammar but that's not the point. after this, the class went into some kind of debate going about what would happen in C. after thinking for a while we gave reasons saing that the else would correspond to the first if and other saying that it would correspond to the second if.

 

before you go ahead and try to find out the answer with barely 3 lines of code, i've already done it, i would like to raise the debate here, and this is it:

 

on the folowing C code, what would be the output?

int a =1;if (a==0) thenif (a==2) then printf("out1");else printf ("out2");

have fun thinking about it before trying it

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to comment
https://linustechtips.com/topic/315962-if-clauses-and-order/
Share on other sites

Link to post
Share on other sites

I didn't know you could chain if statements like that. I guess a bracketless if statement just terminates at the first semicolon.

 

Here's another fun one:

int t = 1;if (!(0 > t))printf("test1");if (!0 > t)printf("test2");

or the second one in my sig

 

fixity is fun to screw with.

Link to comment
https://linustechtips.com/topic/315962-if-clauses-and-order/#findComment-4297903
Share on other sites

Link to post
Share on other sites

The else block is evaluated if none of the other conditions evaluates to true. This behavior holds whether there is a single if-else, or an arbitrarily large number of if statements chained together. If even one of the previous if or elseif statements evaluates to true, then the else block is ignored.

 

As far as I know this holds in all C-style languages and most other languages that have similar conditional control structures. I'd be interested to hear counter examples.

 

Bracketless if statements are dumb and people that use them are a pain.

Link to comment
https://linustechtips.com/topic/315962-if-clauses-and-order/#findComment-4298542
Share on other sites

Link to post
Share on other sites

The else block is evaluated if none of the other conditions evaluates to true. This behavior holds whether there is a single if-else, or an arbitrarily large number of if statements chained together. If even one of the previous if or elseif statements evaluates to true, then the else block is ignored.

 

As far as I know this holds in all C-style languages and most other languages that have similar conditional control structures. I'd be interested to hear counter examples.

 

Bracketless if statements are dumb and people that use them are a pain.

The else is only triggered if the second if evaluates to false and the first evaluates to true. 

 

There's nothing wrong with bracketless if statements if you are doing single liners with them e.g. if (empty) return; This is just silly though.

Link to comment
https://linustechtips.com/topic/315962-if-clauses-and-order/#findComment-4298967
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×