Looping

Looping – Unit 1: Introduction To Java (Computer Science 30S)

If you don’t feel like reading this web page, here are two alternatives:

  • Read this presentation of the same content (slides 140 – 163)
  • Watch this playlist of videos of the same content on YouTube

Content Sections:

  1. Java Looping Statements
  2. The while Loop…
  3. The do while Loop…
  4. The for Loop…
  5. Starting an Assignment
  6. Some NetBeans Tips for Working Easier
  7. Completing the Assignment
  8. Handing In an Assignment
  9. How Assignments are Assessed

Java Looping Statements

  • Remember that looping statements are used to cause repetition in your program
  • Loops allow the program to execute statements over and over
  • Loops are always controlled by a loop control variable which has an initial and final value
  • Looping statements come in two basic types
  • Event controlled loops are used when you don’t know when to stop a loop (an event – e.g. the end of a file) will trigger the end of the loop
  • Count controlled loops are used when you need to execute the loop a specific number of times (e.g. x times)
  • Event controlled loops come in two forms:
  1. When you want the code body executed at least once and then tested
  2. When you want a test first before entering the loop
  • Loops are always tested with a Boolean expression
  • In mathematical logic, we can also combine logical expressions
  • We can combine logical expressions using ANDOR, and NOT to form more complex expressions:

Capture(17)

  • Using these terms with Boolean variables A and B:

Capture2(8)


The while Loop…

  • The structure of a while loop in Java is virtually identical to the way it is structured in C#
  • While loops are used for when you don’t know in advance how many times to execute the loop
  • This is how it’s done…

Capture3(4)

  • Take out a piece of scrap paper, write down what you feel the output will be from the next piece of code…

Capture4(4)

  • While loops are event controlled
  • There can be many events that can control a while loop
  • One of the most common is a “End of File” event (EOF)
  • Other events such as something being found in a search will end a while loop
  • For example…

Capture5(3)


The do while Loop…

  • This variation of the while loop is used in those circumstances when you want the body of the loop executed at least once before the test is executed
  • It includes the do keyword
  • For example…

Capture6(3)

  • Watch out for infinite loops…

Capture7(3)

  • The lack of {}  do not define the code scope, so only the first statement executes
  • This means an infinite loop

The for Loop…

  • Unlike while loops, which are non deterministic loops, for loops are used when it is known exactly how many times to execute a loop
  • The for loop also has the identical structure to the way it is written in C#
  • This is how it’s done…

Capture8(2)

  • For example…

Capture9(2)

  • Notice that you can declare the looping variable right inside the loop
  • Produces this output…

Capture10(1)


Starting an Assignment

  • Assessments are designed to give choice to you as a programmer and target the learning outcomes of the unit
  • You will solve the problem and demonstrate your learning
  • The first step is to read the assignments and decide which problem you are choosing to solve – this can be done by reading the list of possible assessment problems (on the website)

Picture1(14) 

  • For this demonstration, a different problem (actually from my grade 10 Computer Science course) will be solved
  • Now you should read the assignment, look for clues or any help I might provide in the description, and ask Mr. Wachs for any clarification if you are not sure what the problem is asking

Picture2(4)

  • Now we will start coding, but first some NetBeans tips to make your job easier

Some NetBeans Tips for Working Easier

  • NetBeans has a lot of tools to help with coding, some of which are obvious, other which are a little more hidden, here are some useful tips:
  1. Holding the Alt Key and using the mouse scroll will zoom in and out on code, making it easier to read (this can also be done before “printing” code for hand in to better fit into the PDF)
  2. Double clicking the top of the tab for the code will show or hide other windows making it easier to have a large code window open
  3. These two buttons located just below the tab can comment code in or out if the cursor is on a line, or if line(s) of code are selected

Picture3(3)

  1. At any point if you want Intellisense (auto-complete) to appear or reappear, just press CTRL + SPACE
  2. The key word “null” can often be used in code with Java to act as a “placeholder” for code you don’t actually want to fill in (or don’t know how to fill in) like in output dialog boxes
  3. Some code shortcuts can autocomplete code, the full list of all of  these can be found by clicking: Help – Keyboard Shortcuts Card (this card could be printed and kept by your computer as a reference)

Picture4(2) 

  1. One simple example is typing “sout” and then pressing tab to autocomplete the line:

Picture5(2)

  1. When an application is running and you want to stop it immediately, press the small X in the bottom of the NetBeans screen

Picture6(2)

  1. Pressing the keys ALT + SHIFT + F will autoformat (clean up) all your code (if nothing is selected) or the selected code (good idea to do before hand in) – Note: these format styles can also be altered in NetBeans, just ask Mr. Wachs if this is something you want to do
  2. Altering NetBeans templates will make your job easier especially for commenting code for future projects, to do this go to Tools – Templates, scroll down to “Java” and click the plus “+” sign to expand, then select “Java Main Class” and then “Open in Editor“, and then modify the template and save like this:

Picture7


Completing the Assignment

  • Now start NetBeans and create a new project for this solution
  • Select File – New Project
  • Select “Java with Ant”, “Java Application”, and Next
  • Name your project, and put it in a location that you can find again if you need to, then select Finish
  • I would also recommend you have your example open from the unit to use as a reference and even copy and paste code from

Picture8(1)

  • I will start with some easy code to welcome the user to the program, a line to act as a placeholder for the solution, and then a line to thank the user for using the program

Picture9

  • After running this to make sure this works, now I read through the problem
  • remove the placeholder code, create the variables I need, get user inputsconvert data types as needed, and provide the user outputs

Picture10


Handing In an Assignment

  • After running the application, testing it with many different inputs, completing the comments, and cleaning up code (use ALT + SHIFT + F to do this quickly) it is time to prepare it for hand in
  • As seen in a previous presentation, the process is:
  1. Turn code into a PDF file to maintain the look (styles, fonts, colors, etc.)
  2. Take screen captures of the running program (or a screen video for final projects)
  3. Name the files appropriately
  4. Hand in both files (they can be combined or left as separate files)
  • And now the file(s) will be handed in on Mr. Wachs’ website (or through InformNet for online students)
  • Once you have the code “printed” with screen capturesname the file properly into a proper PDF for hand in

Picture11

  • On the proper hand in pageupload the file(s), you can see instructions on the site, or email (or talk to) Mr. Wachs if you are confused

Picture12


How Assignments are Assessed

  • After your assignment has been assessed, you will receive a grade
  • You can look at feedback on your assignment after Mr. Wachs hands It back to you and your work will be annotated (have comments on your code) and a marking sheet explaining your grade
  • You can always resubmit assignments as many times as you wish to get the grade you want (of course this will depend on time and moving forward in the course)
  • I will now go over some of the basics of the marking sheet, but you can always read the sheet in more detail, or contact Mr. Wachs if you have any questions or feel he has made a mistake in grading your work (I actually encourage students to challenge my grading – just come with a good argument, and you will most likely win!)
  • This will be the mark feedback sheet (a PDF document) you should receive along with your PDF(s) you handed in
  • There will be sections with your name, the assignment you completed, your scores in various areas, the total score, and all the totals for the entire unit

Picture13(1)

  • The second page of this marking sheet goes over the specific learning outcomes and the details of how each mark was assessed

Picture14

  • I would recommend copying and pasting the code from this example into your other large example to keep a copy

The NEXT thing to work on is the “Looping” example page