...

/

Conditional Statements

Conditional Statements

Explore conditional statements in MATLAB and how to use them in Python.

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.

Press + to interact
Difference in the syntax of if statement in MATLAB and Python
Difference in the syntax of if statement in MATLAB and Python

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:

Press + to interact
Guess = 5;
if Guess == 5 % The condition is true
disp("Congratulations!!! we guessed the right number.") % The code is executed
end
  • Line 3: We use the if statement to check if Guess is equal to 5.

  • Line 4: If Guess is equal to 5, then this line will execute and print the message using the ...