title: Difference between using ternary operator and if condition
body: <p>I was solving a code challenge (covert a number from decimal to binary and then print the consecutive # of 1s in a binary # ) on HackerRank.com and I wrote the following code below to solve the challenge (see below). </p>
  
  <pre><code>public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      int n = in.nextInt();
      String binaryNum = "";
      int count = 0, maxCount = 0; 
      StringBuilder binary = new StringBuilder(); 
      while(n != 0){
          binaryNum = Integer.toString(n % 2);
          binary.append(binaryNum);
          n /= 2; 
      }
      binary = binary.reverse();
      System.out.println(binary);
      for(int i = 0; i &lt; binary.length(); i++){
        if(binary.charAt(i) == '1'){
              count++; 
              System.out.print("" + count);
          } if(count &gt; maxCount) {
              maxCount = count;
          }    
       else {
           count = 0; 
          }
      }
      System.out.print(maxCount);
  }
  </code></pre>
  
  <p>With the code above the count variable would not increment when n = 439(110110111). It would print 1211111 instead. As result, maxCount was 2 instead 3. However, when I used the following code below the count variable incremented as expected when n=439(120120123). As result, maxCount was equal to 3. </p>
  
  <pre><code>public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
          int n = in.nextInt();
          String binaryNum = "";
          int count = 0, maxCount = 0; 
          StringBuilder binary = new StringBuilder(); 
          while(n != 0){
              binaryNum = Integer.toString(n % 2);
              binary.append(binaryNum);
              n /= 2; 
          }
          binary = binary.reverse();
          System.out.println(binary);
  
          for(int i = 0; i &lt; binary.length(); i++){
                  count = (binary.charAt(i) == '1') ? count + 1 : 0;
                  System.out.print("" + count); 
               if(count &gt; maxCount) {
                  maxCount = count;
              }    
          }
          System.out.print(maxCount);
      }
  </code></pre>
  
  <p>I would like to understand why the output of count is different when I used the first code option vs. the second code option. Thanks for the help.  </p>