简单掌握C++编程中的while与do-while循环语句使用
While 语句 while ( expression ) statement 备注 // while_statement.cpp #include <string.h> #include <stdio.h> char *trim( char *szSource ) { char *pszEOS = 0; // Set pointer to character before terminating NULL pszEOS = szSource + strlen( szSource ) - 1; // iterate backwards until non '_' is found while( (pszEOS >= szSource) && (*pszEOS == '_') ) *pszEOS-- = ' '; return szSource; } int main() { char szbuf[] = "12345_____"; printf_s("nBefore trim: %s",szbuf); printf_s("nAfter trim: %sn",trim(szbuf)); } 在循环顶部计算终止条件。如果没有尾随下划线,循环不执行。
do statement while ( expression ) ; 备注 // do_while_statement.cpp #include <stdio.h> int main() { int i = 0; do { printf_s("n%d",i++); } while (i < 3); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |