Is there an equivalent to the “for … else” Python loop in C++?

前端 未结 14 2088
野的像风
野的像风 2021-01-31 01:21

Python has an interesting for statement which lets you specify an else clause.

In a construct like this one:

for i in foo:
  if         


        
14条回答
  •  猫巷女王i
    2021-01-31 01:57

    If doesn't mind using goto also can be done in following way. This one saves from extra if check and higher scope variable declaration.

    for(int i = 0; i < foo; i++)
         if(bar(i))
             goto m_label;
    baz();
    
    m_label:
    ...
    

提交回复
热议问题