How to write if-else statements

preview_player
Показать описание
We see how to write if-else statements:

00:00 -Introduction
00:13 -Syntax for an if-else statement
01:26 -Example 1: Check for a value less than 0.5
03:21 -Example 2A: Check if a given scalar is even or odd
08:32 -Example 2B: Error check for devious users
12:16 -Example 3A: Check if two arrays have the same size
15:18 -Example 3B: Limit test to variables stored using double class
18:00 -Example 3C: Investigate the performance of our Example 3 code
20:29 -Example 4A: Decide to play a game
23:01 -Example 4B: Develop nested if statements
28:04 -Community challenge: If-else versus simple if statements
28:37 -Conclusion
Рекомендации по теме
Комментарии
Автор

In if statements, you are done with that function if it does not meet that criteria. If there is an "else" function added onto it, it would be like going from this or not that to this or another thing. Overall if else reduces redundancy and improves code efficiency.

ryotaisomatsu
Автор

If we didn't have if-else functionally in MATLAB, we would have used a lot of nested if statements for just simple tests, writing repeated and more complicated codes.
For example, we want to know whether the user wants to play the game or not and so give users a choice of selecting between Yes and No. In this case, if we did not have an if-else function, we would have written our code if choice == 1 do something, and another if block after that code saying if choice == 2, do something. This will make us write repeated codes again and again, which is considered a bad habit in programming.
I think that If-else statements are good to use if we can predict the possible output of users or if we have limited choice for users.

xoheey