Assignment Definition And General Feedback By Michael Panitz at Cascadia Community College (

Similar documents
Assignment Definition And General Feedback By Michael Panitz at Cascadia Community College (

Title: Counting While Loops

Title: Logical operators: NOT!

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

The for Loop. Lesson 11

Session 4 Starting the Air Raid Game

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Mr. Monroe s Guide to Mastering Java Syntax

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CP122 CS I. Iteration

CS 177 Recitation. Week 1 Intro to Java

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

AP Computer Science A Summer Assignment 2017

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

TESTING AND DEBUGGING

Activity 7: Arrays. Content Learning Objectives. Process Skill Goals

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Intro. Scheme Basics. scm> 5 5. scm>

A Bad Idea Monday, April 16, :23 AM

CS1150 Principles of Computer Science Loops (Part II)

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

static String usersname; public static int numberofplayers; private static double velocity, time;

Warmup : Name that tune!

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

BBM 102 Introduction to Programming II Spring Exceptions

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Initial Coding Guidelines

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Valuable points from Lesson 6 Adobe Flash CS5 Professional Classroom in a Book

Loops. In Example 1, we have a Person class, that counts the number of Person objects constructed.

St. Edmund Preparatory High School Brooklyn, NY

Fundamentals of Programming Session 4

CS112 Lecture: Repetition Statements

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Following is the general form of a typical decision making structure found in most of the programming languages:

COMPUTER PROGRAMMING LOOPS

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

Math Modeling in Java: An S-I Compartment Model

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

3 The L oop Control Structure

Controls Structure for Repetition

Formatting & Style Examples

Dane County Treasurer s Login Help

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Session 8 Finishing Touches to the Air Raid Game

1.3 Conditionals and Loops

BEGINNER PHP Table of Contents

CS 106 Introduction to Computer Science I

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Repetition with for loops

Lesson 3: Accepting User Input and Using Different Methods for Output

Scope of this lecture. Repetition For loops While loops

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Text Input and Conditionals

MITOCW watch?v=0jljzrnhwoi

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

CSE143 Notes for Monday, 4/25/11

Session 1 Welcome to Greenfoot & Code Breaker Authored by Brian Cullen

Activity 3: Data Types

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 03. REQUIREMENTS CHANGE. I Love You, You re Perfect... Now Change!

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

CS112 Lecture: Loops

Repetition, Looping. While Loop

Visual Studio.NET. Although it is possible to program.net using only the command OVERVIEW OF VISUAL STUDIO.NET

Technical Questions. Q 1) What are the key features in C programming language?

Lab # 2. For today s lab:

Programming in ROBOTC ROBOTC Rules

COMP-202 Unit 9: Exceptions

Introduction to the Java Basics: Control Flow Statements

Lecture 5: Implementing Lists, Version 1

Loops. CSE 114, Computer Science 1 Stony Brook University

Example Program. public class ComputeArea {

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

What does this program print?

Oct Decision Structures cont d

printf( Please enter another number: ); scanf( %d, &num2);

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives:

First Java Program - Output to the Screen

How to make a "hello world" program in Java with Eclipse *

OrbBasic Lesson 1 Goto and Variables: Student Guide

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

CS 106 Introduction to Computer Science I

Slide 1 CS 170 Java Programming 1 Testing Karel

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Full file at

Transcription:

For Loops: Will Tanna Assignment Definition And General Feedback By Michael Panitz at Cascadia Community College (http://www.cascadia.edu) Table of contents: Summary When To Use and Avoid This Example of Usage Example Trace Syntax Explanation Help With The Logic Important Details Common Mistakes (And How To Fix Them) Summary: There are many variations of loops that you can do in Java. One of those variations is the for loop. A for loop is used when you know how many times you want to use a certain command. Unlike the while command where it keeps going unless a command comes out true or false. The for loop command bypasses the asking question portion of the loop and goes right to the commands given. It uses a counter command to count how many times the main command has been used. When to Use and Avoid this Method: There are many different variations of loops in Java. How do you know which one to use? You should ask your self what kind of program you are working with or creating. If you know the exact integer a certain command is, then the for loop will work out best for you. The great thing about the for loop is that instead of relying on questions being asked for the robot you are telling it a direct command that it needs to follow a certain amount of times However, there are some scenarios where using the for loop is bad. When you have walls or Things that are randomly generating in different locations. You won t know the exact integer value because it is always changing. Another reason why not to use the for loop would be for if the robot needs to be asked a question. If a Robot needs to move down a hallway then turn around, the while loop can make a command for just that situation. You can program the robot to walk down the hallway then ask itself if it can keep moving forward or turn around so it does not crash. If you were to do a for loop command and the integer was too high the Robot could hit a wall down the hallway and break.

When to Use: When To Avoid: Example Of Usage: Now we are going to learn how to write this loop in Java. Lets say for example you have a robot that just got pushed and when getting pushed all his Things came out of his backpack. Now he needs to move forward and pick those Things up to put them back in his backpack. One way to type this is (in this program the robot is named Brian): brian.move(); brian.pickthing(); brian.move(); brian.pickthing(); brian.move();

brian.pickthing(); brian.move(); brian.pickthing(); Instead of typing all of that, you can save yourself a lot of time by using the for loop command. The for loop command can create a loop where it will repeat the commands brian.move(); and brian.pickthing(); a however many times you want or need. This is how you create the for loop command. You first start for the for loop command be typing for ( Second, you need to create an integer for the loop. You do this by typing int right after the ( we just put in. After int we want to put a space and type counter = 0;. What we have so far is: for (int counter = 0; What this means is we have an integer that is starting at 0. The counter can be named anything. But for this program we will leave it as counter. There needs to be a ; after 0 that way Java knows that is when that command ends. A After the semicolon we must add the integer the counter will count up too, or equal too. What this will look like is:. This shows that the counter will be true if the counter is less then 4. When Brian moves 4 times he is no longer less then the counter, which means he will stop. Once the counter integer is done, we need a command that will actually count the counter. We do this by typing:. What this command means is that for every time the statements are done it will count 1 towards the counter. You can put any number where the 1 is, but if you are trying to count just one statement at a time, there really is no need to go above 1. You must end the command with a parenthesis so Java knows that s when the integer counter is done, and you can go onto the statements. Just to clarify, the statements are the lines of code that Brian will take to move and pick the Things up. Once you get all of the code lined up and set. This is an example of what it would look like. For the sake of the example we will bold the counter portion of the code. Line Code 1 import becker.robots.*; 2 public class For_Loop_Example extends Object 3 { 4 public static void main(string[] args 5 { 6 City tokyo = new City(); 7 NewRobotType brian = new NewRobotType(tokyo, 1, 0, Direction.EAST, 0); 8 new Thing(tokyo, 1, 1); 9 new Thing(tokyo, 1, 2); 10 new Thing(tokyo, 1, 3); 11 new Thing(tokyo, 1, 4);

12 for (int counter = 0; 13 { 14 brian.move(); 15 brian.pickthing(); 16 } 17 } 18 This is what we will get with the code we just typed: Example Trace: Now that the code is all typed up and ready to run. We will now trace the program to show what the counter command is doing to Brian and the four Things. What this command will do is move Brian four times while picking up each thing. Once he has done that, he will stop. Line # Program Statement Brian St # Brian Ave # Brian Direc Brian Back pack Thing location 1 Thing location 2 Thing location 3 Thing location 4 Wall Counter True/Fals e 4 public static void - - - - - - - - - - - main(string[] args) 6 City tokyo = new - - - - - - - - - - -

City(); 7 NewRobotType 1 0 E 0 - - - - - - - brian = new NewRobotType(to kyo, 1, 0, Direction.EAST, 0); 8 new Thing(tokyo, 1 0 E 0 (1,1) - - - - - - 1, 1); 9 new Thing(tokyo, 1 0 E 0 (1,1) (1,2) - - - - - 1, 2); 10 new Thing(tokyo, 1 0 E 0 (1,1) (1,2) (1,3) - - - - 1, 3); 11 new Thing(tokyo, 1 0 E 0 (1,1) (1,2) (1,3) (1,4) - - - 1, 4); 12 for (int counter =0; 1 0 E 0 (1,1) (1,2) (1,3) (1,4) - 0 True 14 brian.move(); 1 1 E 0 (1,1) (1,2) (1,3) (1,4) - 0-15 brian.pickthing(); 1 1 E 1 - (1,2) (1,3) (1,4) - 0-12 counter = 1 1 E 1 - (1,2) (1,3) (1,4) - 1 - counter +1 12 for (int counter =0; 1 1 E 1 - (1,2) (1,3) (1,4) - 1 True 14 brian.move(); 1 2 E 1 - (1,2) (1,3) (1,4) - 1-15 brian.pickthing(); 1 2 E 2 - - (1,3) (1,4) - 1-12 counter = 1 2 E 2 - - (1,3) (1,4) - 2 - counter +1 12 for (int counter =0; 1 2 E 2 - - (1,3) (1,4) - 2 True 14 brian.move(); 1 3 E 2 - - (1,3) (1,4) - 2-15 brian.pickthing(); 1 3 E 3 - - - (1,4) - 2-15 counter = counter +1 12 for (int counter =0; 1 3 E 3 - - - (1,4) - 3-1 3 E 3 - - - (1,4) - 3 True

14 brian.move(); 1 4 E 3 - - - (1,4) - 3-15 brian.pickthing(); 1 4 E 4 - - - - - 3-12 counter = counter +1 12 for (int counter =0; 1 4 E 4 - - - - - 4-1 4 E 4 - - - - - 4 False As you can see, line 4-11 is the code to set up the city and the main details of the program. After that the program runs the commands of the robot. Line 12 is the start of the counter. It is set to repeat the command 4 times. The statement for line 12 will continue to be true until the counter hits 4. When it hits 4 it is no longer less then 4. Therefore the statement turns to False. Syntax Explanation: Let's start with the program as it's written here. Line # Program Source Code 1. import becker.robots.*; 2. public class For_Loop_Example extends Object 3. { 4. public static void main(string[] args) 5. { 6. City tokyo = new City(); 7. Robot brian = new Robot(tokyo, 1, 0, Direction.EAST, 0); 8. new Thing(tokyo, 1, 1); 9. new Thing(tokyo, 1, 2); 10. new Thing(tokyo, 1, 3); 11. new Thing(tokyo, 1, 4); 12. 13. brian.move(); 14. brian.pickthing(); 15. brian.move(); 16. brian.pickthing(); 17. brian.move(); 18. brian.pickthing(); 19. brian.move(); 20. brian.pickthing(); 21. } 22. }

23. Below, you can see the finished program with the differences highlighted in yellow, so it's easy to see what's been added/changed). Line # Program Source Code 1. import becker.robots.*; 2. public class For_Loop_Example extends Object 3. { 4. public static void main(string[] args) 5. { 6. City tokyo = new City(); 7. Robot brian = new Robot(tokyo, 1, 0, Direction.EAST, 0); 8. new Thing(tokyo, 1, 1); 9. new Thing(tokyo, 1, 1); 10. new Thing(tokyo, 1, 1); 11. new Thing(tokyo, 1, 1); 12. for (int counter =0; counter = counter 13. { 14. brian.move(); 15. brian.pickthing(); 16. } 17. } 18. } 19. The code we typed in yellow is the new method we learned. We went from a long line of code to just a few lines. By using a counter command you can immensely shorten your program, but there are some things you need to watch out for. Here is the code we wrote: 12 for (int counter = 0; 13 { 14 brian.move(); 15 brian.pickthing(); 16 } If you notice the for is in purple; same with the int. The reason why these are purple is because Java recognizes these commands. It tells Java that these words have

specific meaning and perform a certain action. The for is for the loop we are trying to do. The int is used to show that we do in fact have an integer in our statement. If you notice, the two purple words are lower case. This is very important because they must always be lower case. Along with the two purple words all the starting letter of every word needs to be lowercase. For example we used counter as what our counter would be called. It is lower case as you can see. If we wanted to name the counter, Brian moves and picks things up. We would write it as brianmovesandpicksthingsup. When you first look at the code it can be a little confusing on which counter does what in the program. The very first counter with the =0 is the counter to designate what number the counter is starting. In this case we are starting at 0. The next counter with the < 4 means that we are counting until we get to 4. The for loop will be true every time the number is less then 4. Once it hits 4 it comes back false and the loop still stop. The last 2 counters, +1 is the command for actually counting the for loop. Once on loop has been done, this bit of code will count 1 number towards the counter. Eventually adding up to the number that we designated in the second counter command. After the main for loop counter code has been put into place, we must put the statements in. It is very important that you put curly braces in, to state that the statements in between them are the ones the for loop will do. In between the curly braces is where we put our statements that we want to repeat. In this case it is brian.move(); and brian.pickthing();. Once we type these commands we must end the for loop with one more curly brace like on line 16. Once that last curly brace is put into place, we are done with the loop. This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 3.0 Unported License. Plagiarism: If you believe that some or all of this document infringes on your intellectual property (i.e., part or all of this document is copied from something you've written) please immediately contact Mike Panitz at Cascadia Community College (perhaps using the Faculty And Staff Directory at http://www.cascadia.edu/pages/searchtemplate.aspx