How to continually print output inside a if else condition?

I have been trying to continually print the PWM output of pin 3 inside the if else condition but it only prints once. Can I continually print it in serial monitor until it meets the else condition? or use a while loop? or a statemachine ?
Here is my code I also have a code with a similar function but it uses switch cases but still it only prints once

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {
    int InitVal = Serial.parseInt();
    int red = Serial.parseInt();

    if (InitVal == 1){
       if (Serial.read() == '\n') {
        analogWrite(redPin, red);
        Serial.println(red);
       }

       else {
        analogWrite(redPin, 0); 
        Serial.println(0);
       }
    }

  }
}

An IF statement only acts once. It is the WHILE statement that causes the repetition. And in your case it updates the value of InitVal so it is no longer 1

You should not be mixing up individual Serial.read() statements and Serial.parseInt() because both of them remove characters from the serial input buffer to the detriment of the other.

And it would probably be better NOT to use WHILE and instead allow loop() to do the repetition which is what it was designed for.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

mentablack232:
int InitVal = Serial.parseInt();
int red = Serial.parseInt();

Are any more characters left to parseInt?

if (InitVal = 1)

Is this an comparison or assignment operation?

if (Serial.read() == '\n')

How are you sending '\n' character from Serial Monitor?

Thank you @Robin2 i'll look at your Serial Input basics Tutorial.

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

This is my code which utilizes switches maybe this could make a difference in order to find a solution for the pwm output to continually print.

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {
    int InitVal = Serial.parseInt();
    int red = Serial.parseInt();

    switch(InitVal) {
      case 1:
        if (Serial.read() == '\n') {
          
          analogWrite(redPin, red);
          Serial.println(red);
       }
        break;
      case 0:
        analogWrite(redPin, 0);
        Serial.println(0);
        break;
       }
    }

  }

GolamMostafa:
Are any more characters left to parseInt?
Is this an comparison or assignment operation?
How are you sending '\n' character from Serial Monitor?

I'am following the tutorial in this website Read ASCII Sringbecause I'am planning to inter-phase this with a GUI . A GUI sends ascii to the arduino reads it then sends the output value to the GUI.

GolamMostafa:
if (InitVal = 1)

yes it is a comparison operation, the Gui sends a string example [ 1,103 ]
1 = for the conditions and 103 is the actual value to send

yes it is a comparison operation, the Gui sends a string example [ 1,103 ]
1 = for the conditions and 103 is the actual value to send

but

if (InitVal = 1)

is not a comparison. It assigns the value of 1 to InitVal

UKHeliBob:
but

if (InitVal = 1)

is not a comparison. It assigns the value of 1 to InitVal

Im sorry for the typo it is if (InitVal == 1)