{"id":7606,"date":"2020-05-02T09:02:11","date_gmt":"2020-05-02T09:02:11","guid":{"rendered":"http:\/\/ebhor.com\/?p=7606"},"modified":"2020-08-22T07:58:16","modified_gmt":"2020-08-22T07:58:16","slug":"looping-statements-in-cpp","status":"publish","type":"post","link":"https:\/\/www.ebhor.com\/looping-statements-in-cpp\/","title":{"rendered":"Looping Statements in C++"},"content":{"rendered":"\n
In\na programming often a situation may comes where we want to execute the set of code again and again. <\/p>\n\n\n\n
Loop has a ability to repeat set of statement until a condition to be satisfied or a particular number of times.<\/p>\n\n\n\n
There are following types of\u00a0 loop in C++ \u00a0language:<\/p>\n\n\n\n
In\na C++ programming language we can also define a loop within another loop. <\/p>\n\n\n\n
while loop<\/strong><\/p>\n\n\n\n \u201cwhile loop\u201d repeats a set of statements if condition is true.<\/p>\n\n\n\n If condition is false the flow of control do not enter inside of while loop. <\/p>\n\n\n\n It first tests the condition then execute the body of loop.<\/p>\n\n\n\n Syntax of a while loop \u2212<\/strong><\/p>\n\n\n\n Example: Write a program to print 1 to 10 using while loop.<\/strong><\/p>\n\n\n\n Output:<\/strong> <\/p>\n\n\n\n Description: In the above program, line \u201cwhile( a <= 10 )\u201d will check if condition is true or false.<\/p>\n\n\n\n In the above case when a=1, condition is true and flow of control move inside of while and print 1 after that increment occurs. <\/p>\n\n\n\n Now a=2, again condition is true and flow of\ncontrol move inside of while and print 2 after that increment occurs.<\/p>\n\n\n\n Now a=3, again condition is true and flow of\ncontrol move inside of while and print 3 after that increment occurs.<\/p>\n\n\n\n This process is continue until condition is false(in this case condition false occur when a=11). <\/p>\n\n\n\n Example: Write a program to print 10 to 1 using while loop.<\/strong><\/p>\n\n\n\n Output<\/p>\n\n\n\n like a while loop,\u00a0 \u201c do while loop\u201d repeats a set of statements if condition is true. <\/p>\n\n\n\n If condition is false then the flow of control do not enter inside the do-while loop. <\/p>\n\n\n\n \u201cdo-while\u201d first execute the body of loop even if\nthe condition is false than tests the condition.<\/p>\n\n\n\n Means even if the condition if false do-while\nexecutes at least ones. <\/p>\n\n\n\n The syntax of a do...while<\/strong> loop in C programming language is \u2212<\/p>\n\n\n\n Example: Write a program to print 1 to 10 using while loop.<\/strong><\/p>\n\n\n\n Output<\/p>\n\n\n\n Even if the condition if false do-while executes at least ones. <\/strong><\/p>\n\n\n\n Output<\/p>\n\n\n\n Description:<\/strong> in the above program do-while executes ones even condition of is false. <\/p>\n\n\n\n Because do-while first executes the statement then check the condition.\u00a0<\/p>\n\n\n\n \u201cfor-loop\u201d repeat the set of statement until a particular condition to be satisfied or a particular number of times.<\/p>\n\n\n\n Syntax of for-loop:<\/p>\n\n\n\n \u201cfor-loop\u201d has a initialization , condition and iteration (increment\/decrement) part separated by semicolon. <\/p>\n\n\n\n Initialization<\/strong> : This phase allows you to initialize loop\ncontrol variables.<\/p>\n\n\n\n Condition : <\/strong>If condition\u00a0of for-loop is true, then the body of the loop will execute. <\/p>\n\n\n\n And the body of the loop does not execute if the condition is false, and the flow of control jumps to the next statement just after the 'for' loop<\/p>\n\n\n\n Iteration phase:<\/strong> This statement allows you to update(increment\/decrement any loop control variables..<\/p>\n\n\n\n Example: Write a program to print 1 to 10 using for-loop.<\/strong><\/p>\n\n\n\n Output<\/p>\n\n\n\n In a for loop either initialization or iteration , or both may be absent but condition must be present in a for loop.<\/p>\n\n\n\n Output<\/p>\n\n\n\n Example: Write a program to take a number from user and find its factorial.<\/strong><\/p>\n\n\n\n Output<\/p>\n\n\n\n Example:\nWrite a program to take a number from user and <\/strong>check\nwhether a number (entered by the user) is a prime number or not.<\/strong><\/p>\n\n\n\n Prime Number\n: <\/strong>prime\nnumber is a number which satisfies the following\nconditions.<\/strong><\/p>\n\n\n\n Example for prime\nnumbers:<\/strong> 2, 3, 5, 7, 11, 13, 17, 19, 23,29 etc. because\nthis numbers is only divided by 1 and the number itself.<\/p>\n\n\n\n Number\u00a0 4, 6, 8, 9, 10, 12, 14, 15, 16 etc are not prime numbers. <\/strong>Because, the number 4 can be divided by 2.\u00a0<\/p>\n\n\n\n As per the rule of prime number,\u00a0 should be divide 2 numbers only. <\/p>\n\n\n\n They are 1 and the number itself.\u00a0 But, number 4 is also divided by 2. Similarly , all remaining numbers 6, 8, 9, 10, 12, 14\u2026\u2026..\u00a0 also divided by a number other than 1 and the number itself. <\/p>\n\n\n\n Therefore these are not a\u00a0 prime numbers<\/strong>.<\/p>\n\n\n\n Number 1 is neither a prime nor a composite number.<\/p>\n\n\n\n Output<\/p>\n\n\n\n In a programming often a situation may comes where we want to execute the set of code again and again. Loop has a ability to repeat set of statement until a condition to be satisfied or a particular number of times. There are following types of\u00a0 loop in C++ \u00a0language: In a C++ programming language … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[329],"tags":[],"yoast_head":"\nwhile(condition) \n{\n Statement1; \n statement2;\n ----\n ----\n\nIncrement\/Decrement; \n}\n<\/pre>\n\n\n\n
\n#include
1 2 3 4 5 6 7 8 9 10 <\/pre>\n\n\n\n
\n#include
10 9 8 7 6 5 4 3 2 1 <\/pre>\n\n\n\n
do-while\nloop <\/h3>\n\n\n\n
do {\n Statement1; \n statement2;\n ----\n ----\n\nIncrement\/Decrement; \n} while( condition );\n<\/pre>\n\n\n\n
\n#include
1 2 3 4 5 6 7 8 9 10 <\/pre>\n\n\n\n
\n#include
100\n<\/pre>\n\n\n\n
For Looping Statement<\/h3>\n\n\n\n
for(initialization; condition; iteration ) \t\t\t\t\t\t {\tstatement 1;\n\tstatement 2;\n\t--------------\n}\n<\/pre>\n\n\n\n
\n#include
1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n<\/pre>\n\n\n\n
\n#include
1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n<\/pre>\n\n\n\n
\n#include
Enter a number to calculate its factorial\n5\nFactorial = 120\n\n<\/pre>\n\n\n\n
\n#include
Enter a number to check Whether a Number is Prime or Not:\n7\t\t\t\t\/\/ enter by user \n Entered number is a prime number.\n<\/pre>\n","protected":false},"excerpt":{"rendered":"