Use of loops with “while”<\/p>\n
A\u00a0while<\/strong>\u00a0loop statement repeatedly executes a target statement as long as a given condition is true.<\/p>\n The syntax of a while loop in C++ is:<\/p>\n Here,\u00a0statement(s)<\/strong>\u00a0may be a single statement or a block of statements. The\u00a0condition<\/strong>\u00a0may be any expression, and true is any non-zero value. The loop iterates while the condition is true.<\/p>\n When the condition becomes false, program control passes to the line immediately following the loop.<\/p>\n Here, key point of the\u00a0while<\/em>\u00a0loop is that the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.<\/p>\n When the above code is compiled and executed, it produces the following result:<\/p>\n Credits:<\/span><\/p>\n <\/a>http:\/ #TC<\/a>1017 #Mastery<\/a>18\u00a0<\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":" Use of loops with “while”<\/p>\n A while<\/strong> loop statement repeatedly executes a target statement as long as a given condition is true.<\/p>\n The syntax of a while loop in C++ is:<\/p>\n Here, statement(s)<\/strong> may be a single statement or a block of statements. The condition<\/strong> may be any expression, and true is any non-zero value. The loop iterates while the condition is true.<\/p>\n When the condition becomes false, program control passes to the line immediately following the loop.<\/p>\n Here, key point of the while<\/em> loop is that the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.<\/p>\n When the above code is compiled and executed, it produces the following result:<\/p>\n Credits:<\/span><\/p>\n <\/a>http:\/\/www.tutorialspoint.com\/cplusplus\/cpp_while_loop.htm<\/a><\/span><\/p>\nSyntax:<\/h2>\n
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;\">while<\/span>(<\/span>condition<\/span>)<\/span>\n{<\/span>\n statement<\/span>(<\/span>s<\/span>);<\/span>\n}<\/span><\/pre>\n
Flow Diagram:<\/h2>\n
Example:<\/h2>\n
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-image: url('http:\/\/www.tutorialspoint.com\/cplusplus\/images\/try-it.jpg') !important; background-attachment: initial !important; background-color: #eeeeee !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: 100% 0%; background-repeat: no-repeat !important;\">#include<\/a><\/span>
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;\">value of a<\/span>:<\/span> 10<\/span>\nvalue of a<\/span>:<\/span> 11<\/span>\nvalue of a<\/span>:<\/span> 12<\/span>\nvalue of a<\/span>:<\/span> 13<\/span>\nvalue of a<\/span>:<\/span> 14<\/span>\nvalue of a<\/span>:<\/span> 15<\/span>\nvalue of a<\/span>:<\/span> 16<\/span>\nvalue of a<\/span>:<\/span> 17<\/span>\nvalue of a<\/span>:<\/span> 18<\/span>\nvuale of a<\/span>:<\/span> 19
<\/span><\/pre>\nSyntax:<\/h2>\n
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;\">while<\/span>(<\/span>condition<\/span>)<\/span>\n{<\/span>\n statement<\/span>(<\/span>s<\/span>);<\/span>\n}<\/span><\/pre>\n
Flow Diagram:<\/h2>\n<\/p>\n
Example:<\/h2>\n
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-image: url('http:\/\/www.tutorialspoint.com\/cplusplus\/images\/try-it.jpg') !important; background-attachment: initial !important; background-color: #eeeeee !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: 100% 0%; background-repeat: no-repeat !important;\">#include<\/a><\/span> <\/span>\nusing<\/span> namespace<\/span> std<\/span>;<\/span>\n \nint<\/span> main <\/span>()<\/span>\n{<\/span>\n \/\/ Local variable declaration:<\/span>\n int<\/span> a <\/span>=<\/span> 10<\/span>;<\/span>\n\n \/\/ while loop execution<\/span>\n while<\/span>(<\/span> a <\/span> 20<\/span> )<\/span>\n {<\/span>\n cout <\/span> \"value of a: \"<\/span> a <\/span> endl<\/span>;<\/span>\n a<\/span>++;<\/span>\n }<\/span>\n \n return<\/span> 0<\/span>;<\/span>\n}<\/span><\/span><\/span><\/span><\/span><\/pre>\n
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;\">value of a<\/span>:<\/span> 10<\/span>\nvalue of a<\/span>:<\/span> 11<\/span>\nvalue of a<\/span>:<\/span> 12<\/span>\nvalue of a<\/span>:<\/span> 13<\/span>\nvalue of a<\/span>:<\/span> 14<\/span>\nvalue of a<\/span>:<\/span> 15<\/span>\nvalue of a<\/span>:<\/span> 16<\/span>\nvalue of a<\/span>:<\/span> 17<\/span>\nvalue of a<\/span>:<\/span> 18<\/span>\nvuale of a<\/span>:<\/span> 19
<\/span><\/pre>\n