?
Solved

java continue statement

Posted on 2016-09-06
10
Medium Priority
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 1000 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 28

Assisted Solution

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

Open in new window

0
 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
A solution for Fortify Path Manipulation.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Introduction to Processes
Suggested Courses

770 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