Kilobolt
  • Home
  • Tutorials
    • Game Development Tutorial >
      • Unit 1: Beginning Java >
        • Before you begin...
        • Day 1: Setting Up
        • Day 2: Java Basics
        • Day 3: More Basics
        • Day 4: Java Math
        • Day 5: More Math
        • Day 6: If... else...
        • Day 7: More Control Flow
        • Day 8: Looping
        • Day 9: More on Looping
        • Day 10: Inheritance, Interface
        • Day 11: Threads and Graphics
      • Unit 2: Creating a Game I >
        • Day 1: Foundations
        • Day 2: Basic Framework
        • Day 3: Taking User Input
        • Day 4: Enter the Robot
        • Day 5: Background and Sprites
        • Day 6: Adding Enemies
        • Day 7: Shooting Bullets
        • Day 8: Animations
        • Day 9: 2D-Arrays
        • Day 10: Painting the Tilemap
      • Unit 3: Creating a Game II >
        • Day 1: Level Creation - Part 1
        • Day 2: Level Creation - Part 2
        • Day 3: Level Creation - Part 3
        • Collision Detection Basics
        • Day 4: Collision Detection Part 1
        • Day 5: Collision Detection Part 2
        • Day 6: Collision Detection Part 3
        • Day 7: Health System & Death
        • Day 8: Basic AI & Final Touches
      • Unit 4: Android Game Development >
        • Day 1: Introduction to Android
        • Day 2: Setting up for Development
        • Day 3: Creating our First Android Application
        • Day 4: Parts of an Android Application
        • Day 5: The Android Game Framework: Part I
        • Day 6: The Android Game Framework: Part II
        • Create an Android Game From Scratch (or port your existing game)
        • Day 7: Creating an Android Game (From Start to Finish)
      • Reference Sheet
    • Zombie Bird Tutorial (Flappy Bird Remake) >
      • Unit 1: Building the Game >
        • Introduction
        • Day 1: Flappy Bird - An In-depth Analysis
        • Day 2: Setting up libGDX
        • Day 3: Understanding the libGDX Framework
        • Day 4: GameWorld and GameRenderer and the Orthographic Camera
        • Day 5: The Flight of the Dead - Adding the Bird
        • Day 6: Adding Graphics - Welcome to the Necropolis
        • Day 7: The Grass, the Bird and the Skull Pipe
        • Day 8: Collision Detection and Sound Effects
        • Day 9: Finishing Gameplay and Basic UI
        • Day 10: GameStates and High Score
        • Day 11: Supporting iOS/Android + SplashScreen, Menus and Tweening
        • Day 12: Completed UI & Source Code
    • Android Application Development Tutorial >
      • Unit 1: Writing Basic Android Apps >
        • Before you begin...
        • Day 1: Android 101
        • Day 2: Getting to Know the Android Project
        • Day 3: The Development Machine
        • Day 4: Building a Music App - Part 1: Building Blocks
        • Day 5: Building a Music App - Part 2: Intents
        • Day 6: Building a Music App - Part 3: Activity Lifecycles
  • New Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Facebook
  • Twitter

GAME DEVELOPMENT TUTORIAL: DAY 1-9: More on Looping

9/16/2012

49 Comments

 
Picture
I think after 3 more days, we can begin actual game development.

Of course there's a lot more to learn, but I think it would be more fun and more efficient to learn some of the tougher concepts by application.

Without further ado, let's jump into our next lesson!

Lesson #1-16: Analyzing the Looping Class

In the last lesson, I created a class called Looping as follows: 
 public class Looping {
public static void main(String[] args) {

int initialValue = 0;
int finalValue = 10;

int counter = 0;

if (initialValue < finalValue) {
System.out.println("Input accepted!");
System.out.println("Initial Value: " + initialValue);
System.out.println("Final Value: " + finalValue);
System.out.println();
System.out.println("Initiating count.");
System.out.println();

System.out.println(initialValue);
counter++;

while (initialValue < finalValue) {
initialValue++;
System.out.println(initialValue);
counter++;

}

if (initialValue == finalValue) {
System.out.println();
System.out.println("Counting complete.");
System.out.println("There are " + counter
+ " numbers (inclusive) between "
+ (initialValue - counter + 1) + " and " + finalValue
+ ".");
}

} else {
// Executed if: if (initialValue < finalValue) above is not true.

System.out.println("Final value is less than initial value!");
System.out.println("Please choose new values.");
}

}
}
The Output:
Picture
It might have been a while since we discussed some of these concepts, so please refer to previous lessons if you are confused!
And of course, you can comment below with questions as always.

Now to analyze:

1. The blue text begins the Looping class. (I hope you know this by now  ).
2. The light blue text then begins the main method (you should know what the main method is also).

3. The next three statements beginning "int..." initialize three integers: initialValue, finalValue, and counter.

4. The point of this program is to count from a lower number to a higher number, so the first condition it tests for, using an if statement is whether initialValue < finalValue.

5. If this condition is satisfied, we see a bunch of System.out.println("...")

6. The counter variable is increased by 1.

7. The while loop then initiates, and this increases initialValue by 1, and displays the new initialValue.

8. Counter variable is again increased by 1.

9. With each iteration (repetition) of the while loop, we check if the initialValue (which increases by 1 with each iteration) is equal to finalValue, because that is when we can stop counting.

10. When this if condition (colored gold) is satisfied, we stop counting, and display the counter variable, which has been increasing by 1 with each increase in value of the initialValue variable.


Make sure you run this code a few times with your own numbers to understand what exactly is happening at each step.

If you want to make your own programs, you should be able to analyze and at least have a general sense of what other programmers are trying to accomplish with their code.

Lesson #1-17: The For Loop

The while loop utilized a counter that we coded ourselves. Now we will implement the for loop which comes with a built-in counter, so to speak. 

The form of a for loop is as follows (as stated in Java docs): 
Picture
"When using this version of the for statement, keep in mind that:

1. The initialization expression initializes the loop; it's executed once, as the loop begins.
2. When the termination expression evaluates to false, the loop terminates.
3. The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value."

Let's look at an example: 
 class ForLoop {
public static void main(String[] args){
for(int variable = 1; variable <11; variable++){
System.out.println("Count is: " + variable;
}
}
}
The output would be: 
Picture
The for loop is quite simple:

The first part of the for (...) is the initialization of a variable.
We initiate an integer called variable, with the value of 1.

The second part "variable <11" is the condition. Think of this as: while (variable < 11)...
Everything inside the for loop will run as long as this second part is satisfied.

The third part "variable++" is what happens to the initialized variable after each iteration. We increase it by one.


Lesson #1-18: Applying the For Loop

In this lesson, we will recreate the Looping program using not the while loop, but the for loop!

Have a look at the while loop example again: 
 public class Looping {
public static void main(String[] args) {

int initialValue = 0;
int finalValue = 10;

int counter = 0;

if (initialValue < finalValue) {
System.out.println("Input accepted!");
System.out.println("Initial Value: " + initialValue);
System.out.println("Final Value: " + finalValue);
System.out.println();
System.out.println("Initiating count.");
System.out.println();

System.out.println(initialValue);
counter++;

while (initialValue < finalValue) {
initialValue++;
System.out.println(initialValue);
counter++;

}

if (initialValue == finalValue) {
System.out.println();
System.out.println("Counting complete.");
System.out.println("There are " + counter
+ " numbers (inclusive) between "
+ (initialValue - counter + 1) + " and " + finalValue
+ ".");
}

} else {
// Executed if: if (initialValue < finalValue) above is not true.

System.out.println("Final value is less than initial value!");
System.out.println("Please choose new values.");
}

}
}
We can safely remove all the counter variable related statements, as the for loop will handle that for us.
The only other segment of code we need to modify is the while loop section.

We will replace this with the for loop : 
public class ForLooping {
    public static void main(String[] args) {

        int initialValue = 0;
        int finalValue = 10;
        int counter = 0;

        if (initialValue < finalValue) {
            System.out.println("Input accepted!");
            System.out.println("Initial Value: " + initialValue);
            System.out.println("Final Value: " + finalValue);
            System.out.println();
            System.out.println("Initiating count.");
            System.out.println();

            System.out.println(initialValue);
            counter++;
           
            for (initialValue = 1; initialValue < finalValue + 1; initialValue++) {
                System.out.println(initialValue);
                counter++;
               
                if (initialValue == finalValue){
                     System.out.println();
                     System.out.println("Counting complete.");
                     System.out.println("There are " + counter + " numbers (inclusive) between " + (initialValue - counter + 1) + " and " + finalValue + ".");
                }
            }
                   
        } else {
            System.out.println("Final value is less than initial value!");
            System.out.println("Please choose new values.");
        }

    }
}
Remember to change the name of the class (Select it on the left side of the screen in the Package Explorer, use F2) before running the code!

Also, this code is not color coded. Try to keep track of what each pair of braces contain. An easy way to do this is to start from the highest level and go deeper into the code: (Class, main method, if statement, for loop, if statement). The indentations should help you discern which braces correspond. 

If you need help, refer to the previous class "Looping." It is mostly similar.
Also if you copy and paste this into Eclipse, you can click next to a curly brace and Eclipse will draw a small rectangle on its counterpart to make it easier to tell.

Ctrl + Shift + F automatically indents your code for you! Use that well.


The output: 
Picture
Using the for loop is another way to do the same work! 

This concludes Day 9.
Picture
Go to Day 8: Looping
Go to Day 10: Inheritance, Interface
49 Comments
Reece
11/16/2012 01:20:33 pm

Thanks!

Reply
Zak
12/6/2012 01:16:06 pm

In your first example explaining the For Loop there is a minor error. you were missing a parentheses. it should look like this.

for(int variable = 1; variable <11; variable++) {
System.out.println("Count is: " + variable);
}

and in the applying the for loop. I see what's missing but don't get exactly how it's working. Like what part actually says initialValue == finalValue. or is that missing? because when you run the code you no longer get the there 11 numbers between 0 and 10. so maybe that's throwing me off.

Could you just add that in? (I'm actually gonna just try this real quick.. but I'd still appreciate any input that may help clarify this for me.) Thanks!

Reply
James C.
12/7/2012 02:12:38 am

Thank you so much for catching my mistakes.

Everything was fixed!

Reply
Nica
5/15/2014 03:18:45 pm

Another minor mistake in the first ForLoop example, the "System.out.println("Count is: " + variable;" is missing a closing parentheses:

class ForLoop {
public static void main(String[] args){
for(int variable = 1; variable <11; variable++){
System.out.println("Count is: " + variable;
}
}
}

Great tutorials though. You are really good at explaining all this.

Dnnis
1/3/2013 11:52:08 am

Hello Can you explain to me this part of the Syntaxis please?

for (initialValue = 1; initialValue < finalValue + 1; initialValue++)

I understand the For loop but I got Confused when you write this "finalValue + 1" ......what did you do it?

Reply
dnnis
1/3/2013 11:53:29 am

Reply
Sparsh
1/7/2013 01:16:47 am

Yeah....
I am too stuck in the same statement as Dnnis.
Also why cant we use initialValue<=finalValue instead of using initialValue<finalValue+1???

Reply
James C
1/7/2013 01:24:47 am

It's always a good idea to have a < not a <= inside a for loop. It behaves in more predictable ways (because of the way it increments).

To keep it a <. I added a + 1.

Reply
James C.
1/7/2013 01:37:40 am

Although now that I look at it, i probably should have initialized it at 0. :)

Refer to the answer in this link:
http://stackoverflow.com/questions/182600/should-one-use-or-in-a-for-loop

The key word is "idiomatic."

Adrian
2/2/2013 02:07:34 pm

Hi love the tutorial so far. looking forward to getting further in.

Just a comment on the code :
for (initialValue = 1; initialValue < finalValue + 1; initialValue++) {
System.out.println(initialValue);
The only thing I see here is for the class to work correctly initialValue needs to be 0. If you change initialValue to be anything else the output becomes incorrect.
I know eclipse produces a warning but the line could be changed to

for (initialValue = initialValue; initialValue < finalValue + 1; initialValue++) {

to fix this. either that or the loop could use another assignment variable to track its' looping other than initialValue

Reply
Chuck
2/9/2013 10:01:48 am

I seen this too, so I came up with this simple little fix.
for (initialValue = initialValue + 1; ...

Reply
sarlej
12/14/2014 05:59:57 am

Actually: "for (initialValue++;...." is enough.

Chuck
2/9/2013 09:46:52 am

I decided to remove all the counters and initiate a "difference" that = finalValue - initialValue, then reference it later where counters were used. What kinds of pros/cons could there be to this?
It worked and everything, but to get difference + 1 to work for the number count statement, I had to type it like this:

System.out.println("There are " + (difference + 1)
+ " numbers (inclusive) between "
+ (initialValue - difference) + " and "
+ finalValue + ".");
I'm surprised it worked. :P

Reply
Andreas
4/12/2013 07:56:08 am

The example greatly benefits if you start the for loop like

for ( initialValue = 0; initialValue <= finalValue; initialValue++ )

Though you are right to point out that "greater or equal" comparison has a risk one just has to avoid comparing against the maximum value for the _type_ of the loop counter to overcome this problem.

Reply
Sven
2/9/2014 02:47:10 am

for ( ... ; initialValue <= finalValue; ... ) is not correct i think

in this case the counter will count up to 12 with initialValue counted up to 11 i think

initalValue counter (initialValue <= finalValue) = false

0 1 (starts) no
1 2 no
...
9 10 no
10 11 ( 10 <= 10) = true, no
11 12 ( 11 <= 10) = false, yes

Reply
Sven
2/9/2014 03:11:40 am

output would be ELSE CASE coz condition ( initalValue == 10 ) of 2nd if statement will never be fulfilled due to initalValue = 11 after loop termination and and therefore if ( initialValue == 10 ) { ... } will be skipped i think

Sven
2/9/2014 03:54:19 am

Oh now i got it XD

u r saying initalValue <= finalValue does not work properly

mea culpa

Sven
2/9/2014 05:27:34 am

Oh no u refer to chuck's code

i FINALLY got it XD

therefore for ( ...; initalValue <= finalValue; ...) runs properly

I thought u refer to the tutorial code

mea culpa again

Yousef
8/24/2013 12:51:54 am

I was trying to test myself by seeing the output before reading your source code, then I wrote my code in Eclipse. After writing my code I found out that there is a little difference between my code and yours, and I just want you to tell me if it's bad practice or not.

public class Test {
public static void main(String[] args) {
int initialValue = 0;
int finalValue = 10;
int counter = 0;

if (initialValue < finalValue) {
System.out.println("Input accepted!");
System.out.println("Initital value: " + initialValue);
System.out.println("Final value: " + finalValue);
System.out.println();
while (initialValue <= finalValue) {
System.out.println(initialValue);
initialValue++;
counter++;
}
System.out.println();
System.out.println("Counting complete.");
System.out.println("There are " + counter + " numbers between "
+ (initialValue - counter) + " and " + finalValue + ".");
} else {
System.out.println("Invaild.");
}

}
}

Reply
ahmed
9/5/2013 11:50:03 am

System.out.println(intialvalue);
didn't print 0 value and printed it as intialvalue
what that mean?
and thanx

Reply
Sven
2/9/2014 01:57:29 am

thinking of it i would say the correct loops would be

either for ( initialValue = 0; initialValue < finalValue; initalValue++)
or for ( counter = 1; initialValue < finalValue + 1; initalValue++)

unfortunately i can't check it coz i don't own a pc at the moment :(

Reply
Sven
2/9/2014 03:25:11 am

2nd loop must be

or for ( counter = 1; initialValue < finalValue; initalValue++)

Reply
Sven
2/9/2014 02:01:05 am

btw thanx for this great tutorial :)

Reply
seth
3/20/2014 04:10:27 pm

I love your enthusiasm, Sven. Your comments are helping me think about the code in new ways.

Reply
Sven
2/9/2014 02:14:41 am

For explanation: i don't think for ( initialValue = 1; ... ; initialValue++) workes coz THERE IS NO STATEMENT SETTING initialValue TO 1 and THEREFORE THE LOOP IS NEVER INITIALIZED

Reply
Sven
2/9/2014 03:42:16 am

is there really a finalValue + 1 needed

this a < a + 1 causes a lot of ????? for me

initalValue < finalValue gives a good termination expression

for ( initialValue = 0; initialValue < finalValue; ... )

initalValue / counter / ( initialValue < 10 )

0 / 1 (loop starts coz initalValue = 0) / ( 0 < 10 ) true
...
10 / 11 / ( 10 < 10) false therefore counter is terminated

Reply
Sven
2/9/2014 04:11:04 am

sorry again
last line must be

10 / 11 / (10 < 10 ) false therefore LOOP is terminated

Reply
Sven
2/9/2014 04:32:44 am

wouldn't for ( a = a; a < a + 1; a++) create an infinite loop
due to a < a + 1 is always true ?

we have two different variables

initalValue (a) & finalValue (b)

therefore a < a + 1 can never be the termination expression

it must be a < b otherwise the output is wrong

hope u can follow my thoughts

Reply
Sven
2/9/2014 04:49:45 am

plz correct me if i'm wrong :)

i'm a absolutely newbe to java

and yes therefore i'm really confused XD

Sven
2/9/2014 04:58:19 am

dammit i really need a pc

this java stuff is really fun :D

Sven
2/9/2014 08:54:07 am

here's my version of the ForLooping class

public class ForLooping {

public static void main (String[] args) {

int initialValue = 0;
int finalValue = 10;
int counter = 0;

if (initialValue < finalValue) {
System.out.printIn("input ACCEPTED!");
System.out.println("initialValue:" + initialValue);
System.out.println("finalValue:" + finalValue);
System.out.println("INITIATING COUNT!");

for (counter = 0; initialValue + counter < finalValue +1; counter++){
System.out.println(initalValue + counter);
}

System.out.println("counting COMPLETE!");
System.out.println("there are" + counter + "numbers (inclusive) between" + initialValue "and" + finalValue + "!");
}

else {
System.out.println("Input INVALID!!!");
}
}
}
}

wonder if this one works?

Reply
Sven
2/9/2014 02:39:19 pm

doesn't work, but this one should be fine


class ForLooping {
public static void main (String[] args
) {
int initialValue = 0;
int finalValue = 10;
int counter = 0;

if (initialValue < finalValue) {
System.out.println("input ACCEPTED!");
System.out.println("initialValue:" + initialValue);
System.out.println("finalValue:" + finalValue);
System.out.println("INITIATING COUNT!");

for (counter = 0; initialValue + counter < finalValue +1; counter++){
int result = initialValue + counter;
System.out.println(result);}

System.out.println("counting COMPLETE!");
System.out.println("there are" + counter + "numbers (inclusive) between" + initialValue + "and" + finalValue + "!");
}

else {
System.out.println("Input INVALID!!!");
}
}
}

Reply
Sven
2/10/2014 12:21:16 am

tested this via online IDE and it runs properly :)))))))

and i think i see now through this a < a+1 topic

a <= b <=> a < b + 1

Reply
Buzz.
4/15/2014 02:27:36 am

Since the 'for loop' statement in the example provided above is only Valid if the value for the initialValue = 0,

Here is my interpretation( version ) of 'for loop' statement which works for any initialValue.
It is as follows :

public class ForLooping {
public static void main(String[] args) {
int initialValue = 7;
int finalValue = 30;
int counter = 0;

if (initialValue < finalValue) {
System.out.println("Input Accepted.");
System.out.println("Initial Value: " + initialValue);
System.out.println("Final Value: " + finalValue);
System.out.println();
System.out.println("Initiating Count.");
System.out.println();


for (int x = initialValue; x < finalValue+1; x++) {
System.out.println(x);
counter++;

if (x == finalValue) {
System.out.println();
System.out.println("Counting Complete.");
System.out.println("There are " + counter
+ " numbers (inclusive) between "
+ (x - counter + 1) + " and "
+ finalValue + ".");
}
}
} else {
System.out.println("Final Value is less than or equal to the Initial Value!");
System.out.println("Choose New Values.");
}

}

}

Reply
iyad alsnabrh
6/23/2014 12:08:10 am

i write this code its more simple from the original and give the same result but is it good or its not the same purpose from the original example ?

int initialValueb = 2;
int finalValuef = 10;
int count = 0;
while (initialValueb <= finalValuef) {

System.out.println(count);
count++;
initialValueb++;
}

System.out.println("the lopp is completed");
System.out.println("there are " + (count) + " betwen "
+ (initialValueb - count) + " and " + finalValuef);

}
}

Reply
Miguel
7/6/2014 03:26:32 pm

Hey, thanks for the tutorial, I'm really enjoying it.
But I have a question... On the For Loop example, changing initial value doesn't work.
Example:
I changed initial value to 6:

Input accepted!
Initial Value: 6
Final Value: 10

Initiating count.

6
1
2
3
4
5
6
7
8
9
10

Counting complete.
There are 11 numbers (inclusive) between 0 and 10.

Why is that? How could I fix that?
Thanks in advance

Reply
Miguel
7/6/2014 03:32:18 pm

Found the problem:
for (initialValue = 1; initialValue < finalValue + 1; initialValue++) {
System.out.println(initialValue);
counter++;

I fixed it by using += instead of =
for (initialValue += 1; initialValue < finalValue + 1; initialValue++) {
System.out.println(initialValue);
counter++;
(Now, I think it's probably what it was meant to be, but the + was probably missing... ^^')
Now the code initializes by summing 1 to the initial value, not making it BECOME 1 =P

Reply
Chaitanya
7/14/2014 12:42:04 am

i m really enjoying learning development! first step to my career ! thank u soo much

Reply
Maverink
8/8/2014 12:25:53 pm

Sorry( im sure this is a dumb question) but why you need the variable counter..cant you just add increments to variable initialValue?

Reply
Antonio
9/22/2014 08:03:01 am

Imagine you had:
int initialValue = 5;
int finalValue = 10;

then at:
for (initialValue = 1; initialValue < finalValue + 1; initialValue++) {

you would mess the intended result, because it would not run 5 to 10 and defined earlier, but 1 to 10 and defined on FOR.

maybe should be:
for (someVar = initialValue; initialValue < finalValue + 1; someVar++) {

and would work for any values defined. am i right?

Reply
John
12/6/2014 01:23:56 am

wow thanks for this i did some googeling and found that the ods of a coin landing on its side are 1 in 6000 so i made this to see if that is true

import java.util.Random;

public class SideLandingExperiment {

static boolean coinOnSide = false;
static int timesTried = 0;

public static void main(String[] args) {

while (coinOnSide == false) {
Random rand = new Random();
timesTried++;
System.out.println("Try number" + timesTried);
int result = rand.nextInt(6001);

if (result == 0) {
System.out.println("side");
System.out.println("It took " + timesTried
+ " tries to get the coin to land on its side");
coinOnSide = true;
}

else if (result > 0) {

if (result < 3000) {

System.out.println("heads");
}
}
if (result > 3000) {
System.out.println("tails");

}// ends if
}// ends loop
}// ends method main
}// ends Class

Reply
John link
12/6/2014 01:27:25 am

ps if you want pastebin :

http://pastebin.com/PRxi5vnX

Reply
TUGAAAAA
3/16/2015 05:56:03 pm

Tantos portugueses :o

Reply
SmartAss Zombie
5/1/2015 11:56:46 pm

while (initialValue < finalValue) {
initialValue++;
System.out.println(initialValue);
counter++;
may you please explain this for me
and one for question: what is counter?

Reply
CrapDogg
7/31/2015 09:56:30 am

Took me forever to find what I had done wrong....my results:

Input accepted!
Initial Value: 0
Final Value: 10

Initiating count.

0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10

Counting complete.
There are 11 numbers (inclusive) between 0 and 10.


The Issue:
for (initialValue = 1; initialValue < finalValue + 1; initialValue++) {
System.out.println (initialValue);
System.out.println (initialValue);
counter++;
two occurrences of System.out.println....weird results.

*Fantastic job explaining this complex language!

Reply
ALI
10/14/2015 03:42:10 am

Hey, thank you for this great tut.
i wrote a code that counts 10 as in the example in the tut, but i didn't use FOR or WHILE, here is the code :

public class PracticeJava {
static int arg2 = 0;
static int arg0 = 1;

public static void main(String[] args) {
System.out.println(arg2);
se();
}

public static void se() {
if (arg2 < 10) {
th();
}
}

public static void th() {

arg2 = arg0 + arg2;
System.out.println( arg2);
se();

}
}

my question is, IS THIS CODE MORE EFFICIENT (in terms of CPU processing and RAM Usage)?

THX for your time

Reply
Bryan
4/4/2016 09:21:16 am

First off, thank you for this very informative tutorial, I am eagerly working my way through it so that I can understand Java fully.

I was playing with the initial value in the forLoop class and found that it was only counting from 0 to whatever the final value was. The issue is the code is setting the initalValue to "1" in the "for" statement
for (initialValue = 1; initialValue < finalValue + 1; initialValue++)
So when I would run it with 4 through 10, it would print...
Input accepted!
Initial Value: 4
Final Value: 10

Initiating count.

4
1
2
3
4
5
6
7
8
9
10

Counting complete.
There are 11 numbers (inclusive) between 1 and 10.




I corrected this by telling the for statement
for (initialValue = initialValue; initialValue < finalValue + 1; initialValue++)
The assignment to variable initialValue has no effect
However this time it prints correctly...

Input accepted!
Initial Value: 4
Final Value: 10

Initiating count.

4
4
5
6
7
8
9
10

Counting complete.
There are 8 numbers (inclusive) between 4 and 10.

Am I missing something with the forLoop, or just digging deeper than I need to right now?

Reply
marqs
2/2/2017 11:40:57 am

for (initialValue = 1; initialValue < finalValue + 1; initialValue++) {
should be
for (initialValue += 1; initialValue < finalValue + 1; initialValue++) {
instead
because otherwise it will just overwrite the initialValue from the beginning of the program ;)

Reply
Ali
12/7/2018 05:07:09 pm

I know this will sound stupid, but just wondering if the for loop parameters can take floating number rather than integer. I know nobody will use it, but just wondering.

Reply



Leave a Reply.

    Author

    James Cho is the lead developer at Kilobolt Studios. He is a college student at Duke University and loves soccer, music, and sharing knowledge. 


© 2014 Kilobolt, LLC. All rights reserved.