If-else statement, MATLAB Programming

The IF-ELSE Statement:

The if statement selects whether an action is executed or not. Selecting between the two actions, or selecting from numerous actions, is accomplished by using if-else, nested if, and switch statements.

The if-else statement is used to select between the two statements, or sets of statements. The common form is as shown below:

if condition

action1

else

action2

end

 

At First, the condition is computed. If it is theoretically true, then the set of statements elected as action1 is executed, and that is it for the if-else statement. If rather the condition is theoretically false, the second set of statements elected as action2 is executed, and that is it. The first set of statements is known as the action of the if clause; it is what will be executed if the expression are true. The second set of statements is known as the action of the else clause; it is what will be executed if the expression is false. One of these actions, and only one, will be executed-that one depends on the value of the condition.

For illustration, to establish and print whether or not a random number in the range from 0 to 1 is less than 0.5, an if-else statement can be used:

if rand < 0.5

disp('It was less than .5!')

else

disp('It was not less than .5!')

end

Posted Date: 10/19/2012 3:18:31 AM | Location : United States







Related Discussions:- If-else statement, Assignment Help, Ask Question on If-else statement, Get Answer, Expert's Help, If-else statement Discussions

Write discussion on If-else statement
Your posts are moderated
Related Questions
Creating matrix variables- arguments: The CAT arguments dimensions are not reliable. The Iterators can also be used for the values on the rows by using the colon operator;

Illustration of Function functions: For illustration, to pass the sin function into fplot, one would pass its handle as shown in figure for the result.   >> fplot(@s

Write a MATLAB program to find the undamped natural frequencies and modes of an electric car treating it as a four degree of freedom system. Determine the undamped natural frequenc

Illustration of Output statements: For illustration, >> disp('Hello') Hello >> disp(4^3)   64 The formatted output can be printed to the screen by using the fpr

Solve the optimality condition for each P equation against M according to the following relation: Condition for Optimality:         ∇ M = λ ∇ P      with respect to C and T.

Example of variable names: If nothing appears when, who or whose is entered, which means there aren't any variables! For illustration, in the starting of a MATLAB session, the

how to wriet program for batch reactor

Grid function: grid shows grid lines on the graph. Called by itself, it is a toggle which turns the grid lines on & off. Alternatively, the commands grid on & grid off can

Write a script to determine the volume of a pyramid, which is 1/3 * base * height, where the base is length * width. On time the user to enter values for the length, width, and th

This assignment is designed to compare performance between Matlab and Excel for performing nonlinear regression analysis of a set of data. There are two data sets in the accompa