Looping statements in Linux

Source: Internet
Author: User

Linux is a free-to-use and free-spread UNIX-like operating system and an open-source operating system. An important feature of this is that the command-line interface and the Shell,shell environment enable users to interact with the core functions of the operating system.

The term script is more about this environment, and scripting is usually a programming language based on some kind of interpreter. Bash is now the default shell environment for most Linux systems.

The programming style in programming is divided into two types:

(1) Procedural programming: It is a command-centric, data-serving instruction

(2) Object-oriented programming: It is a data-centric, Directive service to an object

The shell program is a procedural interpreter that provides the ability to program and then interprets the execution

There are several features of program programming:

A: Sequential execution

B: Cyclic execution

C: Select Execute

Today I'm going to talk about the loop execution. Loop execution is performed repeatedly by a given loop condition until the loop condition is no longer satisfied. The focus here is on the inner loop body. The loop body includes the loop entry condition and the loop exit condition, which must be met at the same time, otherwise it will not go inside the loop, or it cannot loop, causing the system to crash.

Loops are: For, while, until

For loop: The element in the loop list is assigned to the variable, and each assignment executes a loop, and done marks the end of a loop. Until the elements in the list are exhausted, end the entire loop

Let's look at A For loop:

Its main purpose is to print a 99 multiplication table. In this loop we need to specify 1-9 of these nine variable elements for the variables inside. Variables refer to these elements and enter a loop, and a 99 multiplication table is printed out using a looping statement.

      

When we execute this script, let's look at the results of its execution.

             

A perfect multiplication table is printed out using loops.

Of course, a looping statement cannot be used only with one syntax, and these syntaxes can be nested within each other, triggering an internal loop every time the external loop executes.

Then we'll use the For statement to add another statement to see if we can do it. This time I will print a color isosceles triangle! The script here can set the height of the triangle, I will set it to 10 layer bar!

      

The result after the script is run;

      

Therefore, loop statements can be nested with each other. This nesting makes it possible to program shell scripts in a variety of colorful and varied

It says the For loop, and the following describes the while, until loop. These two sequential ways can be said to be similar, can also say the opposite.

While loop: loops when the condition is satisfied, and exits the loop until the condition is not satisfied

Until loop: The condition does not satisfy the loop, and exits the loop until the condition is met

The loop statements of the two loops can be similar, but the loop condition of the judgment is reversed. The same loops get the same results. Then use the while loop to print a chess board to see the use of the while!

      

The result after running the script;

      

Here we introduce the while loop, the until loop is the opposite of its judging condition, just need to put the while inside of less than the change is greater than the whole result will be displayed the same. I'm not going to give it up here. If you're interested, you can try it yourself.

Looping statements in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.