returning to the loop

hello, i have a program and i want that the program check that serial is available or not, if some one send 1 or 2 it increments respectively in icounter or ncounter but if someone sends other than 1 or 2 it goes back to the while statement unless someone send the right number in this case 1 or 2. but i don't know how to return? a little help will be appreciated... thanks here is the code.

while (Serial.available() == 0);
          int vals = Serial.read() - '0';
        
          if (vals == 1)
          {
            iCounter++;
            Serial.println("Your vote is successfully casted");
            Serial.println("You have voted for Imran");
            Serial.println();
            Serial.println();
            digitalWrite(ledPin, HIGH);
          }
          else if (vals == 2)
          {
            nCounter++;
            Serial.println("Your vote is successfully casted");
            Serial.println("You have voted for Nawaz");
            Serial.println();
            Serial.println();
            digitalWrite(ledPin, LOW);
          }
          else
          {
            Serial.println("Invalid... Try again");
            Serial.println();
            Serial.println();
          }

if you want to force an exit from a loop, use:

break;

nicoverduin: if you want to force an exit from a loop,

i don't want to exit, i want to return to while loop again so the person can enter the right value if it entered other than 1 or 2 for the first time.

Sorry I did not understand your question. Try this:

while(1) {
   if(Serial.available() != 0) {
          int vals = Serial.read() - '0';
        
          if (vals == 1)
          {
            iCounter++;
            Serial.println("Your vote is successfully casted");
            Serial.println("You have voted for Imran");
            Serial.println();
            Serial.println();
            digitalWrite(ledPin, HIGH);
          }
          else if (vals == 2)
          {
            nCounter++;
            Serial.println("Your vote is successfully casted");
            Serial.println("You have voted for Nawaz");
            Serial.println();
            Serial.println();
            digitalWrite(ledPin, LOW);
          }
          else
          {
            Serial.println("Invalid... Try again");
            Serial.println();
            Serial.println();
          }
    }
}

And if this is the only code in your loop, leave the first and last line out

nicoverduin:
Sorry I did not understand your question.

when i did this it got stuck in infinite loop, i also have other code below this. i want to run the other code if someone enter 1 or 2.
do you think break will work?
here is the complete code.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX

  byte data[5];  //For holding the ID we receive
  int val = 0;
  byte abubakar[5] = {0x48,0x00,0x40,0x65,0x92};
  byte latif[5]  = {0x48,0x00,0x40,0x90,0x76}; 
  int ledPin = 13;


  // Variables will change:
  int iCounter = 0;   // counter for the number of button presses
  int nCounter = 0;   // counter for the number of button presses
  int result; // difference between votes
  int buttonState = 0;         // current state of the button
  int lastButtonState = 0;     // previous state of the button

 void setup()
 {
   // Start serial port 19200 bps
   Serial.begin(19200);
   // set the data rate for the SoftwareSerial port
   mySerial.begin(19200);
   
   pinMode(ledPin, OUTPUT);
   
   // Setting Auto Read Mode - EM4102 Decoded Mode - No password
   // command: FF 01 09 87 01 03 02 00 10 20 30 40 37
   mySerial.write(0xFF);  //Header
   mySerial.write(0x01);  //Reserved
   mySerial.write(0x09);  //Length (Command + Data)
   mySerial.write(0x87);  //Command (0x87 sets auto mode behavior
   mySerial.write(0x01);  //Data 1: Enable Auto-Read
   mySerial.write(0x03);  //Data 2: Mode – Parity decoded – Manchester RF/64
   mySerial.write(0x02);  //Data 3: Total number of block to be read (2)
   mySerial.write((byte)0x00);  //Data 4: No password expected
   mySerial.write(0x10);  //Data 5: Password byte 1
   mySerial.write(0x20);  //Data 6: Password byte 2
   mySerial.write(0x30);  //Data 7: Password byte 3
   mySerial.write(0x40);  //Data 8: Password byte 4
   mySerial.write(0x37);  //Checksum
   
   delay(500);
   while(mySerial.available()>0)
   {
     mySerial.read();
   }
   mySerial.println();
   Serial.println("RFID module started in Auto Read Mode, Waiting for Card...");
 }

 void loop()
 {
   for(int i=0; i<5; i++)
   {
     val = mySerial.read();
     while (val != 0xff)
       {  //On Successful read, first byte will always be 0xFF
          val = mySerial.read();
          delay(1000);
       }
   
       //we already read the header (0xff)
       mySerial.read();              // reserved
       mySerial.read();              // length
       mySerial.read();              // command (indicates tag data)
       data[0] = mySerial.read();    // we read data 1
       data[1] = mySerial.read();    // we read data 2
       data[2] = mySerial.read();    // we read data 3
       data[3] = mySerial.read();    // we read data 4
       data[4] = mySerial.read();    // we read data 5
       mySerial.read();              // checksum
   
       // Indentify RFID Card
       boolean a_card = true;
       boolean l_card = true;
       Serial.print("Card found - Code: ");
       for (int i=0; i<5; i++)
       {
         if (data[i] < 16) Serial.print("0");
         Serial.print(data[i], HEX);
      
         //cross-check
         if (data[i] != abubakar[i]) a_card = false;
         if (data[i] != latif[i]) l_card = false;
       }
       mySerial.println();
   
       if (a_card || l_card)
       {     
          Serial.println();
          Serial.println("Welcome...");
          Serial.println("Please input candidate number you want to vote...");
          Serial.println("For Imran Khan enter 1...");
          Serial.println("For Nawaz Sharif enter 2...");
        
          while(1) {
    
          int vals = Serial.read() - '0';
        
          if (vals == 1)
          {
            iCounter++;
            Serial.println("Your vote is successfully casted");
            Serial.println("You have voted for Imran");
            Serial.println();
            Serial.println();
            digitalWrite(ledPin, HIGH);
          }
          else if (vals == 2)
          {
            nCounter++;
            Serial.println("Your vote is successfully casted");
            Serial.println("You have voted for Nawaz");
            Serial.println();
            Serial.println();
            digitalWrite(ledPin, LOW);
          }
          else
          {
            Serial.println("Invalid... Try again");
            Serial.println();
            Serial.println();
          }
       
     
   }
 }
   }
   if (iCounter > nCounter)
   {
     Serial.println("Imran got more votes than Nawaz");
     Serial.print("Imran got: ");
     Serial.println(iCounter);
     Serial.print("Nawaz got: ");
     Serial.println(nCounter);
     result = iCounter - nCounter;
     Serial.print("Winning margin: ");
     Serial.println(result);
   }
   else if (nCounter > iCounter)
   {
     Serial.println("Nawaz got more votes than Imran");
     Serial.print("Nawaz got: ");
     Serial.println(nCounter);
     Serial.print("Imran got: ");
     Serial.println(iCounter);
     result = nCounter - iCounter;
     Serial.print("Winning margin: ");
     Serial.println(result);
   }
   else
   {
     Serial.println("Draw");
     Serial.print("Imran got: ");
     Serial.println(iCounter);
     Serial.print("Nawaz got: ");
     Serial.println(nCounter);
   }
   while(1) { }
 }

No, it will not. You need to test if you have serial data available.

http://gammon.com.au/serial

@Nick Gammon and @nicoverduin thanks both of you, i figured it out. thanks once again for your help. :)