Jump to content

While repeating structure


Frank

Recommended Posts

A repetitive structure allows to execute an instruction or set of instructions several times.

A repetitive execution of judgments is characterized by:

- The judgments or recurring.

- The test or condition before each iteration, will motivate that are not repeated or judgments.

While repeating structure.

Graphical representation of the structure while:

9T3BvVV.png

We must not confuse the graphical representation of the repetitive structure WHILE with conditional structure IF

Operation: First the condition is verified, if it is true that the operations indicated by the branch of True run.

A true branch plotted in the bottom of the condition. A line at the end of the repeat block connected to the top of the repetitive structure.

If the condition is False continues False branch and leaves the repetitive structure to continue the execution of the algorithm.

The block is repeated while the condition is true.

Important: If the condition always returns true we are in the presence of an infinite repeating cycle. This situation is a programming error, the program never ends.

Problem 1:

Make a program that prints screen numbers 1 to 100.

Without knowing the repetitive structures we can solve the problem by using a sequential structure. Initialize a variable with the value 1, then print the variable, the variable we increase again and so on.

Flowchart:

Qymc9ec.png

If we continue with the diagram we would not reach the next 5 pages to finalize it. Use a sequential structure to solve this problem it causes a flowchart and a program in C # too long.

Now Look for the repetitive structure while using a solution:

3Rx9j8j.png

It is very important to analyze this diagram:

The first operation initializes the variable x to 1, then starts while the repetitive structure and have the following condition (x <= 100), while the variable x is less than or equal to 100.

When running condition returns TRUE because the content of x (1) is less than or equal to 100. When the true condition instruction block containing the while statement is executed. The statement block contains an output and an operation.

The content of x is printed, and then increments the variable x by one.

The operation x = x + 1 is read as "in the variable x the content of x plus 1 is saved." That is, if x contains one executed after this operation is stored in xa 2.

At the end of the block of instructions containing the repeating structure is again checked the condition of the repetitive structure and the process explained above is repeated.

While the condition returns true the statement block is executed; to return false verification of the condition leaves the continuous repetitive structure and algorithm, in this case the program ends.

The hardest part is the definition of the condition of the while block structure and what instructions will be repeated. Note that if, for example, have the condition x> = 100 (if x is greater than or equal to 100) causes no syntax error but we are in the presence of a logic error because at first assessed the condition returns false and not executes the block of statements we wanted to repeat 100 times.

There is no recipe to define a condition of a repetitive structure, but is achieved with continuous practice solving problems.

Once you raised the diagram must verify if it is a valid solution to the problem (in this case should print the numbers 1 to 100 on the screen), for it can keep track of the flow diagram and the values ​​taken by the variables along the execution:

x

1

2

3

4

.

.

        100

        101  When x is equal to 101 the condition return false,

            in that case the diagram ends.

Important: We can see that the repetitive block if the condition returns false the first time can not be executed any time.

The variable x must be initialized with some value before the operation x = x + 1 is executed in the event of not being initialized a compilation error occurs.

Program:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace RepetitiveStructureWhile1

{

    class Program

    {

        static void Main(string[] args)

        {

            int x;

            x = 1;

            while (x <= 100)

            {

                Console.Write(x);

                Console.Write(" - ");

                x = x + 1;

            }

            Console.ReadKey();

        }

    }

}

Recall that a problem will not be 100% solved if we do the program in C # that shows the desired results.

Let's try some modifications of the program and see what changes should be made to:

1 - Print numbers from 1 to 500.

2 - Print numbers from 50 to 100.

3 - Print numbers of -50 to 0.

4 - Print numbers from 2 to 100 but 2 by 2 (2,4,6,8 .... 100).

Replies:

1 - We must change the condition of the while with x <= 500.

2 - We need to initialize x with the value 50.

3 - Initialize x to the value -50 and set the condition x <= 0.

4 - Initialize x with value 2 and within the x enhance repeating block 2

    (x = x + 2)

Problem 2:

Write a program that prompts the burden of a positive value and show us from 1 to the value entered one by one.

Example: If we enter 30 to display on screen the numbers from 1 to 30.

It is of fundamental importance to analyze the flowcharts and subsequent coding in C # of the following problems in several other unseen problems in the previous year situations arise.

Flowchart:

Wqb2ety.png

We can see that is entered by keyboard the n variable. The operator can load any value.

If the operator load 10 repetitive block is executed 10 times, as the condition it is "While x <= n", ie "while x is less than or equal to 10"; x begins because one and is incremented by one each time the block is executed repetitive.

A test chart we can make giving values to variables; for example, if we enter track 5 it is:

n x

5 1  (It prints x)

        2 " "

        3 " "

        4 " "

        5 " "

        6 (Exits while because 6 is not less than or equal to 5)

Program:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace RepetitiveStructureWhile2

{

    class Program

    {

        static void Main(string[] args)

        {

            int n,x;

            string linea;

            Console.Write("Enter the final value:");

            linea=Console.ReadLine();

            n=int.Parse(linea);

            x=1;

            while (x<=n)

            {

                Console.Write(x);

                Console.Write(" - ");

                x = x + 1;

            }

            Console.ReadKey();

        }

    }

}

The names of the variables and x can be words or letters (as in this case)

The variable x is called the counter. An accountant is a special type of variable that increases or decreases with constant values during execution of the program.

X indicates the counter at all times the amount of printed values on the screen.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...