L4_control_stud - CGN 3421 - Computer Methods Gurley...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CGN 3421 - Computer Methods Gurley Lecture 4 - Control Structures page 18 of 26 Lecture 4 - Control Structures Three ways to control the flow of a program if - end decision (no loop) if (condition1 true) needed statements elseif (condition2 true) optional statements else optional statemetns end needed for - end counted loop (repeat statements, no decision) for variable = start : increment : stop statements inside counted loop end while - end conditional loop (repeat statements and make a decision) while (condition true) statements inside conditional loop end
Background image of page 1

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

View Full DocumentRight Arrow Icon
CGN 3421 - Computer Methods Gurley Lecture 4 - Control Structures page 19 of 26 if - end decision (no loop) >> if ( condition 1 ) >> execute statement(s) >> elseif ( condition 2 ) >> execute statement(s) >> elseif ( condition 3 ) >> execute statement(s) >> else >> execute statement(s) >> end >> . .. rest of the program decision structure will find only one condition true when one condition is found true, all other conditions are ignored condition 1 condition 2 condition 3 anything else
Background image of page 2
CGN 3421 - Computer Methods Gurley Lecture 4 - Control Structures page 20 of 26 Example #1: grades >> grade = input(‘enter student grade ’) >> if (grade >= 90) >> letter = ‘A’ >> elseif (grade >= 80) >> letter = ‘B’ >> elseif (grade >= 70) >> letter = ‘C’ >> else >> letter = ‘D or F’ >> end decision structure will execute only one letter assignment when one condition is found true, all other conditions are ignored grade >= 90 grades < 70 80 > grade >= 70 90 > grade >= 80
Background image of page 3

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

View Full DocumentRight Arrow Icon
CGN 3421 - Computer Methods Gurley Lecture 4 - Control Structures page 21 of 26 example #2: grades (logic error) >> grade = input(‘enter student grade ’) >> if (grade >= 70)
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

L4_control_stud - CGN 3421 - Computer Methods Gurley...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online