Strucutred Programming
Strucutred Programming
Practical
Structured Programming
Structured programming
Decisions
The if Structure: This structure allows us to execute a set of statements if a logical
condition is true. Its general syntax is
if condition
statements
end where, condition is a logical expression that is
either true or false.
The while Structure
A while loop repeats as long as a logical condition is true. Its general syntax is
while condition,
statements,
end
The statements between the while and the end are repeated as long as the condition
is true, Example:
The while… break Structure
Although the while structure is extremely useful, the fact that it always exists
at the beginning of the structure on a false result is somewhat constraining.
The syntax of this version, called a while … break structure, can be written as
while (1)
statements
if condition,
break,
end
statements,
end
Decisions
The if Structure: This structure allows us to execute a set of statements if a logical
condition is true. Its general syntax is
if condition
statements
end where, condition is a logical expression that is
either true or false.
The if Structure
Relational operators
3. Condition may be vector or matrix, in which case it is true only if all its
elements are non-zero. A single zero element in a vector or matrix renders it
false.
Example of logical expression
In MATLAB, the double equal sign (==) is used when testing for equality, e.g.,
if x == 0
disp(‘x equals zero’ ),
end
The if Structure
Examples:
if condition,
statements,
else
statements,
end
The if … else Structure
Example:
The if … elseif Structure
It often happens that the false option of an if … else structure is another
decision. This type of structure often occurs when we have more than two
options for a particular problem setting. For such cases, a special form of
decision structure, the if … elseif has been developed. It has the
general syntax:
if condition,
statements,
elseif condition,
statements,
.
.
.
else
statements,
end
The if … elseif Structure
Example:
The if … elseif Structure
Example 1:
Suppose the Nepal Bank offers a scheme for fixed depositors 8.5 percent
interest on balances less than Rs. 10000, 10.5 percent for balances of Rs.
10000 or more but less than Rs. 100000, and 13 percent for balances of Rs.
100000 or more. Check your new balance after one year according to these
scheme.
The if … elseif Structure
Example 2:
Calculate the roots of the quadratic equation ax2 + bx + c = 0.
The switch Structure
The switch structure is similar in spirit to the if … elseif structure. However,
rather than testing individual conditions, the branching is based on the
value of a single test expression. Depending on its value, different blocks of
code are implemented. It has general syntax:
switch testexpression,
case value,
statements,
case value,
statements,
.
.
.
otherwise
statements,
end
The switch Structure
Example: Displaying message
depending on the value of string
variable, grade of students.