Solved

java continue statement

Posted on 2016-09-06
10
114 Views
Last Modified: 2016-09-06
Hi,

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
package com.solution;

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

        String searchMe = "peter piper picked a " + "peck of pickled peppers";
        int max = searchMe.length();
        int numPs = 0;

        for (int i = 0; i < max; i++) {
            // interested only in p's
            if (searchMe.charAt(i) != 'p')
                continue;

            // process p's
            numPs++;
        }
        System.out.println("Found " + numPs + " p's in the string.");
    }
}

Open in new window

Found 9 p's in the string.

How below printed 35 when i comment continue where as above printed 9
package com.solution;

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

        String searchMe = "peter piper picked a " + "peck of pickled peppers";
        int max = searchMe.length();
        int numPs = 0;

        for (int i = 0; i < max; i++) {
            // interested only in p's
            if (searchMe.charAt(i) != 'p')
               // continue;

            // process p's
            numPs++;
        }
        System.out.println("Found " + numPs + " p's in the string.");
    }
}

Open in new window


Found 35 p's in the string.

peter piper picked a " + "peck of pickled peppers

when i counted above it has 49 characters including spaces though

please advise
0
Comment
Question by:gudii9
  • 5
  • 4
10 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 250 total points
ID: 41786437
Commenting out that line makes the code act like this:

            if (searchMe.charAt(i) != 'p')
               numPs++;

Open in new window


So your code is counting every character that isn't a P. This demonstrates why it can be a good idea to wrap if blocks in braces always, even when there's only one line.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41786451
So your code is counting every character that isn't a P.

including free space?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41786454
peter piper picked a " + "peck of pickled peppers

when i count i am counting as 40 manually though. which also matches below calculation
 as 49-9=40
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41786457
Don't count the " + " characters; they're not part of the string. (Print it out and see.)
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
ID: 41786492
CPColin is right. Please use
System.out.println("max is " + max);

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Author Comment

by:gudii9
ID: 41786542
package com.solution;

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

        String searchMe = "peter piper picked a " + "peck of pickled peppers";
        int max = searchMe.length();
        int numPs = 0;

        for (int i = 0; i < max; i++) {
            // interested only in p's
            if (searchMe.charAt(i) != 'p')
               // continue;

            // process p's
            numPs++;
        }
        System.out.println("max is " + max);
        System.out.println("Found " + numPs + " p's in the string.");
    }
}

Open in new window


max is 44
Found 35 p's in the string.


i wonder why max says 44 so 44 is 35 plus 9(ie nine P's) total 44 of all characters without +?


why they used + in the below String
peter piper picked a " + "peck of pickled peppers
just to show String concatenation?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41786552
Probably.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41786558
" + "

Open in new window


even space before and after plus also i should not count to get 44 looks.

Otherwise count coming around 47
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41786559
Print the searchMe string and count the characters. The plus sign and the spaces are not part of the string; they're Java syntax.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41786569
Print the searchMe string and count

right that is max which is 44 .


 for (int i = 0; i < max; i++) {
            // interested only in p's
            if (searchMe.charAt(i) != 'p')
               // continue;

            // process p's
            numPs++;
        }

Open in new window



above code technically  mean

 for (int i = 0; i < max; i++) {
            // interested only in p's
        //    if (searchMe.charAt(i) != 'p')
               // continue;

            // process p's
            numPs++;
        }

Open in new window


as we are checking searchMe.charAt(i) != 'p' but them adding numPs by one as continue not there to skip that particular iteration of for loop
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now