Frank Posted July 15, 2015 Share Posted July 15, 2015 Any problem that requires a repetitive structure can be solved using the while statement. But there is another repetitive structure whose pose is easier in certain situations. In general, the structure used for situations in which we know how many times we want the statement block is run. Example: charge 10 numbers, enter 5 marks of students, etc. We know in advance the number of times we want the block repeated. We will see, however, that in the C# language structure can be used for any repetitive situation, because ultimately it is nothing but a structure while widespread. Graphic representation: In its most typical and basic form, this structure requires an integer variable that serves as a lap timer. In the section indicated as "counter initialization" is usually placed the name of the variable that will counter by assigning to that variable an initial value. In the section "condition" the condition that must be true for the cycle to continue (if false, the cycle will stop) is placed. And finally, in the section of "counter increase" an instruction to change the value of the variable that counter (once to allow the condition is false) is placed When the cycle begins, before giving the first round for the variable takes the value indicated in the section of "counter initialization." Verified immediately, automatically, if the condition is true. If so block cycle operations is executed, and when the same instruction has been placed in the third section is executed. Then he turns to control the value of the condition, and so it goes until the condition deliver a fake. If we know how many times to repeat the block is very simple to use a for, for example if we want to repeat 50 times the block of statements can be made as follows: The FOR's variable can have any name. In this example, the defined named f. Consider the example: - The variable f is set to 1 initially. - The value of the condition is automatically controlled: as f is 1 and this is minor 50, the condition results true. - As the condition was true, the operation(s) runs. - After executing them, it returns to instruction f++, so the Variable f is incremented by one. - It becomes a control (automatically) if f is less than or equal to 50. As now its value is 2, it runs the statement block again and variable increases for the end of the same again. - The process is repeated until the variable f is increased to the value 51. At this time the condition is false, and the cycle will stop. The f variable can be modified within the block for operations, although this could cause problems if the programmer logic is inexperienced. The f variable can be initialized to any value and end at any value. Moreover, it is not mandatory that the modification statement is an increase of counter type (f++). Any instruction that changes the value of the variable is valid. If for example f = f + 2 is written instead of f++, the value of f will be increased by 2 at every turn, not to 1. In this case, it means that the cycle does not carry out the 50 laps but only 25. Problem 1: Make a program that prints screen numbers 1 to 100. Flowchart: We can observe and compare with the problem made the while. With the structure while COUNTER x used to count laps. With COUNTER for f fulfills that function. Initially as f is 1 and does not exceed the block 100 is executed, f print content at the end of the variable f repeating block is incremented by 1, and 2 is not more than the instruction block 100 is repeated. When the variable reaches for 101 out of the repetitive structure and continues the execution of the algorithm shown after circle. The f variable (or whatever you decide to call it) must be defined as a variable. Program: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace RepeatingStructureFor1 {   class Program   {     static void Main(string[] args)     {       int f;       for(f=1;f<=100;f++)       {         Console.Write(f);         Console.Write("-");       }       Console.ReadKey();     }   } } Problem 2: Develop a program to load 10 values by keyboard and then we show the sum of the input values and their average. This problem already developed, we will resolve using the FOR structure. Flowchart: In this case, the FOR's variable (f) only the required for the statement block is repeated 10 times. Program: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace RepeatingStructureFor2 {   class Program   {     static void Main(string[] args)     {       int sum,f,value,average;       string line;       sum=0;       for(f=1;f<=10;f++)       {         Console.Write("Imput Value:");         line=Console.ReadLine();         value=int.Parse(line);         sum=sum+value;       }       Console.Write("The sum is:");       Console.WriteLine(sum);       average=sum/10;       Console.Write("The average is:");       Console.Write(average);       Console.ReadKey();     }   } } The problem requires 10 values, Consider braces to enclose statement block for repeated within. The average is calculated outside for after loading the 10 values. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now