In sentencing a 19-year-old who pleaded guilty to statutory rape last week, a judge in Idaho made it clear his punishment would include an extra wrinkle: government-mandated celibacy.

The unusual proclamation by Judge Randy Stoker of the Fifth District of Idaho that abstinence would be a condition of probation appears to be based at least partly on an archaic, rarely enforced state law that forbids premarital sex.

But unless the law is successfully challenged, a consensual sexual encounter could prompt a prison stay for Cody Herrera, who was 18 when he sexually assaulted a 14-year-old girl.

Mr. Herrera was sentenced last week to five to 15 years in prison, but the sentence was suspended in favor of a rehabilitation program of sorts that serves as a middle ground between probation and prison time. Based on Mr. Herrera’s success in the program, a judge could opt to release him on probation or send him to prison to serve the original sentence. The program usually takes about six months.