how can i use cout<<“hello world”; without using ; at the end of the line

前提是你 提交于 2020-02-16 13:14:54

问题


I want to display hello world message in c++ without using semicolon. how can I do that? Is it possible? I have tried it inside if else block like

if(true) {
cout<<"hello world"

}

but it does not work like that.


回答1:


if (cout << "hello world") {}



回答2:


You can put it as the condition:

if (std::cout << "hello world") {}



回答3:


You can use if, for or while, maybe more.

if(std::cout << "Hello World") { }

while(std::cout << "Hello World" && false) { } // && false to loop only once

for(std::cout << "Hello World"; false;) { } // has semicolons, but not at the end of line ;)

Or put it on the next line:

std::cout << "Hello World 4"\
;      


来源:https://stackoverflow.com/questions/59990046/how-can-i-use-couthello-world-without-using-at-the-end-of-the-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!