C++ Looping structures (for, while, do-while)
In C++, there are three main types of looping structures: for
, while
, and do-while
loops.
The for
loop is a looping structure that executes a block of code repeatedly for a fixed number of times. The general syntax of a for
loop is as follows:
for (initialization; condition; update) { // code to be executed }
In this example, the for
loop will execute the block of code 5 times, with the value of i
starting at 0 and incrementing by 1 on each iteration, until i
is no longer less than 5.
The while
loop is a looping structure that executes a block of code repeatedly while a condition is true. The general syntax of a while
loop is as follows:
while (condition) { // code to be executed }
In this example, the while
loop will execute the block of code repeatedly as long as the value of i
is less than 5, and the value of i
will be incremented by 1 on each iteration.
The do-while
loop is a looping structure that executes a block of code repeatedly at least once, and then continues to execute the block of code while a condition is true. The general syntax of a do-while
loop is as follows:
do { // code to be executed } while (condition);
do-while
loop will execute the block of code at least once, and then continue to execute the block of code while the value of i
is less than 5.
Comments
Post a Comment