I'm just learning ROR and I came across the if / else statements. I also came across 'elseif' but my text editor (textmate) doesn't pick it up as a keyword, nor does the program run properly.
if name == 'Chris' puts 'What a lovely name.' elseif name == 'Katy' puts 'What a lovely name!' end I'm using the book Learn to Program, which was written several years ago. I was wondering if the 'elseif' was changed because, when I simply use 'else', it seems to function properly.
It only has if (expression) statement else statement . Because an if statement is a statement (obviously), you can use it after else , just like any other statement, like for or empty ; or switch or any arbitrary expression statement.
The if/else statement With the if statement, a program will execute the true code block or do nothing. With the if/else statement, the program will execute either the true code block or the false code block so something is always executed with an if/else statement.
elseif , as its name suggests, is a combination of if and else . Like else , it extends an if statement to execute a different statement in case the original if expression evaluates to false .
The if-else statement is used to execute both the true part and the false part of a given condition. If the condition is true, the if block code is executed and if the condition is false, the else block code is executed.
It's elsif, not elseif.
Slightly confusing when you're new, probably.
Try with elsif (no second e).
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With