0% found this document useful (0 votes)
12 views7 pages

تقرير if For

The document provides an overview of control instructions in MATLAB programming, detailing conditional control instructions like 'if' and 'switch', as well as repetition control functions such as 'for' and 'while'. It also covers error control commands, exception statements, and examples of using 'if' statements and 'for' loops in MATLAB. Overall, it emphasizes the importance of these instructions for structuring algorithms and implementing programs effectively.

Uploaded by

ara30612
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

تقرير if For

The document provides an overview of control instructions in MATLAB programming, detailing conditional control instructions like 'if' and 'switch', as well as repetition control functions such as 'for' and 'while'. It also covers error control commands, exception statements, and examples of using 'if' statements and 'for' loops in MATLAB. Overall, it emphasizes the importance of these instructions for structuring algorithms and implementing programs effectively.

Uploaded by

ara30612
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Control and control instructions are among the most used instructions in

MATLAB programming, as is the case in all other programming languages.


They are also among the instructions that help in building programs and
structuring algorithms for their wide use and ease of program
implementation, where operations are applied to arrays and operations are
repeated through a process that executes a specific control instruction with
another control instruction. That is, control and control instructions can
overlap, such as the conditional if tool with the for repetition control
instruction. They are also characterized by their ease of use, overlap, and
flexibility in linking them with other control instructions. Control and control
instructions vary to be used according to the required need for them in the
appropriate place within the specified scope. The structure of the program is
as follows:

1- Conditional control instructions

This type of conditional control is divided into two types:

A) If condition instructions: This is a main instruction through which


the specific or required condition is set, where the condition is
set here that aims to nominate the appropriate operation or the
specific case that is to be applied to the general case, and the
conditional statement or sentence usually represents an
organized mathematical operation such as greater than or less
than that produces a result based on a specific input or a series
of successive inputs. B) Switch: This tool is widely used in
programming because it enables us to execute a set of long
operations with several conditions within a special case.

2- Repetition control functions: It is divided into four categories:

a) Open control function for: By means of this tool we can repeat the
execution of a series of operations within the part allocated to it in the
program and we can repeat a specific operation as needed to reach the
desired output or results.

b) Conditional control function while: This tool can execute a set of


operations, but the work of this tool ends with the control of the specific
condition that was placed within the tool.

c) Passage and traversal function continue: It is a command that works on


either executing a specific operation or crossing it within a series of
operations that will be executed.
D (break execution command: This command works on wrapping the work of
the repetition tool when a specific condition is controlled within the program
structure

2- Error control commands

These commands are called

A) Error execution command try: where there may be a specific operation in


which an error occurred within a series of error operations, where the
operation or operations in which an error may occur are placed within this
tool

B) Error impact command catch: where this command is linked to the


previous command try where

With this command, a specific phrase or special operation will be used to


indicate when an error occurs

Inside the part in which an error occurred

3- Return to the main state command return

Where this command is used in a case where the program is free of Errors
and we want to return to the main state of the program or the execution or
the result of the execution of this program is not in the correct form or due to
incomplete input operations

4- Exception statements These statements are called:

A (Single exception statement else: It is a phrase that applies a specific and


unique processing operation

On cases that do not apply to the specific condition placed inside the if
condition statement

B (Comprehensive exception statement otherwise: Here the program poisons


by processing cases that apply

With several conditions that are inside several if condition statements that
do not match the conditions that

It deals with special cases and is usually placed inside the switch multi-
condition function

Because it contains more than one case that deals with a conditional
operation that deals with special cases
C) Else if: This is a conditional statement used when there is a specific
operation that does not apply to you or is not completely synonymous with
the previous conditions, which requires writing a new specific condition. All of
these conditions must end with an e-termination statement.

1- If statement:

This statement is used to compare two objects according to a specific


condition. There are a set of ways to implement the “if condition” as follows:

The first method:

If logical expression

Statement 1

Else

Statement 2

End

Logical expression: Condition of the if conditional statement

Statement: A statement in MATLAB

If the condition is true, then the statement statement 1 is executed, and


otherwise the statement statement 2 is executed

Example 1:

**The a must be specified first to run the program (for example a=2)

If (a ==4)

B=5;

Else

B=8;

End

Here if a=4 then B=5 and vice versa then B=8

Example 2: * To execute // 1. We write the program text in m-file

Command window ًin a, b ًwe did not specify.2


3. We click on Run *

If (a>b)

Disp (a)

Else

Disp(‘ a is not greater than b’)

End

Second method:

Nested if

If logical expression1

Statement 1

Else if logical expression 2

Statement 2

Else if expression 3

Statement 3

Else

Statement 4

End

End

 The number of times it was repeated end

If the first condition is true, then the first statement will be executed, and if
the first condition is false, then the second condition will be executed, and if
the second condition is false, then the third condition will be executed, and if
the third statement is false, then the statement after else will be executed,
and so on.

Example 3:

If a==1

B=1

Elseif a==2
B=4

Elseif a==3

B=9

Else

B=25

End

End

Example 4: Write a program that checks the value of the variables a and b. If
they are greater than zero, find their product and vice versa. Print that the
value is not greater than zero.

Solution:

Clear; clc

A=input(‘a=’) ; b=input(‘b=’);

If (a>0 && b>0)

Disp (‘ a and b values greater than zero ‘);

C=a * b

Else

Disp (‘ a and b values not greater than zero ‘);

End

2- For loop assignment:

To execute a statement in MATLAB more than once, we use for loop


assignment and the general formula is:

For variable =initial value :increment :final value

Statements

End

Variable: A variable in MATLAB


Initial value: The first value that the variable will start with

Increment: The increment range that is added to the first value

Final value: The final value that the variable reaches

The loop statement takes the initial value and puts it in the variable, then
executes the statements that follow the loop assignment until reaching the
end statement

It adds the Increment range to the variable to obtain a new value And if the
variable is less than or equal to the final value, if the answer is yes, it goes to
execute from above all the sentences that follow the iteration, but if the
answer is no, it exits the iteration.

Example 1:

For i=1:5

Disp ( I )

End

When executed, the result will be as follows: 1

Example 2: Print the numbers that are named 3 for the number from 3 to 16.
For i= 3:3 :16 disp( I )

End

When executed, the result will be as follows:

12

15
Example 3: Print the numbers descending from 22 to 17?

For i=20 : -1 : 17

Disp( I )

End

When executed, the result will be as follows:

20

19

18

17

Example 4: Print the numbers that complete the named 3 (from 16 to 3)?

For i= 16 : -1 : 3

If rem(I , 3)==0

Disp( i)

End

End

15

12

You might also like