• Post Reply Bookmark Topic Watch Topic
  • New Topic

understand code  RSS feed

 
Greenhorn
Posts: 14
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi can anyone help, will this loop terminate.?
will the results change if variables are unsigned ints instead of signed int???

Thanks
 
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you compiled and executed the code?

Be sure to add print statements for the variables you want to see the values of.

java does not have unsigned ints
 
jessica stud
Greenhorn
Posts: 14
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code.java:1: error: '{' expected
class code.java
          ^
code.java:9: error: reached end of file while parsing
}
^
2 errors


this is my code:


class code.java
public static void main (String[] args) {
int i = 1;
int c = 1;
while(i > 0){
i = i * 2;
c++;
}
}

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also posted at: http://www.dreamincode.net/forums/topic/398217-understanding-java-simple-code/

code.java is not a valid java class name.

Be sure to wrap your code in code tags.
 
jessica stud
Greenhorn
Posts: 14
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i got rid of all my errors.
but how to see iff the loops terminates
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to see iff the loops terminates

Add a print statement after the loop.
 
jessica stud
Greenhorn
Posts: 14
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

class code{
        public static void main (String[] args) {


int i = 1;
int c = 1;
        while(i > 0){
i = i * 2;
c++;
        System.out.print ("how many loops: ");

        }
    }
}



this is what i have
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please wrap your code in code tags.

What happens when you compile and execute the program?
 
jessica stud
Greenhorn
Posts: 14
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I compile, I get no errors.

When I run, nothing happens
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i will reach the maximum integer value and will return i= -2147483648.
this will terminate your loop.
add a System.out.println(i); to check the i values.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run, nothing happens

Does the program's execution immediately end
or does it run for a long time?

Please post your code in code tags to make it easier to read and understand.
 
jessica stud
Greenhorn
Posts: 14
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OUTPUT :
how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops: how many loops:




I am close, now I just need to count how many times it happens? Is there a way?
Thanks
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like the print stateement is inside of the loop.   If it were outside of the loop it would only print one line. 

just need to count how many times it happens? Is there a way? 

I think the c variable does the counting.  If you want to see its value, add it to the print statement.
 
helena younes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
print c. It will tell you how many times it ran
 
jessica stud
Greenhorn
Posts: 14
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where do you put the print c?
Thanks
 
helena younes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After your while loop, if you only want it printed once its done.
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will help you (and us) to read and understand your code if you indent it consistently and use blank lines judiciously. For example:

This makes it obvious the print statement is inside the while loop when it should be after the loop (btw I haven't corrected your mistake of using the variable lines which isn't declared anywhere)
 
jessica stud
Greenhorn
Posts: 14
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you.

I just don't understand what print c will do?
Will it count all the iterations ?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will it count all the iterations ?

What is the purpose of the statement on line 8?

Where did you get this code?  Why are you working with it?
You need to study java programming some more to be able to understand what the code is doing.
 
jessica stud
Greenhorn
Posts: 14
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question for a midterm exam.
I am trying to understand the code. We will have to write one of our own.
 
helena younes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the beginning of your code you declared a variable : int c
Then in your while loop, you are saying that while i>0,  int i will be multiplied by 2 and c will increase by one.
Therefore, each time your loop runs, int c will be +1.
After the while loop, System.out.print(c); will print your variable c, which is how many times your loop did the work.
I hope this makes sense.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!