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

Lec 7

Uploaded by

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

Lec 7

Uploaded by

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

**The switch Construct

The switch construct is another form of branching construct. It permits


an engineer to select a particular code block to execute based on the
value of a numerical, character, or logical expression. The general form
of a switch construct is:

If the value of switch_expr is equal to case_expr_1, then the first code


block will be executed, and the program will jump to the first statement
following the end of the switch construct. Similarly, if the value of
switch_expr is equal to case_expr_2, then the second code block will
be executed, and the program will jump to the first statement following
the end of the switch construct. The same idea applie for any other cases
in the construct. The otherwise code block is optional. If it is present,
it will be executed whenever the value of switch_expr is outside the
range of all of the case selectors. If it is not present and the value of
switch_expr is outside the range of all of the case selectors, then none
of the code blocks will be executed.
EX:-
N = input('Enter a number of your choice: ');
switch N
case {-2,-5,-8}
disp('negative one selected')
case 0
disp('zero selected')
case {2,5,7}
disp('positive one selected')
otherwise
disp('Some other value')
end

* The while Loop


A while loop is a block of statements that are repeated indefinitely as
long as some condition is satisfied. The general form of a while loop is

The controlling expression produces a logical value. If the expression


is true, the code block will be executed, and then control will return to
the while statement. If the expression is still true, the statements will
be executed again.
This process will be repeated until the expression becomes false. When
control returns to the while statement and the expression is false, the
program will execute the first statement after the end.

EX:

Sol: This program must be able to read in an arbitrary number of


measurements and then calculate the mean and standard deviation of
those measurements. We will use a while loop to accumulate the input
measurements before performing the calculations.
n = 0; sum_x = 0; sum_x2 = 0;
% Read in first value
x = input('Enter first value: ');
% While Loop to read input values.
while x >= 0
% Accumulate sums.
n = n + 1;
sum_x = sum_x + x;
sum_x2 = sum_x2 + x^2;
% Read in next value
x = input('Enter next value: ');
end
% Calculate the mean and standard deviation
x_bar = sum_x / n;
std_dev = sqrt( (n * sum_x2 - sum_x^2) / (n * (n-1)) );
% Tell user.
fprintf('The mean of this data set is: %f\n', x_bar);
fprintf('The standard deviation is: %f\n', std_dev);
fprintf('The number of data points is: %f\n', n);

If we enter either no numbers or only one number, then we will be


dividing by zero in the preceding equations! The division-by-zero error
will cause divide-by-zero warnings to be printed, and the output values
will be NaN. We need to modify the program to detect this problem,
tell the user what the problem is, and stop gracefully.
l

You might also like