As a child growing up in Snowflake, Ariz., a town that his Mormon pioneer great-great-grandfather helped found in the 1870s, Senator Jeff Flake learned to sing a popular children’s hymn, “Choose the Right.”

He had no trouble recalling the hymn’s words on the telephone Wednesday, a day after he took to the floor of the Senate to deliver a stinging rebuke to his party and president, and to announce that he would not run for re-election in 2018.

His decision was political and pragmatic, he acknowledged: he faced a tough primary battle and trailed in the polls. But his revulsion at President Trump also appeared to reflect his Mormon faith. It is a faith that puts a premium on decorum and comity, one that was born in America but is increasingly international and multicultural, and one whose young people often wear rings engraved “CTR” as a reminder of the hymn, which begins, “Choose the right when a choice is placed before you.”

Mormons are also accustomed to testifying in public at monthly meetings, sometimes offering passionate stories of personal revelation.

“We are taught that we ought to stand up for what we know is right, and also to be decent,” Mr. Flake said in the telephone interview. “I hope I’m acting on the faith that I believe in.”

Mr. Flake came out early in the presidential primaries as an opponent of Mr. Trump, and unlike many in his party, he has remained a vocal critic, despite representing a state where the president is still popular. Although he has generally voted with Mr. Trump and the Republican majority in the Senate, he chastised his party on Tuesday for acquiescing in the lying and divisiveness that he said had come from the White House.

“Reckless, outrageous and undignified behavior has become excused and countenanced as ‘telling it like it is,’ when it is actually just reckless, outrageous and undignified,” Mr. Flake said in his 17-minute floor speech. “And when such behavior emanates from the top of our government, it is something else. It is dangerous to a democracy.”

Steve Evans, a Salt Lake City lawyer who writes about Mormon topics on the website By Common Consent, said it was no coincidence that members of the Church of Jesus Christ of Latter-day Saints like Mr. Flake and Mitt Romney would be among “the major headliners of anti-Trumpism.”

“This is borne out of a strong sense of personal morality, but also out of a cultural sense of decorum,” Mr. Evans said. “Mormons are prudes both privately and publicly. But there is also strong scriptural teaching behind it all. The Book of Mormon warns the reader that America is a choice land, that we must be careful in choosing our leaders, and the judgments of God can come on a people that choose evil leaders. Religious teachings like these may be informing their worldview.”

Video

Senator Jeff Flake of Arizona said he would be leaving the Senate at the end of his term because to stay would cause him to compromise too many of his principles.Published OnOct. 24, 2017CreditCreditAl Drago for The New York Times

Mr. Trump’s candidacy for president ran into considerable antipathy in heavily Mormon Utah, where Republican candidates usually coast to victory. Evan McMullin, a Mormon and a former C.I.A. officer and policy director for House Republicans who ran as an independent in 2016, received 21 percent of the vote in the state. Hillary Clinton, the Democratic candidate, won 27 percent, so altogether, more Utahans voted against Mr. Trump (48 percent) than for him (45 percent).

Max Perry Mueller, an assistant professor of American religion at the University of Nebraska-Lincoln, said he heard so many religious overtones in Mr. Flake’s speech that he plans to set aside his next planned lesson for the American religious history class he teaches, and instead have his students deconstruct the senator’s remarks “as a Mormon speech.”

Professor Mueller said the speech reminded him of the cadence, tone and themes that Mormon leaders often use when addressing the church’s vast general conference meetings in Salt Lake City, calling on members to refuse to accommodate the immorality of the larger world.

“That speech reflects a Mormon understanding of human agency and participation in history, that humans bring about change, and move the world towards perfection,” said Professor Mueller, the author of “Race and the Making of the Mormon People.”

In one passage near the end of his speech, Mr. Flake said: “This spell will eventually break. That is my belief. We will return to ourselves once more, and I say, the sooner the better. Because we have a healthy government, we must also have healthy and functioning parties. We must respect each other again in an atmosphere of shared facts and shared values, comity and good faith. We must argue our positions fervently and never be afraid to compromise.”

In the telephone interview, Mr. Flake spoke of his deep involvement with his church, of serving as a missionary in South Africa and Zimbabwe in the 1980s, and of rarely missing a Sunday service with his family in Mesa, Ariz., over his 17 years in Congress.

But he emphasized that he did not want to imply that he received any direction in his political choices from his church or its leaders. Doug Andersen, a spokesman for the church, said it had a longstanding policy of political neutrality and would make no comment.

For the note of optimism that he struck at the end of his floor speech, Mr. Flake said he drew on a family motto that his parents had posted on the refrigerator at home: “Assume the best, always look for the good.”

“That certainly is informed by our faith,” he said, “and who we ought to be.”

He said he had to remind himself that in the end, what will matter more than his status or his job will be choosing what is right, as the hymn says.

“That’s what I’ve tried to do,” he said. “I don’t always succeed.”

A version of this article appears in print on , on Page A17 of the New York edition with the headline: Guidance for Senator In a Children’s Hymn. Order Reprints | Today’s Paper | Subscribe