C++忽略变量并阻止输出

Sam*_*nna 1 c++

我有2个变量,一个值为5,另一个值为0.如果我有:

cout << var1;      //the one with a value of 5
cout << var2;      //the one with a value of 0
Run Code Online (Sandbox Code Playgroud)

有没有办法让第二个变量在代码中的那个点上的值为0时不打印任何内容?

Mys*_*ial 5

只需使用if语句:

cout << var1;
if (var2 != 0)
     cout << var2;
Run Code Online (Sandbox Code Playgroud)