Vehicular fog and cloud computing (VFCC) system, which provides huge computing power for processing numerous computation-intensive and delay sensitive tasks, is envisioned as an enabler for intelligent connected vehicles (ICVs). Although previous works have studied the optimal offloading scheme in the VFCC system, no existing work has considered the departure of vehicles that are processing tasks, i.e., the occupied vehicles. However, vehicles leaving the system with uncompleted tasks will affect the overall performance of the system. To solve the problem, in this paper, we study the optimal offloading scheme that considers the departure of occupied vehicles. We first formulate the task offloading problem as an semi-Markov decision process (SMDP). Then we design the value iteration algorithm for the SMDP to maximize the total long-term reward of the VFCC system. Finally, the numerical results demenstrate that the proposed offloading scheme can achieve higher system reward than the greedy scheme. INDEX TERMS Vehicular fog computing, cloud computing, task offloading, semi-Markov decision process.