In: Computer Science
write an operational semantic for a while loop in c++
While loop in C++;
while(condition)
{
//block of code,the statements that need to be execute
//any increment conditions:
}
--> First, while loop checks the condition whether it is true or not. if it is true then it go inside of the while loop to execute block of code.otherwise it is get terminated and execute the code after the while loop.
Example:
Sample output:
--> here in this example the integer variable i is initialized to 0 and then in while loop it checks whether the condition is true or not.
--> i<3 here i=0 so,0<3(true) so the block of code inside the while loop will execute and i variable is incremented to 1
again loop checks the condition 1<3(true) so,again it gets inside of the loop and code executed again incremented to 2
now 2<3(true) so again hello world is printed again. and incremented to 3
-->now i=3, 3<3(false) so code inside while loop is node going to execute.so,loop is terminated like this.
-->that's why hello world is printed 3 times only
--