tanman
tanman

Reputation: 175

How to run if statement 3 consecutive times?

I want a 1 to be printed if a>m three consecutive times. I have tried running it through a for loop.

for(int i = 0; i<message.length; i+=3){
  if(a>m){
    System.out.println("1");
  }
}

I want the program to stop right when it hits the first set where a>m three consecutive times.

Upvotes: 1

Views: 2499

Answers (9)

Nitin Dandriyal
Nitin Dandriyal

Reputation: 1607

Add a counter variable that increments is a > m and resets otherwise

int ctr = 0;
for (int i = 0; i < message.length; i++) {
    ctr = a > m ? ctr + 1 : 0;
    if (ctr == 3) break;        
}

Just curious to know how are a and m getting modified, they have to modified inside that loop, otherwise this code doesn't make any sense

Upvotes: 0

Chamatake-san
Chamatake-san

Reputation: 551

There are also options that don't involve loops. Maybe not as pretty, but they may be easier to understand if you don't have much experience with loops.

if(a>m){
    System.out.println("111");
  }

or

if(a>m){
    System.out.println("1" + "\n" + "1" + "\n" + "1");
  }

or

 if(a>m){
    System.out.println("1");
    System.out.println("1");
    System.out.println("1");
  }

It kind of depends on what requirements you have to meet.

Upvotes: -1

Razib
Razib

Reputation: 11173

Try this -

int count1 =0, count2=0, count3=0;

for(int i = 0; i<message.length; i+=3){
  if(a>m){
    System.out.println("1");

    if(count1==0){
       count1++;
       count2=0;
       count3=0;
    }

    else if(count1==1){
       count2++;
       count3=0;
    }

    else if(count1==1 && count2==1){
       count3++;
    } 

    else{
       count1=0;
       count2=0;
       count3=0;

    }





  }

 if(count1==1 && count2 ==1 && count3==1){
    break;
 }
}

Upvotes: 0

sri hs
sri hs

Reputation: 116

int count = 0;
for(int i = 0; i<message.length; i+=3) {
  if(a>m) {
    System.out.println("1");
    count++;
    if(count == 3)
      break;
  }
}

Upvotes: 0

for(int i = 0; i < 3; i++){
      if(a > m){
         // 1 will be printed for 3 times and no more provided 'a > m'
         System.out.println("1");
      } else {
         break;
      }
}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

Add a counter of consecutive hits, which is initially set to zero. Increment it each time you get a hit. Once the counter reaches three, break out of the loop. Otherwise, continue looping:

int consecutiveHits = 0;
for (...) {
    if (a > m) {
        ...
        consecutiveHits++;
        if (consecutiveHits == 3) {
            break;
        }
    } else {
        consecutiveHits = 0;
    }
}

Upvotes: 3

Michael Collins
Michael Collins

Reputation: 380

Try using an index and a while loop like this. Everytime the if statement hits its counted by I. When the if statement hits 3 times the loop stops...

int i = 0

while(i < 3){
if(a>m){
    System.out.println("1");
    i++;
}
}

Upvotes: 0

A. Abramov
A. Abramov

Reputation: 1865

This means you want to break out of the loop. There's 2 main ways to do that:

Using the break keyword:

Java includes a keyword for breaking out of loops as well as keyword for skipping certain parts of loops. If you want to break out of the loop if a certain term applies - you can just use it - break; - it will stop the loop on the spot. Example:

for(int i = 0; i<message.length; i+=3){
 if(a>m){
  System.out.println("1");
  break; // you go out of the loop-
  }
}
// and the code goes on from here.

Using a flag:

You can use a boolean value to track how the loop goes each time in the term part. This way, you can track it and go out of the loop if you need to. Example:

Boolean myFlag = true;
for(int i = 0; i<message.length && myFlag ; i+=3){ // notice how the term changed.
 if(a>m){
  System.out.println("1");
  myFlag = false; // The term will no longer fullfill and you will go out of the loop.
  }
}

Upvotes: 0

augray
augray

Reputation: 3161

You seem to be improperly using your for syntax if you just want the '1' printed three times. Instead of your incrementing statement being i+=3, you probably want it to be i++. And for the comparison, you want i<3, not i<message.length. So this is probably what you want:

for(int i = 0; i<3; i++){ //go through the loop three times. Print 1 in the loop only if a>m.
    if(a>m){
        System.out.println("1");
    }
}

See the syntax on the for loop here.

It executes the third statement every time through the loop, so with i initialized to 0 and the statement i+=3 at the end, the first time you go through the loop i will be 0, next time it will be 3, then 6, then 9, etc. until your condition becomes false (as you've written it, it will go until i>=message.length, so if message.length was 3, you would only go through the loop one time as you've written your code).

Upvotes: 0

Related Questions