Jump to content

Hey guys so I have these two pieces of code:

 

for (int i = 0; i < 10; i++) {
	for (int j = 0; j < 10; j++) {
	if (j > i)
		continue;
	System.out.println("Java");
	}
}

AND

for (int i = 0; i < 10; ++i)
	for (int j = 0; j <= i; ++j)
		System.out.println("Java");

Can someone explain to me how do they print "Java" 55 times? 

 

Thanks guys!

Hi

Link to comment
https://linustechtips.com/topic/689680-java-beginner-needs-help/
Share on other sites

Link to post
Share on other sites

Step through the code one statement at a time. Write it out on paper or whatever works best for you.

// example 1
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        if (j > i)
            continue;
        System.out.println("Java");
    }
}

/*
i = 0
    j = 0
        j > i == false
        print "Java"
    j = 1
        j > i == true
        continue
    ...
i = 1
    j = 0
        j > i == false
        print "Java"
    j = 1
        j > i == false
        print "Java"
    j = 2
        j > i == true
        continue
    ...
...
*/

 

Link to comment
https://linustechtips.com/topic/689680-java-beginner-needs-help/#findComment-8849434
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×