MATLAB Control Statements

What are control statements?

The name itself suggests that something is being controlled, these control statements decide or alter the flow of execution ina program. In program, we often need to modify or alter few lines of code, these control statement helps to perform this type of task.

Four categories of control statements are sequence control statements, decision control statements, repetition control statements (looping statements), and case control statements.

MATLAB control statements.

MATLAB Control Statements

If…end

If…end statement helps to execute a block of code by satisfying a condition. The condition is defined by user at the time of coding. This if… block of code is closed with the keyword “end”. We can also use elseif, else if further conditioning is required.

Let us look at the syntax and few examples below.

Syntax:

 if exp1
 set of statements
 end 

Flowchart:

MATLAB Control Statements

Example:

 j = 10;
 if j ~= 0
 disp('Nonzero value')
 end 

Output:

Nonzero value

If-else…end statement

This control statement executes at most one no. of commands on which condition evaluates true, and a default block is evaluated if conditions are not met.

The control flows in this direction à if clauseà1stelseif clause à 2ndelseif clause àelse clause.

Let us look at the syntax and few examples below.

Syntax:

 if exp1
             statements block
 else
             Statements block
 end 

Flowchart:

MATLAB Control Statements

Example:

 If size(d) is equal to size(f), arrays are concatenated otherwise an empty array is returned as output.
 d = ones(2,3);
 f = rand(3,4,5);
 ifisequal(size(d),size(f))
    k = [d; f];
 else
 disp('d and f are unequal.')
    k = [];
 end 

Output:

d and f are not the same size.

If-elseif-else….end

This control statement is the extended version of previous control statement, here we keep on adding elseif clause according to our requirement.

Let us look at the syntax and few examples below.

Syntax:

 If exp1
             Statements block
 elseif exp2
             Statements block
 else
             Statements block
 End 

Flowchart:

MATLAB Control Statements

Example:

 x = 20;
 min = 2;
 max = 6;
 if (x >= min) && (x <= max)
 disp('within specified range')
 elseif (x > max)
 disp('exceeds maximum ')
 else
 disp(' is below minimum')
 end 

output:

exceeds maximum

Nested if-else

Nested if-else is used when we decide to condition set of statements inside statements, but each if clause requires the end keyword. Let us look at the syntax and few examples below.

Syntax:

 if exp1  
     Statements block 
     if expression  
     Statements block 
     else  
     Statements block 
     end  
 elseif exp2  
     Statements block 
     if expression  
     Statements block 
     end  
 else   
     Statements block 
 end 

At first expression-1 is checked, the following set of statements are executed then control flows to if-else block. Elseif expression2 is further checked weather the condition gets satisfied or not. If none of the above condition is satisfied then at last else-block is executed with the end keyword.

Flowchart:

MATLAB Control Statements

Example:

 k =900;
 i=800;
 if( k ==900)
 if(i==800)
 fprintf('Value of k is 900 and i is 800\n');
 end
 end
 fprintf('value of k is : %d\n', k );
 fprintf('value of i is : %d\n',i); 

output:

 Value of a is 900 and b is 800
 value of a is : 900
 value of b is : 800 

Switch Statement

Switch statement can be an alternative of if-else statements, it is used to test a input against set of rules already defined. This control statement executes one of the several group of statements.

Let us look at the syntax and few examples below.

Syntax:

 switch switch_expr  
     case case_exp1  
         set of Statements  
     case case_exp2  
         set of Statements  
     case case_exp..N  
         set of Statements    
     otherwise  
         set of Statements  
 end   

Flowchart:

MATLAB Control Statements

Example:

 % program to convert number to string
 a = input('enter a number from 1 to 7  : ')  
 switch  a  
     case 1  
         disp('number in wordsàone')  
     case 2  
         disp(' number in wordsàtwo')  
     case 3  
         disp(' number in wordsàthree')  
     case 4  
         disp(' number in wordsàfour')  
     case 5  
         disp(' number in wordsàfive')  
     case 6  
         disp(' number in wordsàsix')  
     case 7  
         disp(' number in wordsàseven')  
     otherwise  
         disp('not a valid number')  
 end 

Output:

 enter a number: 4
 number in wordsàfour 

Related Topics

Matlab Calculus

Analytics Calculus is significant in various fields like designing, physical science, and different sciences, yet the estimations are finished by machines or PCs, regardless of whether they are into Physics...

3 minutes read.

MATLAB Variables

What are variables? A variable is a storage location or a container to store or hold data. In programming, variables are used to reservedata created by the programmers so that it...

4 minutes read.

MATLAB’s Array

What is an Array? An array is a collection of similar datatype elements in contiguous memory location. Every element in array is stored with an index number starting from 0. First...

4 minutes read.

MATLAB Strings

You can address text in MATLAB utilizing string exhibits. Every component of a string exhibit stores a grouping of characters. The arrangements can have various lengths without cushioning, for example,...

5 minutes read.

Matlab Square Wave Signal Generation

What is Square Wave signal Generator? A “square wave” is a sort of non-sinusoidal waveform, most normally experienced in gadgets and sign preparing. An optimal square wave substitutes consistently and momentarily...

3 minutes read.

MATLAB Tutorial

Introduction of MATLAB In this MATLAB(stands for matrix laboratory) tutorial, we will cover core concepts of MATLAB and provide a solid foundation to enhance high computing skills. We are going to...

4 minutes read.

Differentiation in Matlab

Differentiation in Mathematics In maths, Derivatives are an essential device of math or calculus. For instance, derivative of the situation of a moving article regarding time is the item's speed: this...

6 minutes read.

MATLAB Operators

What is an Operator? A specific operation is performed by using a symbol which is called an operator. Example: A+B, where A, B are called operand and '+' is called operator. If you...

8 minutes read.

Matlab function

Introduction: Functions in MATLAB are composed with code that link one variable with another, and yielded output is connected precisely to one specific information that shapes a significant piece of any...

6 minutes read.

MATLAB Control Statements

What are control statements? The name itself suggests that something is being controlled, these control statements decide or alter the flow of execution ina program. In program, we often need to...

3 minutes read.

MATLAB Vector

Introduction to MATLAB vector Vectors are one of the representations of arrays. It tends to be addressed in two different ways line(row) vector and section(column) vector. A vector is defined as...

5 minutes read.

Matlab Matrix

Matlab Matrix What is a Matrix? a rectangular array of expressions arranged in particular rows and columns that is treated as a solitary substance and controlled by specific standards. MatlabMatrix A Framework called matrix,...

5 minutes read.

MATLAB Butterworth filter

What is Butterworth filter? The Butterworth channel is a kind of sign preparing channel intended to have a recurrence reaction as level as conceivable in the passband. It is likewise alluded...

3 minutes read.

MATLAB Linear Convolution problem solving

What is linear convolution? In science (specifically, useful examination), convolution is a numerical procedure on two capacities (f1 and g1) that communicates how the state of one is adjusted by the...

4 minutes read.

MATLAB Commands

MATLAB Commands MATLAB (stands for matrix laboratory) is a high-level programming environment for scientific calculation, and it provides functions more efficiently than other programming languages. In addition, it has many in-build...

3 minutes read.

Matlab Unit impulse signal generation

What is the Necessity of learning Matlab programs? Matlab Programming Examples give you an idea of typical Matlab programs in a nutshell.Matlab programming should be utilizing object-oriented programming, GUI programming, and...

3 minutes read.

Matlab Algebra

Up until now, we have seen that every one of the models work in MATLAB just as its GNU, then again called Octave. In any case, for addressing fundamental mathematical...

6 minutes read.

MATLAB Data types

What is Data Type? A data type is a classification that helps to decide what class of value a variable can reserve in programming. And also, what sort of operations can...

6 minutes read.

MATLAB special graphic function

In this section, we portray a greater amount of MATLAB's illustrations graphic functions and the most well-known methods of controlling and redoing graphics. Let’s type help designs, help graph2d (for...

7 minutes read.

Matlab DFT and IDFT

Matlab Program- (DFT and IDFT of a sequence) Program for DFT (Discrete Fourier change) and IDFT (Inverse discrete Fourier change) of a sequence. Program explanation: In Arithmetic, the discrete Fourier change (DFT) changes...

3 minutes read.