C# Tutorial for Beginner : Chapter 6 Looping
INTRODUCTION
You can make loops by utilizing the emphasis statements. Emphasis statements make inserted statements be executed various circumstances, subject to the circle end criteria. These statements are executed all together, with the exception of when a bounce explanation is experienced. By utilizing a loop, you can run a statement or a block of statements more than once until the point that a predefined articulation assesses to false. This sort of loop is helpful for emphasizing over arrays and for different applications in which you know ahead of time how often you need the loop to repeat. In C#, they come in 4 distinct variations, and we will observe every last one of them. Before I explain each one of them, let me give you a flowchart followed by code implementation. Then I will give the analysis from the code.Flowchart
As you can see, The main method is the blue one. This is simple program which show you basic mathematics operation. Each operation use different type of iteration. Addition operation use "foreach" loop, subtraction use "while" loop, multiplication use "for" loop, and the last operation division use "do-while" loop. Moreover, I give an example how to use looping forever inside main method.
Source Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Loops { class Program2 { static void Main(string[] args) { for (;;) { Console.WriteLine("Operations : "); Console.WriteLine("1. Addition\n2. Substraction\n3. Multiplication\n4. Addition"); Console.Write("Enter What operations you want : "); int n = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter Number you want to operate : "); int x = Convert.ToInt32(Console.ReadLine()); switch (n) { case 1: getAddition(x); break; case 2: getSubtraction(x); break; case 3: getMultiplication(x); break; case 4: getDivision(x); break; default: Console.Write("Wrong Input"); break; } Console.Write("Again ? [y/n] "); string again = Console.ReadLine(); if ( !(again == "Y" || again == "y") ) { break; } } } static void getMultiplication(int n) { for (int i = 0; i < 10; i++) { Console.WriteLine(n + " x " + (i + 1) + " = " + (n * (i + 1))); } } static void getAddition(int n) { int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; foreach (int number in numbers) { Console.WriteLine(n + " + " + (number) + " = " + (n + number)); } } static void getDivision(int n) { int i = 0; do { Console.WriteLine(n + " : " + (i + 1) + " = " + (n / (i + 1))); i++; } while (i < 10); } static void getSubtraction(int n) { int i = 0; while (i<10) { Console.WriteLine(n + " - " + (i + 1) + " = " + (n - (i + 1))); i++; } } } }
For Loop
The for iteration is somewhat unique. It's favored when you know what number of iteration you need, either on the grounds that you know the correct measure of emphases, or in light of the fact that you have a variable containing the sum. Before we dive discuss further, take a look example code in "getMultiplication(int n)" method. For looping have 3 section divided by semicolon (;). First part is variable initialization where you declare & initialize a variable which will be used in conditional expression and steps part. Then next part is condition which is a boolean expression which will return either true or false. If the condition is true then block statement inside for scope will be executed. The last part is steps which defines the incremental or decremental part of variable.
In the source code above, "getMultiplication(int n)" method has for statement like picture above. Variable initialization is "int i=0" where the variable i only recognize inside "for" block statement. Condition "i<10" evaluates whether variable i is less that 10. Then steps "i++" means add variable i by one in the end of block statement..
In the source code above, "getMultiplication(int n)" method has for statement like picture above. Variable initialization is "int i=0" where the variable i only recognize inside "for" block statement. Condition "i<10" evaluates whether variable i is less that 10. Then steps "i++" means add variable i by one in the end of block statement..
Foreach In Loop
The foreach statement rehashes a gathering of installed statements for every component in an array or an object collection. The foreach statement is utilized to emphasize through the collection to get the data that you need, however can not be utilized to include or expel things from the source collection to dodge undesirable result. On the off chance that you have to include or expel things from the source collection, utilize a for iteration. The inserted statements keep on executing for every component in the array or collection. After the iteration has been finished for every one of the components in the collection, control is exchanged to the following statement following the foreach block stament.
To start with, setting the name variable to the thing we have achieved each time. That way, we have a named variable to yield. As should be obvious, we announce the name variable to be of the integer type – you generally need to tell the foreach loop which datatype you are hoping to haul out of the accumulation. After the block statement, numbers will give next element to number. This operation will be end when it reach the end of numbers element.
First you need variable control to control repetition in you code. Add the condition how many you want repeat a block of statement. Then do not forget to add increment or decrement of variable control. If you do not add it, you code will crash because it will be looping without ending.
To start with, setting the name variable to the thing we have achieved each time. That way, we have a named variable to yield. As should be obvious, we announce the name variable to be of the integer type – you generally need to tell the foreach loop which datatype you are hoping to haul out of the accumulation. After the block statement, numbers will give next element to number. This operation will be end when it reach the end of numbers element.
While Loop
The while statement executes a statement or a block of statements until a specified expression evaluates to false.First you need variable control to control repetition in you code. Add the condition how many you want repeat a block of statement. Then do not forget to add increment or decrement of variable control. If you do not add it, you code will crash because it will be looping without ending.
Do-While
The inverse is valid for the do loop, which works like the while loop in different angles through. The do loop assesses the condition after the loop has executed, which ensures that the code block is constantly executed in any event once.
Do-while is the same as while loop but the different is evaluation of condition placed after statement. This means whether the condition is false or true, the statement will run once.
Comments
Post a Comment