Related
I am having such a tough time reading and understanding recursive statements in java.
if my professor shows us code and asks what does this do? what does it return?...I find myself staring at the code as if its going to eventually make sense...but it NEVER does.
examples like---
public static Int mystery(int[][] a, int b) {
int x = 0;
for (int i = 0; i < a.length; i++){
for (int j = 0; j < a.length; i++){
x += (a([i][j] == b) ? 1 : 0;
}
}
return x:
}
what does mystery (a, 8) return where a =
{{6, 4, 5, 8},{4, 6, 4, 8,}, {7, 3, 6, 4,}, {1, 5, 7, 8}}
can someone please explain? thanks!
Hi welcome to the world of programming
This is not recursive programming. Recursive is when a function calling itself.
For example, delete files() -> which will call itself again when there is folder, and you want to delete files under it again and again.
Your example is called a 'Nested Loops' -> Loops of Loops, and the condition 'a.length' being used in both i and j is wrong.
You are trying to loop arrays of arrays, a 3d table. It should be something like this.
int x = 0;
for (int i = 0; i < a.length; ++i){
for (int j = 0; j < a[i].length; ++j){
if (a[i][j] == b) {
++x;
}
}
return x;
Changes:
Try not to use 'ternary operator' ?: as this might confuse you as a beginner.
the max length of inner loop should be a[i]'s length
using ++i is better than i++ if you are not using the value directly.
Imagine
[[1,1,1],
[2,2]]
in your first loop, a[0].length will be 3.
in your second loop, a[1].length will be 2.
as for ++i vs i++:
++i is modifying i directly, making the value +1 instantly
i++ means var b = i; i = b + 1;
it is creating a new temporary variable to hold the original i value.
example:
int i = 0, j = 0;
System.out.println(i++); // will print 0, but i will be 1 moving forward
System.out.println(++j); // will print 1, and j will be 1 moving forward
you will learn more on this in future.
Firstly, if you don't understand something, don't just keep staring at it expecting it to suddenly make sense, as that's likely to just be frustrating. Instead, try to break it down into smaller parts, and work from what you know and expand from that, piece by piece.
That's easier said than done, though. How do you split it up? Here are two approaches: you'll need both.
Low level
One way is to ‘dry run’ the code, instruction by instruction, as if you were the computer. This is slow and tedious, but it gives you the best insight into what the computer is actually doing.
Take a piece of paper, or the virtual equivalent such as a spreadsheet. Make a column for each variable in the method: a, b, x, i, j. (Make the one for a extra wide.)
The method starts with parameters being set, so write the array under a and 8 under b. Next, x is set to 0, so write that under x. Then i is set to 0, and compared to a.length. The array a has four items, the inner arrays, so the length is 4 and the current value of i, 0, is less than that, so the condition is true and we enter the body of the outer loop.
The body of the outer loop contains only the inner loop, which sets j to 0 and compares it to a.length. It's less, so enter the body of the inner loop.
The inner loop body does a few things starting on the right of the =, first working out what a[i] means. Currently i is 0, so that's a[0], which is the first sub array. Then work out a[0][j], which is a[0][0], which is 6. That's not equal to b, so the condition is false: take 0 and add it to the
number under x, 0, and write the result under x to replace the current value. (Replacing 0 with 0? Yes, this doesn't actually change anything.)
Now that you've finished this iteration of the inner loop body, do the i++ (which should probably be j++) by replacing i's 0 with 1. Then do the test again: j is still less than 4, so do the inner loop body again, this time with a[1][0].
I'm going to stop here because it really is tedious, but hopefully you can see the process. The inner loop continues until its condition becomes false, then the outer loop increments i, starting the inner loop again but this time looking at a[1][0], a[1][1], and so on. That continues until the outer loop also makes its condition true. Finally, the last value of x is returned, and if you go through all the steps you'll get the same result the computer would. (Try it: once you get the hang of it, it'll be quicker than reading through the above, thank goodness.)
Okay, so that gives you the same sequence of steps the computer takes, and sometimes that level of detail is invaluable, especially when you have a bug such as assigning to the wrong variable name. But you may still end up not knowing why those particular operations, not seeing the wood for the trees. How can you work out what the purpose of the algorithm is?
High level
Another way we can look at the method is to work out what each statement does, and what the possible cumulative effect could be. For instance, the outer loop steps through each of the nested arrays, and the inner loop steps through each number within a nested array: so in combination, they step through every number within the array a. So something is happening with all the numbers.
Also, x is set to 0 at the start, and within the loops it sometimes has 0 added and sometimes 1. That tells us it's a counter, and by the end it'll tell us how often the condition was true: that is, how many of the numbers within a are equal to b. And so that's what the method returns: how many times b appears in a.
We can also tell the minimum value returned (0, when none of the numbers match), and the maximum (the total count of numbers, because at most 1 is added for each of them).
This is much more useful: we know what the method can be used for, and we can work out what the worst cases are: if we give it more than four billion numbers, a 32-bit int for the result wouldn't be enough, for instance; but it won't run forever (assuming j++) because the array has a fixed size, and it won't run out of memory because it doesn't allocate any other than for the variables.
But you need to be careful with this approach, because it's easy to make assumptions and miss details that change the outcome significantly: use the wrong kind of comparison or the wrong variable, or forget to clear a total, say, and you can end up with code that doesn't do what you think it does.
That can result in some of the most annoying bugs, when you stare at your code and you're sure it's right but somehow it still gives you the wrong answer. When that happens to you, switch to dry-running, re-examine your assumptions, or show your code to someone else: without your high-level knowledge of what the code should do, they can spot what it actually does.
Ultimately this is all a skill that you'll learn with practice, so keep writing and reading code.
I am learning java nowadays, and while going through loops in java, I am stuck here, I am unable to understand why the following program is printing "Hello" three times. can you please explain?
public class Helloworld {
public static void main(String[] args) {
for (int i =1; i<=2; i++ ) {
for (int j =1; j<=i ; j++) {
System.out.println("Hello");
}
}
}
}
for loop iterations:
i=1
j=1 — prints "Hello"
j=2 — breaks out of inner loop, `j <= i` is false.
i=2
j=1 — prints "Hello"
j=2 — prints "Hello"
j=3 — breaks out of inner loop, `j <= i` is false.
This is what happens when the above code is run.
To understand why, change
System.out.println("Hello");
to
System.out.println("Hello: i = " + i + ", j = " + j);
compile and run it again, and look carefully at the values of i and j printed out.
Basically, in the second iteration of the outer loop, the inner loop runs twice.
If you cannot compile and run the program, there is another way to understand the behavior. Hand execute it.
Get a pencil an piece of paper, draw a table with a column for each variable. Each time a variable is assigned to / modified (e.g. i = 0 or i++) add a new row to the table with the current values of all of the variables. When you have a test (e.g. i<=2) read the variable's value from the latest row in the table.
After some practice you will be able to do this in your head. After more practice you will be able to read the code (like you read English text, or mathematical notation) and reason about what the code does.
Or use a debugger :-)
Sometimes when learning loops, writing small loops on paper can help.
Here you habe a nested loop using variables i and j.
First, the inner loop, j=1, and 1 is less than or equal to 1(i), so it prints. Then j++.
j is now 2, which is not less than or equal to 1, ending inner loop cycle. Now back to outer loop, i++,
i is now 2.
Inside the inner loop- j=1, less than or equal to 2, so second print. Now j++. Condition still true, as j(as 2) is still less than or equal to 2, so third print.
Hope this debugging method helps you.
You have two loops. The outer i loop runs twice, but inner j loop depends on the value of i with the j<=i part. So for the first i loop, j runs once, and the second i loop j runs twice, so three times all up.
This has to do with the nested looping. There's (2) loops to pay attention to here. For each iteration of the outer loop (i), the inner loop (j) may run multiple times. If you change the output to output the iterators, it will give you some visibility into this property.
System.out.println("i:"+i+" j:"+j);
OUTPUT: $javac HelloWorld.java
i: 1 j:1
i: 2 j:1
i: 2 j:2
If you want to get a more detailed view on nested looping, here's a good video series I'd recommend: Nested Looping (The Coding Train)
Hope this helps!
I want my for loop to have the length a changing number. Can i do that?
I havent really tried anything
for (int i=0; i < colsAdd[i] ; i++){
System.out.print(" 0.0");
}
Actually the colsAdd[0] is the only size.
I'm not sure if I understand exactly what you're asking, but it seems like you're wondering what you are allowed to put into the different parts of a for loop, and the order that they run in.
If we change the code you pasted above to match the diagram below, the rules will become apparent.
for (initialization; condition; updateCounter){
codeInsideLoop
}
Any code can go into those portions of a for loop, they can even be left blank! For instance, the following code is equivalent to a while(true) loop.
for (;true;){
System.out.print("This will run forever!");
}
Or this for loop increments inside of the loop.
for (int i=0; i < 10;){
System.out.print("This will print 10 times");
i++;
}
More in line with your question, this code calls a function each iteration to see if it should continue looping. This function could use any logic to return a boolean value. It will execute at the end of every singe loop regardless.
for (int i=0; keepLooping(i); i++){
System.out.print("This will print 10 times");
i++;
}
Now, just because you can do these things does not mean you should! Generally you want to keep the structure of your loops simple so everyone can understand what your goal is.
[edited the post to be more clear]
The code below is supposed to represent a number of trial runs, and within each trial assume a is the number of cases that need to be checked.
while(testCaseAmt > 0) {
for(int i = 0; i < a; i++) {
if(some condition)
//code when i is not the last element
//then I would like to break/return here.
if(i = a - 1)
//code when i is the last element
//then if the above if statement never runs during the a amt of trials
//i will print out something else
}
testCaseAmt--;
}
My question is if there is another way to access specifically the last element within a for loop to give it specific instructions. I want to loop through a set trials and if they meet the first condition, I stop the loop immediately and return. However, if the condition isn't found I still need to print out not found.
Cheers.
To me this seems related to "off-by-one errors" or "fencepost errors", see here.
If the code you're executing when i is not the last element isn't too complex, it could be better to just end the for loop one iteration earlier and write the specific code for the last element outside the for loop.
Adding an if statement inside a for loop where you know it will only be true at the very last iteration could be seen as bad-style.
You can define a boolean conditionMet = false before the for-loop and iterate until i < a-1.
If some condition applies, execute your code, then set conditionMet = true and break out of the for-loop.
After the for-loop, check if !conditionMet and in this case, execute the code for the last element.
I am learning java as well android. Almost everything that we can perform by while loop those things we can do in for loop.
I found a simple condition where using while loop is better than for loop
if i have to use the value of counter in my program then i think while loop is better than for loop
Using while loop
int counter = 0;
while (counter < 10) {
//do some task
if(some condition){
break;
}
}
useTheCounter(counter); // method which use that value of counter do some other task
In this case I found while loop is better than for loop because if i want to achieve the same in for loop i have to assign the value of counter to another variable.
But is there any specific situation when while loop is better than for loop
One main difference is while loops are best suited when you do not know ahead of time the number of iterations that you need to do. When you know this before entering the loop you can use for loop.
A for loop is just a special kind of while loop, which happens to deal with incrementing a variable. You can emulate a for loop with a while loop in any language. It's just syntactic sugar (except python where for is actually foreach). So no, there is no specific situation where one is better than the other (although for readability reasons you should prefer a for loop when you're doing simple incremental loops since most people can easily tell what's going on).
For can behave like while:
while(true)
{
}
for(;;)
{
}
And while can behave like for:
int x = 0;
while(x < 10)
{
x++;
}
for(x = 0; x < 10; x++)
{
}
In your case, yes you could re-write it as a for loop like this:
int counter; // need to declare it here so useTheCounter can see it
for(counter = 0; counter < 10 && !some_condition; )
{
//do some task
}
useTheCounter(counter);
for and while are equivalent, just a different syntax for the same thing.
You can transform this
while( condition ) {
statement;
}
to this:
for( ; condition ; ) {
statement;
}
The other way:
for( init; condition; update) {
statement;
}
is equivalent to this:
init;
while(condition) {
statement;
update;
}
So, just use which looks better, or is easier to speak.
Remember,
Everything done with a for loop can be done with a while loop, BUT not
all while loops can be implemented with a for loop.
WHILE :
While-loops are used when the exiting condition has nothing to do with the number of loops or a control variable
FOR :
for-loops are just a short-cut way for writing a while loop, while an initialization statement, control statement (when to stop), and a iteration statement (what to do with the controlling factor after each iteration).
For e.g,
Basically for loops are just short hand for while loops, any for loop can be converted from:
for([initialize]; [control statement]; [iteration]) {
// ...
}
and
[initialize];
while([control statement]) {
//Do something [iteration];
}
are same.
Use a FOR loop when you know the number of times you want to loop. The technical term for that is the number of iterations. How do you know the number of iterations? You know the start, stop and step. If you know those three pieces of information, you should use a FOR loop because it's the right tool for the job.
Use a DO loop when you don't know the number of iterations. If you don't know the start, stop, step or some combination of those then you need to use a DO loop. The expression will be evaluated at the top of the loop.
Use a DO WHILE loop if you want to loop at least once. Use just a WHILE loop if you don't want to loop at least once. The expression will be evaluated at the bottom of the loop.
for is finite, in the sense that it will finish looping when it runs out of elements to loop through....
while can be infinite if a condition isn't met or the loop broken
Edit
My mistake ... for can be infinite ..
The while loop is generally better when you don't have an iterator (counter usually).
One thing I feel I should point out is that when you use a for loop, you do not need to assign counter to another variable. For example for(counter=0; counter<10; counter++) is valid Java code.
As for your question, a for loop is usually better when you want a piece of code to run a certain number of times, and a while loop is better when the condition for the code to keep running is more general, such as having a boolean flag that is only set to true when a certain condition is met in the code block.
You can do something like:
int counter;
for (counter = 0; counter < 10; ) {
//do some task
if(some condition){
break;
}
}
useTheCounter(counter);
Anything that a while-loop can do, can also be done in a for-loop, and anything a for-loop can do, can also be done in a while-loop.
No. There's not a specific situation where for is better than while.
They do the same thing.
It's up to you choose when apply one of those.
int counter = 0;
while (counter < 10) {
//do some task
if(some condition){
break;
}
}
useTheCounter(counter); // method which use that value of counter do some other task
Hi I repeat your code because it is incorrect. You forget to increase your counter so it will remains on 0
int counter = 0;
while (counter < 10) {
//do some task
if(some condition){
break;
}
counter++;
}
useTheCounter(counter); // method which use that value of counter do some other task
while loops are much more flexible, while for loops are much more readable, if that's what you're asking. If you are wondering which one is faster, then look at this experiment I conducted concerning the speed of for and while loops.
https://sites.google.com/a/googlesciencefair.com/science-fair-2012-project-96b21c243a17ca64bdad77508f297eca9531a766-1333147438-57/home
while loops are faster.
What ever you can write in for loop can be converted to while loop. Benefits of using for loop are
The syntax allows you to start a counter, set the condition to exit loop, then auto increment.
You can get the same done in while loop also. But all which you can do in while loop is not possible to do in for loop. For example if you have more than one counter and you want any of them to increment based on a condition then while only can use. In for loop at the end of loop the counter increment happens automatically.
Best use
For matrix or single array single directional traversal, for loop is good
In case of multiple conditions and multiple counters then while loop is good. if yuo want to traverse an array from both sides based on different conditions then while loop is good.
While loop, there is lot more chance to forget increment counter and ends up into infinite loop, while in for loop syntax will help you to easily set the counter.
For loops include the notion of counting, which is great. Yet, when you don’t know how many times the code should run, while loops make sense.