POMPANO BEACH, Fla. — President Trump made an unannounced visit on Friday to a hospital here to console victims of a school shooting in nearby Parkland, Fla., one of the deadliest mass shootings of his presidency, before meeting with law enforcement officials.

“It’s very sad something like that could happen,” a solemn Mr. Trump told reporters who had gathered at the hospital, Broward Health North. In his public remarks, the president focused on praising law enforcement officials and doctors; he hailed the speed of emergency medical workers as “record setting.”

The president’s visit came as students at schools nationwide were on edge, and as an intense debate waged among lawmakers and public officials about gun restrictions.

Criticism has also been directed at Mr. Trump, who has so far focused on the mental health of the suspect, Nikolas Cruz, rather than on Mr. Cruz’s access to an AR-15-style rifle that he had legally obtained at a gun shop.