I wonder... Which is easier if-else statement or switch-case statement to be learned for first?
I wonder... Which is easier if-else statement or switch-case statement to be learned for first?
Just GET it OFF out my mind!!
I think that if-else statements may be easier to learn since you would not also need to explain the concept of fall through (assuming that the programming language's switch construct has such a feature) while explaining the concept of control flow branching.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
However, I just confused with if (nested statement) which is a part of if statement.
For instance, I'll give this easy-to-understand (I think) for my friends...
That's mean whenever you hungry you'll eat some food first, even if you're not you will still go to the campus.Code:bool hungry = true; if( hungry ) { print( "Eating some food..." ); } print( "Go to campus!" );
But, I still don't get the *right* example (analogy) for the nested one.
Obviously the code above still can be changed to:Code:bool hungry = true; if( hungry ) { if( momCooking ) { print( "Eating some food..." ); } else { print( "Go to restaurant to eat." ); } } print( "Go to campus!" );
Ohhh yeah... I don't get it.Code:bool hungry = true; if( hungry && momCooking ) { print( "Eating some food..." ); } else if( hungry && !momCooking ) { print( "Go to restaurant to eat." ); } print( "Go to campus!" );
Just GET it OFF out my mind!!
Actually, mom may be cooking and we just aren't hungry.
can be false if hungry is false and momCooking is true, if hungry is true and momCooking is false, or if hungry is false and momCooking is false.Code:if ( hungry && momCooking )
In this case, I would say it is irrelevant whether or not momCooking until we determine hungry, so the "outter" if statement should only check whether hungry is true or not. If it isn't, we don't even need to bother checking for momCooking.
is much clearer.Code:bool hungry = true; if( hungry ) { if( momCooking ) { print( "Eating some food..." ); } else { print( "Go to restaurant to eat." ); } } print( "Go to campus!" );
I would only combine expressions in an if statement (or a [do-]while/for condition) if each expression must be resolved before we determine a course of action.
abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "
[edit] oops, I just noticed the bool hungry = true, so you're right in this case. However then I would say it is not useful in this case to have hungry even in the if statement since we are really only checking to see whether momCooking or not.
However, if we remove the initialization to true and instead assume that hungry might be true or false (determined earlier in the code), then my statements are valid.[/edit]
[edit2]gah, sorry for the double post, hit the wrong button.[/edit2]
abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "