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
RECOGNIZER and TRANSLATOR: i want to recognized devanagari word using matlab. I have done it using template matching. but i am not get devanagari word at the output. But get speci

Operator Precedence Rules: A few operators have precedence over the others. For illustration, in the expression 4 + 5 * 3, the multiplication takes the precedence over additio

hey ! why the command sawtooth and square does not exist in Matlab R2012a?

Objectives In the following exercises, students are required ?rstly to implement code to determine the frequency components present in a signal, and then to extract a desired si

For a statistically stationary environment it would be advantageous to use gear shifting, that is to reduce the adaptation gain with time. To illustrate this, try using a varying a

Illustration of Modifying Elements: Illustration, the fifth element in the vector newvec is 9 >> newvec(5) ans = 9 The subset of a vector, that would be a vector it

ACTIVITIES OF A PROJECT 1-2 1-3 1-4 1-5 2-6 3-6 3-7 4-7 5-7 7-6 6-8 7-8 THE COMMPANY LOSES 2,000 FOR EVERY WEEK THE PROJECT LASTS BEYOND 30 WEEKS. fOR EACH OF THE PROPOSALS; ACTIVI

Illustration of a conditional loop - While loop: As an illustration of a conditional loop, we will write a function which will find the first factorial which is greater than t

I want to know how to create a numerical solution code for analysing temperature/isotherms on a 2d square plate with a singular heat source and opposing edges being held at constan

Illustration of switch statement: Here are the two illustrations of calling this function: >> quiz = 22; >> lg = switchletgrade(quiz) lg = X >> quiz = 9; >>