SUNY Geneseo Department of Mathematics
Monday, March 21
Math 230 02
Spring 2016
Prof. Doug Baldwin
while expression
statements
end
expression
is true, execute statements
, continue until
expression
becomes falsei = …
while i < …
…
i = i + 1
end
input
and fprintf
statements?fprintf
gives programmer fairly tight control over format of displayed
informationinput
reads numbers (for now) from user, evaluating expressions if it
has to>> fprintf( 'Hello World' );
Hello World>> fprintf( 'Hello World\n' );
Hello World
>> fprintf( 'Hello\nWorld\n\n' );
Hello
World
>> x = 17.6;
>> fprintf( 'x is %f\n', x );
x is 17.600000
>> fprintf( 'x is %d\n', x ); % I expected this to print as a base-10 integer
x is 1.760000e+01
>> y = 3;
>> fprintf( 'y is %d\n', y );
y is 3
>> fprintf( 'y is %f\n', y );
y is 3.000000
>> fprintf( 'x is %5.2f\n', x );
x is 17.60
>> fprintf( 'x is %5.2f and y is %d\n', x, y );
x is 17.60 and y is 3
>> x = input( 'Enter a number ' );
Enter a number 4
>> x
x =
4
>> x = input( 'Enter a number ' );
>> Enter a number sin( pi + 17 )
x
x =
0.9614
>> fprintf( 'x = %5.2f\n', x );
x = 0.96
>> x = 0.966;
>> fprintf( 'x = %5.2f\n', x );
x = 0.97
function totalSum = addPositive()
totalSum = 0;
usrNum = input('Enter positive number (<0 to quit): ');
while (usrNum >= 0)
totalSum = totalSum + usrNum
usrNum = input('Enter positive number (<0 to quit): ');
end
end
function [ age ] = getAge()
max_possible_age = 120;
age = input( 'Please enter your age ' );
% while age is invalid
% input new age
while ~(age >= 0 & age <= max_possible_age)
age = input( 'Please enter your age ' );
end
end