arduino serial input + motion sensor

I have an arduino which is connected to 4 relays and a motion sensor.out of the 4 relays 1 relay switches on/off based on the motion sensor and the other 3 relays are to be controlled via the computer using a serial connection.
I Know how to write code to control the relays based on a serial input given . Like for example 'a' to control relay 1 , 'b' for relay 2 etc. using switch case.
i also know the code to control the 1 relay based on motion sensor. The problem is combining the two codes into 1 code , because the code to control the 3 relays via serial input has the line to wait for serial input from the pc. Now if it keeps waiting for serial input then it would not be possible for arduino to check the motion sensor's PIN for a HIGH or LOW . How do i write the arduino code to constantly keep checking the status of the motion sensor's PIN and also accept the serial input from the pc . Both should not have much delay..

Thanks..

The problem is combining the two codes into 1 code , because the code to control the 3 relays via serial input has the line to wait for serial input from the pc.

Why? The Arduino should do nothing if there is no serial data to read, rather than waiting for serial data to show up.

Check if there is any data available. If so, collect it. If complete, process it.

If no data available, read the other sensor, and do what needs to be done.

thanks for your reply.
as you know if you close the serial connection the arduino resets. So my python script makes sure that the serial connection will remain established all the time. So my code has a while(serial.available()>0) line which means it will always be inside the while loop. and the first line under the while loop is cmd = serial.read() , so wont it wait?or will it keep executing all the lines in the while loop even if there is nothing to read ?

So my code has a while(serial.available()>0) line which means it will always be inside the while loop.

That's not what it means at all. It will remain in the while loop, as long as there is serial data to read. Once all the pending data has been read, or you break out of the loop because an end-of packet marker arrived, the rest of loop() executes.

The arrival of more serial data does not reset the Arduino. Opening the serial port does.

Yes, your code will execute the while and nothing else. ((edit: PaulS comment is more accurate))

So you need to write code that does something like

loop() {
  if (Serial.available()>0) { 
    // Serial.read, digitalWrite  to control the serial controlled relays
  }
  if (analog/digitalRead(motionsensor) ) {
    // digitalWrite to control the other relay
  }
}

This software will alternativly check the serial line and check the motion sensor. The code that does "something" must not contain large delay() or long loops as that stops it alternating.

thank you so much msquare and paulS.
Will try it out and get back here.. I guess i totally misunderstood what those 2 lines of code does..Thanks again...