
University College Computer Applications Lectures
"Explore MATLAB lectures on flow control, loops, switch-case statements, and relational/logical operators at Al-Mustaqbal University College. Learn programming concepts with examples and practical exercises."
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
AL-Mustaqbal University College COMPUTER APPLICATIONS: MATLAB Mayas Aljibawi
Lecture Number 5 Flow Control - 2
While..end This loop is used when the number of passesis not specified. The looping continues until a stated condition is satisfied. The while loop has the form: while expression Block of statements end Example: a=1; while a<=10 z=a+1; a=a+1; end disp(z)
Write a program to print the Hello World based on the entered number x=input('enter the value\n'); while x~=0 disp('hello') x=x-1; end
Switch ....Case The switch statement executes certain statements based on the value of variable or expression. switch switch_expr case case_expr1 commands ... case {case_expr2,case_expr3} commands ... otherwise commands ... end
Example: x=input('enter the value\n'); switch (x) case 1 disp ('first') case 2 disp('second') case 3 disp('third') otherwise disp('not in the first places') end
Write a program to print Stop if the entered value is red , Go if the enter value is green otherwise print Prepare . x=input('enter the value\n , 's ); % input strings (like names) switch (x) case 'red' disp ('stop') case 'green' disp('go') otherwise disp('prepare') end
Relational and Logical Operators A relational operator compares two numbers by determining whether a comparison is true or false. Relational operators are shown in the Table.
and && . ) ( clc clear a=input('enter the value\n'); if( a > 15 && a < 5) ---------- disp('in range ) else disp('not in range') end
|| or ) ( clc clear a=input('enter the value\n'); if( a < 15 || a > 5) disp('in range') else disp('not in range') end ----------
Write a program to find the factorial of the entered number factorial 4 ( 1*2*3*4 ) . clc clear a=input('enter the value\n'); f=1; for i=1:a f=f*i; end disp(f)
Write a program to find the even and odd number based on entered number. -- 2 8 , , mod , mod(8,2) < clc clear clc clear a=input('enter the value\n ); X=mod(a,2); switch x case 0 disp('even') otherwise disp('odd') end a=input('enter the value\n'); switch (mod(a,2)) case 0 disp('even') otherwise disp('odd') end
IF clc clear a=input('enter the value\n'); x=mod(a,2); if(x==0) disp('even') else disp('odd') end clc clear a=input('enter the value\n'); if(mod(a,2)==0) disp('even') else disp('odd') end