if else函数用法

用法一

int main() {
    
    int a = 5;
    if( a > 0)
        cout << "a > 0" << endl;
    else if(a > 3 )
        cout << "a > 3" << endl;
    else {
        cout << "else" << endl;
    }
	return 0;
}

打印:
a > 0

用法二

int main() {
    
    int a = 5;
    if( a <= 0)
        cout << "a <= 0" << endl;
    else if(a <= 3 )
        cout << "a <= 3" << endl;
    else {
        cout << "a > 3" << endl;
    }
	return 0;
}

打印:
a > 3

用法三

    if( pFile->InitProtocol (strHost, iPort, strUser, strPasswd, 2 * iTimeout + iActive) )
        std::cout << "\tfail:" << strName << std::endl;
    else if( pFile->List(strPath) )
        std::cout << "\tfail list:" << strName << std::endl;
    else {
        FILE *pfd;
        CUnit cUnit;
        string_x strFile,strNewFile;
        
        strFile = strName + ".log";
        pfd = fopen(strFile.c_str(), "a+");
        if(pfd){
            pFile->sort(3);
            while(pFile->next (cUnit)){
                if(cUnit.m_bDir) continue;
                if( str_match (cUnit.m_strName, strMatch) ) continue;
                strNewFile = renameRules(cUnit.m_strName, strScanf, strRename);
                if(strNewFile.empty() || strNewFile.find("$") != string_x::npos) continue;
                fprintf(pfd, "%lld,%s,%lld,%s\n", cUnit.m_iSize, cUnit.m_strName.c_str(), cUnit.m_iTime, strNewFile.c_str());
            }
            fclose(pfd);
        }
        std::cout << "\tsucc:" << strName << std::endl;
    }

if …else用法总结

if else条件匹配的话,不会再往下匹配。
如果不成立,一直往下匹配。

你可能感兴趣的:(编程技术)