關於while(n--)和while(n)和if(n)的簡單講解~!


其中while(n):


n是bool型變量時,就是代表n為true時運行循環, n是int型時,就是代表不為0時運行循環 n為表達式,就代表表達式成立時運行循環
下面是一個測試例子:
#include<iostream>
using namespace std;
int main()
{
 int n;
 scanf("%d",&n);
 while(n)
 {cout<<n<<endl;
 n--;}
     return 0;
 
}
while(n--)的原型是while(n&&n=n-1)
所以它的使用及可參照while(n)只是加了一個條件!
測試例子:
#include<iostream>
using namespace std;
int main()
{
 int n;
 scanf("%d",&n);
 while(n--)
 cout<<n<<endl;
     return 0;
 
}
此外if(n)也是一個道理!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM