Conditional Statements
Explore conditional statements in MATLAB and how to use them in Python.
We'll cover the following...
A conditional statement is a type of programming construct that allows programmers to specify one or more conditions and then specify a block of code that will be executed if those conditions are met.
The basic syntax of a conditional statement is an if keyword followed by the condition to be evaluated and then a block of code to be executed if the condition is true. There could be multiple conditions for which we use the keywords, such as if-else, if-elif-else, etc. Depending on the programming language, the syntax of the statement might have different forms, but the general idea is the same.
The if statement
The if statement is used to check whether a certain condition is true or false, and a block of code is executed only if the condition is true.
The following is the code for the if statement in MATLAB:
Guess = 5;if Guess == 5 % The condition is truedisp("Congratulations!!! we guessed the right number.") % The code is executedend
Line 3: We use the
ifstatement to check ifGuessis equal to5.Line 4: If
Guessis equal to5, then this line will execute and print the message using thedisp()command. ...