0% found this document useful (0 votes)
66 views

Conditional Statements-OCTAVE - Lab Class

Uploaded by

Ghadeer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Conditional Statements-OCTAVE - Lab Class

Uploaded by

Ghadeer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Conditional Statements

IT 6010-Maths for Computing


What are Conditonal Statements?

• Conditional statements allow you to


control the flow of your program
based on certain conditions.
• In Octave, conditional statements
include if, else, elseif, and switch,
which control the flow of execution
based on logical conditions.
1. if-end Statement
• if-end statement the simplest form of conditional
statement
• it is used to execute a block of code
conditionally
Syntax:
if(condition)
% code to be executed if condition is
true
end
 condition: A logical expression that returns either
true or false. If the condition evaluates to true,
the code inside the if block is executed. Otherwise,
the block is skipped.
1. if-end Statement
Example1: Check if 5 is positive and, if it is, display a
message indicating that it is positive.

x = %5assigns the value 5 to the variable x

if x > 0 % the condition x > 0 is checked


% since x is 5, the
condition is true, so the
disp('x is positive');
code is executed,
displaying ‘x is positive’
end
% marks the end of the if block, indicating
where the conditional statements stop

If the condition (x > 0) were false (e.g., if x was -3 or


0), the disp statement would not execute, and nothing would
be printed.
1. if-end Statement
Example2: Check if the temperature is greater than 30 and,
if so, display a message indicating that it is hot outside

T = 35 % assigns the value 35 to the variable T

% the condition T > 30 is checked


if T > 30
% since T is 35, the
disp('It is hot condition is true, so
outside.');
the code is executed,
displaying ‘It is hot
end outside.’

% marks the end of the if block, indicating


where the conditional statements stop
2. if-else-end Statement
The if-else-end statement is used to execute a
specific block of code based on whether a condition
is true or false.
Syntax:
if(condition)
% code to be executed if condition is
true
else
% code to be executed if condition
is false
end
 condition: A logical expression that returns either
true or false. The if block runs if the condition is
true; otherwise, the else block runs.
2. if-else-end Statement
Example1: Check if -2 is positive or negative
number = -2; % assigns the value -2 to the variable number
if number > 0 % the condition number > 0 is checked
disp('The number is positive.'); % Since number is -2,
condition is false,
else
else block runs,
disp('The number is negative.'); displaying ‘The
number is negative.’
end
% marks the end of the if-else block,
indicating where the conditional statements
stop
2. if-else-end Statement
Example2: Check if 7 is even or odd

number = 7; % assigns the value 7 to the variable number


% check if the remainder of
if mod(number, 2) == 0
number divided by 2 is 0
disp('The number is even.'); % Since number is 7,
condition is false, else
else
block runs, displaying
disp('The number is odd.'); ‘The number is odd.’

end
% marks the end of the if-else block,
indicating where the conditional statements
stop
3. if-elseif-else-end statement
The if-elseif-else-end statement is used to execute
different blocks of code based on multiple conditions.
Syntax:
if (condition1)
% Code to execute if condition1 is true
elseif (condition2)
% Code to execute if condition2 is true
else
% Code to execute if neither condition1
nor condition2 is true
end
3. if-elseif-else-end statement
Example1: Check if 0 is positive, negative or neither

x = 0; % assigns the value 0 to the variable x

if x > 0 % the condition1 x > 0 is checked


disp('x is positive');
elseif x < 0 % the condition2 x < 0 is checked
disp('x is negative');
% Since x is 0, conditions 1 and
2 are false, else block runs
else
disp('x is neither positive nor
negative');
end % marks the end of the if-elseif-else block,
indicating where the conditional statements stop
3. if-elseif-else-end statement
Example2: Check if -5 is positive, negative or neither

x = -5; % assigns the value -5 to the variable x

if x > 0 % the condition1 x > 0 is checked


disp('x is positive');
elseif x < 0 % the condition2 x < 0 is checked
disp('x is negative');
% Since x is -5, conditions 1 and
3 are false, elseif block runs
else
disp('x is neither positive nor
negative');
end % marks the end of the if-elseif-else block,
indicating where the conditional statements stop
4. Nested statement
You can place if statements inside other if or else
blocks. This is called nesting. Nested if statements are
useful when you need to check multiple related
conditions.
Syntax:
if (condition1)
if (condition2)
% Code to execute if both condition1 and
condition2 are true
end
end
4. Nested statement
Example1: Check if 10 and -5 are positive or negative
x = 10; % assigns the value 10 to the variable x
y = -5; % assigns the value 5 to the variable y
if x > 0 % the condition1 x > 0 is checked

if y > %0 the condition2 y > 0 is checked


% Since x is
disp('Both x and y are positive');
10, y is -5
else conditions 1
and 2 are
disp('x is positive, but y is not');
false, inner
end else block
else % marks the end of the inner loop runs

disp('x is not positive');


end
% marks the end of the outer loop, indicating where
the conditional statements stop
Logical Operators

Logical AND (&&)


Both conditions must be true for the if block to
execute.
Example:
x = 5;
y = 10;
if (x > 0 && y > 0)
disp('Both x and y are positive');
end
Logical Operators

Logical OR (||):
If either one of the conditions is true, the
if block will execute.
Example:
x = -3;
y = 5;
if (x > 0 || y > 0)
disp('At least one of x or y is
positive');
end
Logical Operators

Logical NOT (~):


The ~ operator inverts the condition. If the
condition is true, ~ will make it false, and
vice versa.
Example:
x = -5;
if ~(x > 0)
disp('x is not positive');
end
IMPORTANT NOTES:
• Proper indentation and consistent style make
your code more readable and maintainable. Practice
these constructs to become proficient in controlling
program flow in Octave.
• Avoid deeply nested if statements: They can be
hard to read and maintain. Use elseif or switch-case
when appropriate.
• Use logical operators wisely: Combining
multiple conditions with &&, ||, or ~ can reduce the
need for nested if statements and make code more
readable.
• Keep conditions simple: Complex conditions can
make the code harder to debug. Break conditions into
smaller parts if needed.

You might also like