input(提示信息,选项) Code 其中提示信息为一个字符串,用于提示用户输入什么样的数据,其中可以加多 个换行符n.例如 TExample A=input('输入A矩阵:\n') Example 执行该语句后,首先在屏幕上显示'输入A矩阵',然后等用户从键盘上输入该 矩阵.nput语句不加选项时表示输入数值,而当选项为's'时,则允许用户输 入一个字符串.例如 TExample nm=input('what''s your name?','s') Previous Next First Last Back Forward 3
input(J´&E,¿ë) ↓Code Ÿ•J´&EèòáiŒG, ^uJ´^r—\üoÍ‚, Ÿ•å±\ı áÜ1Œ\n. ~X ↑Example A=input(’—\A› :\n’) ↓Example â1TäÈ, ƒk3¶4˛w´’—\A› ’, ,^rlÖ˛—\T › . input äÈ ÿ\¿ëûL´—\Íä, ¿ëè’s’û, K#N^r— \òáiŒG. ~X ↑Example nm=input(’what’’s your name?’,’s’) Previous Next First Last Back Forward 3
Example disp输出语句disp语句用于向命令窗口显示指定信息.例如 TExample A='statistics'; disp(A) A=[1,2,3;2,4,5]; disp(A) Example pause暂停语句 语法 pause #程序暂停,等待用户按任何键以继续 TCode pause(n)#暂停n秒 pause on#使得下续的pausel或pause(n)执行 pause of1#使得下续的pause或pause(m)不执行 Previous Next First Last Back Forward 4
↓Example disp ——äÈ dispäÈ^uï·-Iùw´ç½&E. ~X ↑Example A=’statistics’; disp(A) A=[1,2,3;2,4,5]; disp(A) ↓Example pause 6 äÈ ä{ ↑Code pause #ßS6 , ñ^rU?¤Ö±UY pause(n) # 6 n¶ pause on # ¶eYpause½pause(n)â1 pause off #¶eYpause½pause(n)ÿâ1 Previous Next First Last Back Forward 4
Code 章条件控制一if,switch if,else,and elseif语句 语法结构 TCode if logical_expression statements elseif logical_expression statements else statements end Code 例如 Example Previous Next First Last Back Forward 5
↓Code ✿ ^áõõ— if, switch if, else, and elseif äÈ ä{( ↑Code if logical_expression statements elseif logical_expression statements else statements end ↓Code ~X ↑Example Previous Next First Last Back Forward 5
1frem(a,2)==0 disp('a is even') b=a/2; end Example 以及 fE以ampl if n <0 If n negative,display error message. disp('Input must be positive'); elseif rem(n,2)==0 If n positive and even,divide by 2. A=n/2; else A=(n+1)/2; If n positive and odd,increment and divide. end Example switch语句 Previous Next First Last Back Forward 6
if rem(a, 2) == 0 disp(’a is even’) b = a/2; end ↓Example ±9 ↑Example if n < 0 % If n negative, display error message. disp(’Input must be positive’); elseif rem(n,2) == 0 % If n positive and even, divide by 2. A = n/2; else A = (n+1)/2; % If n positive and odd, increment and divide. end ↓Example switch äÈ Previous Next First Last Back Forward 6
switch语句根据表达式值的不同执行不同的语句.语法结构 switch expression (scalar or string) case valuel statements Executes if expression is valuel case value2 statements X Executes if expression is value2 otherwise statements Executes if expression does not match any case end Code 例如 Example switch var Previous Next First Last Back Forward 7
switch äÈä‚Là™äÿ”â1ÿ”äÈ. ä{( ↑Code switch expression (scalar or string) case value1 statements % Executes if expression is value1 case value2 statements % Executes if expression is value2 . . . otherwise statements % Executes if expression does not % match any case end ↓Code ~X ↑Example switch var Previous Next First Last Back Forward 7