Self-cultivation for C++ lovers (2): if-else branch statement

Today, the editor will share the if-else branch statement of C++

1. if single branch statement

Format:

if (judgment statement)

{

execute statement;

}

For example:

if (a == b){
    cout<<"hello";
}

If a=b, output "hello"

2. if-else double branch statement

Format:

if (judgment statement)

{

execute statement;

}

else

{

execute statement;

}

For example:

if (a == b){
    cout<<"hello";
}
else{
    cout<<"hi";
}

If a=b, output "hello", otherwise output "hi"

2. if-else multi-branch statement

Format:

if (judgment statement 1)

{

Execute statement 1;

}

else if (judgment statement 2)

{

Execute statement 2;

}

……

else

{

Execute statement m;

}

For example:

if (a == b){
    cout<<"hello";
}
else if (a == c){
    cout<<"bye";
}
else if (a == d){
    cout<<"good morning";
}
else{
    cout<<"hi";
}

If a=b, output "hello", if a=c, output "bye", if a = d, output "good morning", otherwise output "hi"

The above is the knowledge sharing~

Remember to like it!

Guess you like

Origin blog.csdn.net/pyz258/article/details/129390500