KMU255Programming_4thClassExtraNotes_FlowchartsLoops

# KMU255Programming_4thClassExtraNotes_FlowchartsLoops - a =...

This preview shows pages 1–8. Sign up to view the full content.

KMU 255 Computer Programming Examples for loops and their flowcharts Hacettepe University Department of Chemical Engineering Fall Semester

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
FOR LOOP (DO LOOP) for m = 1 : 10 (calculations) m is a counter to track the number of passes through the calculations. a = 0 for m = 1:10; m a = a + m pause end pause command stops the program and waits for user to press a key. Useful to monitor the values of the variables.
Matlab codes: FOR LOOP EXAMPLE for i = 1 : 11 x(i) = (i-1) * (2*pi/10) ysin(i) = sin(x(i)) clear variables plot ysin vs. x clear for i = 1:11; x(i) = (i-1) * (2*pi/10); ysin(i) = sin(x(i)); end plot (x,ysin)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
WHILE LOOP (calculations) Condition true? No while [condition] (calculations) - a counter variable is not required. - must contain a logical condition to control the looping.
a = 0, m = 0 while a < 54 m = m+1 a = a+m pause print a m = 0;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: a = 0; while a < 54; m = m+1 a = a+m pause end a WHILE LOOP input y y > 10? Yes IF Statement Display message that y > 10 y = 10 Display y y = input (‘Enter a number less than or equal to 10:’); if y > 10 fprintf(‘The number you entered is greater than 10. It will be changed to 10\n’) y = 10; end y If, else, elseif input y y>10 or y<1 ? No Display “in range" Yes Display “out of range" y>10 ? No y<1 ? No Display y Yes Display message that y > 10 y = 10 Display message that y < 1 y = 1 y = input ( ‘Enter a number between 1 and 10:’); if y > 10 I y < 1 fprint f( ‘The number you entered outside the range. It will be changed. \n’) if y > 10; y = 10; fprintf ( ‘The number has been changed to 10. \n’); end if y < 1 y = 1; fprintf( ‘The number has been changed to 1. \n’); end else fprintf ( ‘The number is in the range. \n’) end y...
View Full Document

## This note was uploaded on 03/14/2012 for the course CHEMICAL E kmu 255 taught by Professor Onel,selis during the Fall '11 term at Hacettepe Üniversitesi.

### Page1 / 8

KMU255Programming_4thClassExtraNotes_FlowchartsLoops - a =...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online