{"id":151,"date":"2019-09-24T07:54:22","date_gmt":"2019-09-24T07:54:22","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=151"},"modified":"2019-09-24T08:03:46","modified_gmt":"2019-09-24T08:03:46","slug":"while-loop","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2019\/09\/24\/while-loop\/","title":{"rendered":"while Loop"},"content":{"rendered":"\n<h2>Overview of&nbsp;<code>WHILE<\/code>&nbsp;statement<\/h2>\n\n\n\n<p>The&nbsp;<code>WHILE<\/code>&nbsp;statement is a control-flow statement that allows you to execute a statement block repeatedly as long as a specified is&nbsp;<code>TRUE<\/code>.<\/p>\n\n\n\n<p>The following illustrates the syntax of the&nbsp;<code>WHILE<\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12<\/td><td>WHILE Boolean_expression&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; { sql_statement | statement_block}&nbsp;&nbsp;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this syntax:<\/p>\n\n\n\n<p>First, the&nbsp;<code>Boolean_expression<\/code>&nbsp;is an expression that evaluates to&nbsp;<code>TRUE<\/code>&nbsp;or&nbsp;<code>FALSE<\/code>.<\/p>\n\n\n\n<p>Second,&nbsp;<code>sql_statement | statement_block<\/code>&nbsp;is any Transact-SQL statement or a set of Transact-SQL statements. A statement block is defined using the&nbsp;<code>BEGIN...END<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>If the&nbsp;<code>Boolean_expression<\/code>&nbsp;evaluates to&nbsp;<code>FALSE<\/code>&nbsp;when entering the loop, no statement inside the&nbsp;<code>WHILE<\/code>&nbsp;loop will be executed.<\/p>\n\n\n\n<p>Inside the&nbsp;<code>WHILE<\/code>&nbsp;loop, you must change some values to make the&nbsp;<code>Boolean_expression<\/code>&nbsp;returns&nbsp;<code>FALSE<\/code>&nbsp;at some points. Otherwise, you will have an indefinite loop.<\/p>\n\n\n\n<p>Note that if the&nbsp;<code>Boolean_expression<\/code>&nbsp;contains a&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\">SELECT<\/a><\/code>&nbsp;statement, it must be enclosed in parentheses.<\/p>\n\n\n\n<p>To exit the current iteration of the loop immediately, you use the&nbsp;<code>BREAK<\/code>&nbsp;statement. To skip the current iteration of the loop and start the new one, you use the&nbsp;<code>CONTINUE<\/code>&nbsp;statement.<\/p>\n\n\n\n<h2>SQL Server&nbsp;<code>WHILE<\/code>&nbsp;example<\/h2>\n\n\n\n<p>Let\u2019s take an&nbsp;example of using the SQL Server&nbsp;<code>WHILE<\/code>&nbsp;statement to understand it better.<\/p>\n\n\n\n<p>The following example illustrates how to use the&nbsp;<code>WHILE<\/code>&nbsp;statement to print out numbers from 1 to 5:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567<\/td><td>DECLARE @counter INT = 1;&nbsp;WHILE @counter &lt;= 5BEGIN&nbsp;&nbsp;&nbsp;&nbsp;PRINT @counter;&nbsp;&nbsp;&nbsp;&nbsp;SET @counter = @counter + 1;END<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<p>First, we declared the&nbsp;<code>@counter<\/code>&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/\">variable<\/a>&nbsp;and set its value to one.<\/p>\n\n\n\n<p>Then, in the condition of the&nbsp;<code>WHILE<\/code>&nbsp;statement, we checked if the&nbsp;<code>@counter<\/code>is less than or equal to five. If it was not, we printed out the&nbsp;<code>@counter<\/code>&nbsp;and increased its value by one. After five iterations, the&nbsp;<code>@counter<\/code>&nbsp;is 6 which caused the condition of the&nbsp;<code>WHILE<\/code>&nbsp;clause evaluates to&nbsp;<code>FALSE<\/code>, the loop stopped.<\/p>\n\n\n\n<p>The following shows the output:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12345<\/td><td>12345<\/td><\/tr><\/tbody><\/table>\n","protected":false},"excerpt":{"rendered":"<p>Overview of&nbsp;WHILE&nbsp;statement The&nbsp;WHILE&nbsp;statement is a control-flow statement that allows you to execute a statement block repeatedly as long as a specified is&nbsp;TRUE. The following illustrates the syntax of the&nbsp;WHILE&nbsp;statement: 12 WHILE Boolean_expression&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; { sql_statement&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,18,19],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/151"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":152,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/151\/revisions\/152"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}