While in programming How do you use the loop?

In this post, we will be talking about While in programmingThese are instructions in control structures that are repeated on many occasions, among their main functions is to hinder a number of instructions related to true or false, in addition, it is disclosed how the loop is used.

while-in-programming-1

While in programming

What is the while for in programming, is used to give a series of instructions in a control structure that is repeated many times, it also has the function of hindering a series of instructions, in the case that the evaluation of the linked expression and / or is logical or false.

This means that it becomes repetitive only when the evaluation of the instruction is true.

In addition to the control structures, such as the if or switch statement in programming, there are also reiterative structures.

In repetitive programming structures, related to a while loop, it repeats a block of code as long as it contains a true value, which can be expressed as follows:

  • While (condition).
  • {.
  • instructions;.
  • }.

As you can see there are repetitions and loops, however, it is generally the same, it is important to note that the loops contain:

  • The condition to evaluate is true or false, and it is performed on each repetition of the loop.
  • The statement that displays the lines of code is performed if the condition is true.

Among the characteristics of this kind of loop is that the condition is processed prior to performing the code, then, in the case that the result is false, the instructions will not be executed, while there is another type of loop that executes only one time.

Here we recommend an interesting article related to C language.

Then, while loops are defined, it refers to a cyclical structure that allows one or different lines of code to be repeated repeatedly, without having an initial value and sometimes without knowing when it will return the expected final value.

While loops are those that are not subject to numerical values, on the contrary they depend on Boolean values, which means the truth value of a true or false condition.

while-in-programming-2

How does a While Loop work?

In order to understand how the while loop works, in this part we will mention examples that lead us to better visualize its performance.

As an example, we suppose that for some reason, a user is asked for a number of numbers that occur to them, and that they enter a number greater than 100.

As you can see, you cannot use a for loop, because you have no idea that the user will enter a number greater than 100, it is something that cannot be determined, but the while loop agrees to perform a action infinitely until a specific condition is executed, in this case it is the number entered that is greater than 100.

So, if the user continuously enters the following numbers: 1, 50, 99, 49, 21, 30, 100, the program would not finish, all because the numbers are not greater than 100, but, in the case that enter the number 300, the program has the condition that it will end immediately.

While Loop Syntax in C ++

The syntax of a while loop is simpler and more readable than that of the for loop in C ++, because it only requires a precise stop condition.

Most of the high-level languages ​​the way to write a while loop is very similar, so what you should do is keep in mind the termination condition for the loop that will be prepared.

Let's see in the following example how an end condition would be placed:

  • while (end condition) // for example number == 100.
  • {.
  • ....
  • ....
  • ....
  • ....
  • }.

We will visualize line by line of the aforementioned code, in such a way that it helps us understand what is meant.

Line 1: It has in its content the most important of a while loop.

The syntax is very simple, you can see that a condition is found inside the parentheses, for example: «==. >, <,> =, <=,! = »Or perhaps others, the specially stated condition, is the one that will grant that the cycle continues fulfilling until it reaches the point that the same condition does not continue executing.

So for example, you are confirming that a certain number == 50, the cycle is performed only when any number is equal to 50; when its value is modified to any other quantity, the while loop ends its process, but it will continue with the other part of the program execution.

It should be considered that it is clear that the registered condition will always take a Boolean value, that is, true or false.

Line 2: An "{" opening appears in this line, which means that in this part a block of instructions is being initiated that will be fulfilled once the cycle begins a turn.

However, placing this key is not mandatory, but, if it is not placed, it will only be executed in the while loop that is shown in the first immediate line, then the loop declaration, which means that if you don't want to If different lines are carried out in the cycle, the keys must be placed.

Lines 3 to 7: These lines are where all the operations that you wish to repeatedly execute in the cycle process will be placed. This block may contain the number of lines required.

Line 8: It is the last line and the key must be used by closing "}", as established by the block of the while loop and execution will be terminated, however, the rest of the algorithm will continue.

Now we will show some examples that will lead users to understand in a clear and simple way the use of while loops in C ++, we start with:

Example 1: Ask for numbers on the screen until one is greater than 100

We will continue with the example indicated above, where we proceed to the program asking a user to enter a number of numbers, no matter what they are, and that will stop being performed, when the user enters a number greater than 100, it is an example practical and easy, to verify that the aforementioned has been understood.

Solution Example 1:

We are going to give the solution, it must be borne in mind that the condition must be executed so that the cycle is requesting the number, the cycle will stop only when the number entered is greater than 100, then the condition for me to continue processing is that the number is less than 100, the number must be greater than 100 in order to stop, and to continue with the process the number must be less than or equal to 100.

It can be seen that it is reflected in the following way:

  • int number ;.
  • cin >> number ;.
  • while (number <= 100).
  • {.
  • cout << «Please enter a number«;.
  • cin >> number ;.
  • }.

The absolute functional code is very easy to handle by the user, below is the practice of how to use it:

  • #include "iostream".
  • using namespace std ;.
  • int main ().
  • {.
  •  int number ;.
  •  cout << «Please enter a number«;.
  •  cin >> number ;.
  •  while (number <= 100).
  •  {.
  •  cout << «Please enter a number«;.
  •  cin >> number ;.
  •  }.
  •  system ("PAUSE") ;.
  •  return 0 ;.
  • }.

While loops in programming are very useful, but it must be borne in mind that from the aspect of efficiency and validity like other for loops, it is suggested that they not be used, every time a while loop or any other type of loop is going to be used. cycle, it is best to get advice before if its use is imperative, or if there is another more practical way to use it.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Actualidad Blog
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.