Home > Database > Mysql Tutorial > What are the similarities and differences between the three loop statements While, Loop and Repeat?

What are the similarities and differences between the three loop statements While, Loop and Repeat?

青灯夜游
Release: 2022-12-30 11:13:14
Original
4889 people have browsed it

Same points: cannot be used alone, mainly used in stored procedures and functions FUNCTION. Difference: WHILE judges first and then executes; it executes only when the conditions are met. REPEAT and LOOP are executed first and then judged; the loop ends when the conditions are met. When the condition is false, REPEAT can also be executed once; the other two statements cannot be executed.

What are the similarities and differences between the three loop statements While, Loop and Repeat?

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

1. Overview of MySQL loops

There are three types of loops in MySQL, namely WHILE, REPEAT, LOOP (it is said that there is also goto), which cannot be Used alone, mainly used in stored procedures PROCEDURE and functions FUNCTION.

2. WHILE loop

1. Syntax: WHILE condition DO doSomething END WHILE ;

2. Description: If the condition condition is satisfied, the content of the loop body will be executed. If it is not satisfied, the loop will end. (For example: 2>1, satisfied; 1<2, not satisfied.) ---Judge first, then execute.

3. Example: Create a while loop stored procedure pro_while

DROP PROCEDURE IF EXISTS pro_while ;
CREATE PROCEDURE pro_while()
BEGIN
	 DECLARE  sum INT DEFAULT 0 ;
	 WHILE sum < 100 DO
		INSERT INTO `chapter`.`batch` (`id`, `name`, `age`) 
		VALUES (CONCAT(sum,&#39;&#39;),CONCAT(&#39;while&#39;,sum) , sum);
		SET sum = sum + 1;
   	END WHILE ;
END;
Copy after login

4. Call the stored procedure: CALL pro_while ();

Related recommendations "mysql video tutorial"

3. REPEAT loop

1. Syntax: REPEAT doSomething UNTIL condition END REPEAT ;

2. Description: doSomething first performs relevant operations, and then determines whether the conditions are met. If so, end the loop. (Contrary to WHILE loop) ---Execute first, judge later.

3. Example: Create a repeat loop function fun_repeat

DROP FUNCTION IF EXISTS fun_repeat ;
CREATE FUNCTION fun_repeat() RETURNS INT
BEGIN
	DECLARE  sum INT DEFAULT 1000 ;
		REPEAT 
			INSERT INTO `chapter`.`batch` (`id`, `name`, `age`) 
			VALUES (CONCAT(sum,&#39;&#39;),CONCAT(&#39;repeat&#39;,sum) , sum);
			SET sum = sum + 1;	
		UNTIL sum > 1100 END REPEAT ; -- 满足条件结束循环
	RETURN 1;
END;
Copy after login

4. Execution function: SELECT fun_repeat();

4. LOOP loop

1. Syntax:

loop_name : LOOP
    IF condition THEN
        LEAVE loop_name ;
    END IF;
    doSomething
END LOOP;
Copy after login

2. Description:

  • loop_name is the loop name, customized, keywords are not available.
    • DoSomething related operations.
    • condition If the condition is met, the loop ends. --- Judge first, then execute.

3. Example: Create a loop loop stored procedure pro_loop

DROP PROCEDURE IF EXISTS pro_loop ;
CREATE PROCEDURE pro_loop()
BEGIN
	DECLARE sum int DEFAULT 10000 ;
	loop_sums : LOOP -- 【开始】loop 名字,自定义
		IF sum > 10100 THEN
			LEAVE loop_sums ; -- 满足条件,则结束循环
		END IF; 
		INSERT INTO `chapter`.`batch` (`id`, `name`, `age`) 
		VALUES (CONCAT(sum,&#39;&#39;),CONCAT(&#39;loop&#39;,sum) , sum);
		SET sum = sum + 1;	
	END LOOP ;  -- 【结束】
END ;
Copy after login

4. Call the stored procedure: CALL pro_loop();

##5. Summary

1. WHILE loop, judge first, then execute. When the condition is met, execute, and execute the loop.

2. REPEAT loop,

execute first, then judge. If the condition is met, it will not be executed and the loop will end.

3. LOOP loop, execute first, then judge. If the condition is met, it will not be executed and the loop will end.

4. When the condition is false, the REPEAT loop can also be executed once, similar to the

do {} while (condition); loop in java; while the WHILE and LOOP loops cannot be executed.

For more knowledge about computer programming, please visit:

Introduction to Programming! !

The above is the detailed content of What are the similarities and differences between the three loop statements While, Loop and Repeat?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template