Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Counting the number of statement in java code

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Counting the number of statement in java code

    Hello folks,

    I have started learning java, and this might be a really easy question, is there any a correct way to count the number of statements in Java code and to count the number of statements which have been executed?

    I am studying about 'statement coverage' and want to understand this concept.

    For example how may statements are there in the following code?

    Printsum (Int a, Int b) { ------------ Printsum is a function
    Int result = a+ b;
    If (result> 0)
    Print ("Positive", result)
    Else
    Print ("Negative", result)
    }

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Counting the number of statement in java code

    Can you provide some example ? What did you mean by number of statements?
    Whatever you are, be a good one

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Counting the number of statement in java code

    Before you can count statements you need a definition for what is a statement.
    Can you post your definition for what is a statement?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Mar 2018
    Posts
    10
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Counting the number of statement in java code

    This is how I would count the number of statements:

    1). Printsum (Int a, Int b) { ------------ Printsum is a function
    2). Int result = a+ b;
    3). If (result> 0)
    4). Print ("Positive", result)
    Else
    5). Print ("Negative", result)
    }

    Some statements are in turn made up of other sub statements. So while the "If Else" is a statement it is also made up of the sub statements numbers 4 and 5.

    Do a search for "Java railroad diagram". It will describe what to count as statements. As for automatically counting the number of statements in a class you might try using the "abstract syntax tree" for that class but I'm not 100% sure it will give you exactly what you are looking for.

  5. #5
    Member
    Join Date
    Aug 2017
    Location
    Northern Ireland
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Counting the number of statement in java code

    There are existing tools available for that. For example JaCoCo and EclEmma.
    Tim Driven Development

Similar Threads

  1. counting number of paragraphs in a text
    By lovejava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 19th, 2014, 03:49 PM
  2. Sorting Arrays and Counting the Number of Swaps
    By AThompsonCO in forum What's Wrong With My Code?
    Replies: 10
    Last Post: April 13th, 2014, 02:20 PM
  3. Number Counting Array issues
    By compileDammit in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 25th, 2013, 08:12 PM
  4. Replies: 2
    Last Post: June 4th, 2013, 08:09 PM
  5. [SOLVED] counting number of comparisons in merge sort
    By mia_tech in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 26th, 2012, 11:54 PM

Tags for this Thread