DOC PREVIEW
UNC-Chapel Hill COMP 14 - LECTURE NOTES

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

COMP 14 Introduction to ProgrammingWriting Selection Statements What Type of Construct To Use?Writing Selection StatementsExampleSlide 5QuestionsQuestionAnswerTodayWhy Is Repetition Needed?Repetition StatementsLoopsTypical Uses of LoopsThe while Loop SyntaxThe while LoopThe while Loop ExampleThe while Loop What's Going On?Slide 18Slide 19Slide 20Slide 21Counter-Controlled while LoopSlide 23Reading From a FileSentinel-Controlled while LoopSlide 26Average.java ExampleInput Validation while LoopSlide 29Flag-Controlled while LoopEOF-Controlled while LoopSlide 32To doThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian IlieCOMP 14Introduction to ProgrammingAdrian IlieJuly 5, 2005The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie2Writing Selection StatementsWhat Type of Construct To Use?•Only need to execute additional statements if condition is true•Need to execute separate additional statements based on if the condition is true or false •Need to execute different statements based on multiple conditions•Need to execute different statements based on an expression that evaluates to a char or intifif-elsenested if-elseswitchThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie3Writing Selection Statements•Write the outline of the selection statement♦keywords♦curly braces♦case and break statements (for switch)•Write the expression♦boolean expression, or condition♦expression or variable evaluating to char or int•Write statements that execute based on the conditionThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie4ExampleWrite a selection statement that sets grade to 'F' if score is less than 60 and sets grade to 'P' if score is greater than or equal to 60.1) Choose constructif-else2) Write outlineif ( ){}else{}The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie5ExampleWrite a selection statement that sets grade to 'F' if score is less than 60 and sets grade to 'P' if score is greater than or equal to 60.3) Add conditionif (score < 60){}else{}4) Add statementsif (score < 60){grade = 'F';}else{grade = 'P';}The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie6QuestionsWhat type of selection statement should be used?1. Print "Male" if gender is 'M' and "Female" if gender is 'F'.2. Print the state associated with areaCode, where areaCode is an int.3. Print the maximum of three integers.if-elseswitchnested if-elseThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie7QuestionAssume that the lengths of 3 sides of a triangle are given in the integer variables side1, side2, and side3. Write code that will print:♦"Equilateral" if all three sides are equal♦"Isosceles" if only two sides are equal♦"Scalene" if no sides are equalequilateralisoscelesscaleneFirst, what selection construct should be used?nested if-elseThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie8Answer// side1, side2, and side3 are lengths// of the sides of a triangleif (side1 != side2 && side2 != side3 && side1 != side3)// no sides are equalSystem.out.println ("Scalene");else if (side1 == side2 && side2 == side3)// all sides are equalSystem.out.println ("Equilateral");else// two sides are equalSystem.out.println ("Isosceles");The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie9Today •Repetition statements•while loops•for loopsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie10Why Is Repetition Needed?•Want to add 5 integers to find their average?♦declare a variable for each integer♦initialize the sum♦read in the user input one at a time♦sum the numbers♦take the average•Want to add 1000 integers to find their average?repeat steps 3 and 4 1000 times1. declare one variable for input2. initialize the sum3. read in one line of user input4. add to the sum5. take the averageThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie11Repetition Statements•Allow us to execute a statement multiple times•Often referred to as loops•Controlled by boolean expressions♦like selection, or conditional, statements•Java has three kinds of repetition statements:♦the while loop♦the for loop♦the do loopThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie12Loops•Must use a loop control variable♦controls how many times to loop•4 Parts to Every Loop♦initialization - set loop control variable before condition♦condition - when to stop♦update - change the loop control variable♦body - actions to repeatThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie13Typical Uses of Loops•Repeat a section of code a specified number of times - counter-controlled•Repeat a section of code (reading input) until a specific value is read - sentinel-controlled•Repeat a section of code (reading input) until a valid value is entered - input validation•Repeat a section of code (reading from a file) until the end of the file is reached - EOF-controlled•Repeat a section of code until a boolean variable becomes false - flag-controlledThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie14The while LoopSyntaxwhile ( condition ){ loop body;}while is areserved wordIf the condition is true, the loop body is executed.Then the condition is evaluated again.The loop body is executed repeatedly untilthe condition becomes false.The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie15The while Loop•Syntaxwhile (expression)statement•Expression is always true in an infinite loop•Statements must change value of expression to falseThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie16The while LoopExamplefinal int LIMIT = 3;int count = 0;while (count < LIMIT){System.out.println (count);count++;}System.out.println (“All done!”);booleanconditionloopbodyupdateinitializationOutput:012All done!The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie17The while LoopWhat's Going On?final int LIMIT = 3;int count = 0;while (count < LIMIT){System.out.println (count);count++;}System.out.println (“All done!”); LIMIT countOutput:301012All done!23The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie18The while Loopfinal int LIMIT = 3;int count = 0;while (count < LIMIT) {count++;System.out.println (count);}System.out.println (“All done!”);Output:123All done!The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie19The while Loopfinal int LIMIT = 3;int count = 0;while (count < LIMIT) {System.out.println (count);}System.out.println (“All done!”);Output:0000000000...The UNIVERSITY of


View Full Document

UNC-Chapel Hill COMP 14 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?