Use of loops with “for”<\/p>\n
A\u00a0for<\/strong>\u00a0loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.<\/p>\n The syntax of a for loop in C++ is:<\/p>\n Here is the flow of control in a for loop:<\/p>\n The\u00a0init<\/strong>\u00a0step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.<\/p>\n<\/li>\n Next, the\u00a0condition<\/strong>\u00a0is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.<\/p>\n<\/li>\n After the body of the for loop executes, the flow of control jumps back up to theincrement<\/strong>\u00a0statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.<\/p>\n<\/li>\n The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.<\/p>\n<\/li>\n<\/ul>\n When the above code is compiled and executed, it produces the following result:<\/p>\n Credits:<\/p>\n <\/a>http:\/ #TC<\/a>1017 #Mastery<\/a>20<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":" Use of loops with “for”<\/p>\n A for<\/strong> loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.<\/p>\n The syntax of a for loop in C++ is:<\/p>\n Here is the flow of control in a for loop:<\/p>\n The init<\/strong> step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.<\/p>\n<\/li>\n Next, the condition<\/strong> is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.<\/p>\n<\/li>\n After the body of the for loop executes, the flow of control jumps back up to theincrement<\/strong> statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.<\/p>\n<\/li>\n The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.<\/p>\n<\/li>\n<\/ul>\n When the above code is compiled and executed, it produces the following result:<\/p>\n Credits:<\/p>\n <\/a>http:\/\/www.tutorialspoint.com\/cplusplus\/cpp_for_loop.htm<\/a><\/p>\nSyntax:<\/h2>\n
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;\">for<\/span> (<\/span> init<\/span>;<\/span> condition<\/span>;<\/span> increment <\/span>)<\/span>\n{<\/span>\n statement<\/span>(<\/span>s<\/span>);<\/span>\n}<\/span><\/pre>\n
\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>\nvalue of a<\/span>:<\/span> 19<\/span><\/pre>\n
Syntax:<\/h2>\n
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;\">for<\/span> (<\/span> init<\/span>;<\/span> condition<\/span>;<\/span> increment <\/span>)<\/span>\n{<\/span>\n statement<\/span>(<\/span>s<\/span>);<\/span>\n}<\/span><\/pre>\n
\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 \/\/ for loop execution<\/span>\n for<\/span>(<\/span> int<\/span> a <\/span>=<\/span> 10<\/span>;<\/span> a <\/span> 20<\/span>;<\/span> a <\/span>=<\/span> a <\/span>+<\/span> 1<\/span> )<\/span>\n {<\/span>\n cout <\/span> \"value of a: \"<\/span> a <\/span> endl<\/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>\nvalue of a<\/span>:<\/span> 19<\/span><\/pre>\n