Sri ManakulaVinayagar Engineering College, Puducherry
DEPARTMENT OF ELECTRICAL & ELECTRONICS ENGINEERING
Subject Name : ENGINEERING COMPUTATION WITH MATLAB
Subject Code : U19ECO41
Prepared by : Mr. I.Shivasankkar
Verified by: Approved by:
UNIT – II LOOPS & CONTROL STATEMENTS
Introduction; Relational & Logical operations - Example programs - Operator precedence- Control
& Decision statements- IF - IF ELSE - NESTED IF ELSE - SWITCH - TRY &CATCH - FOR -
WHILE - NESTED FOR - FOR with IF statements, MATLAB programorganization, Debugging
methods - Error trapping using eval & lastern commands.
RELATIONAL OPERATORS
MATLAB relational operators include all common comparisons, as shown in thefollowing table:
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
LOGICAL OPERATORS
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
OPERATOR PRECEDENCE
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
CONTROL AND DECISION STATEMENTS
LOOPS
for Loop
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
nested for Loop
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
while loop
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
BRANCHES CONTROL STRUCTURES
if Control Structure
if Structure
if-else Structure
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
if-elseif-else Structure
nested if Structure
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
switch Statement
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
break Statement
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
continue Statement
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
error Statement
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
try-catch Structure
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
ERRORS AND WARNINGS
Types of Errors
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
ERROR HANDLING USING FUNCTIONS eval and lasterr
Displaying Error Messages
Displaying Warnings
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
MATLAB DEBUGGER
Setting Breakpoints
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Examining Variables
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
Changing Workspace Context
Stepping through Code and Continuing Execution
Ending the Debugging Session
Debugging from the Command Line
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements
Sri ManakulaVinayagar Engineering College, Puducherry
SAMPLE QUESTIONS
2 marks
1. What is a relational operator?
2. What is a logical operator?
3. What is a breakpoint?
4. What is the need of using stack pull-down menu?
5. Write the syntax of ‘if’ statement.
6. Write the syntax of ‘for’ statement.
7. Write the syntax of ‘switch’ statement.
8. What is the function of looping structures?
5 marks
1. Illustrate on the usage of relational operators along with mathematical expression with an example.
2. Demonstrate on logical ‘AND’ operator with an example program.
3. Explain briefly on operator precedence through an example.
4. Summarize briefly on the ‘switch’ statement structure.
5. Illustrate the process of using ‘try-catch’ structure while executing a program.
6. Explain the concepts of error handling using functions ‘eval’ and ‘lasterr’.
10 marks
1. Develop a MATLAB program to add the numbers till the number entered is 0, using while loop.
2. Develop a MATLAB program to find the largest of the three numbers using if-elseif-else structure.
3. Explain in detail about relational and logical operators with example programs.
4. Demonstrate in detail about the debugging methods in MATLAB.
Engineering Computation with MATLAB: Unit-2 – Loops & Control Statements