Вы находитесь на странице: 1из 7

Basic Control Structures in C#

31/08/2013

UKBSTE .Net Nagarjuna Chada IS Telecom 1.2

nagarjuna.chada@tcs.com

Confidentiality Statement Include the confidentiality statement within the box provided. This has to be legally approved Confidentiality and Non-Disclosure Notice The information contained in this document is confidential and proprietary to TATA Consultancy Services. This information may not be disclosed, duplicated or used for any other purposes. The information contained in this document may not be released in whole or in part outside TCS for any purpose without the express written permission of TATA Consultancy Services.

Tata Code of Conduct We, in our dealings, are self-regulated by a Code of Conduct as enshrined in the Tata Code of Conduct. We request your support in helping us adhere to the Code in letter and spirit. We request that any violation or potential violation of the Code by any person be promptly brought to the notice of the Local Ethics Counselor or the Principal Ethics Counselor or the CEO of TCS. All communication received in this regard will be treated and kept as confidential.

Table of Content 1. 2. 3. 4. 5. 6. 7. 8. 9. Introduction .............................................................................................................................................................. 4 if Statement .............................................................................................................................................................. 4 if - else Statement ..................................................................................................................................................... 4 else if Statement ....................................................................................................................................................... 4 Switch Statement ...................................................................................................................................................... 5 While Loop ................................................................................................................................................................ 5 Do-While Loop .......................................................................................................................................................... 5 for Loop ..................................................................................................................................................................... 6 for- each loop ............................................................................................................................................................ 6

1. Introduction
Control Structures are basic mechanisms which are used to control the flow of execution. There are 9 control structure statements in C# 1. 2. 3. 4. 5. 6. 7. 8. 9. if statement. if else statement else if Statement switch statement while loop do-while loop for loop for-each loop goto

2. if Statement
if statement checks a condition and the condition is true it executes the statements inside the if block.
int a = 0; if (a == 0) Console.WriteLine("value of a =0"); Console.Read();

3. if - else Statement
if-else statement evaluates the condition in if block, if it becomes true executes the statements in if block and if the condition becomes false, executes the statements in else block.
int a = 1; if (a == 0) Console.WriteLine("value of a =0"); else Console.WriteLine("value of a is not 0");

4. else if Statement
if-else statement evaluates the condition in if block, if it becomes true executes the statements in if block and if the condition becomes false, evaluates next else if block and executes the code in which ever condition becomes true.

int a = 1; if (a == 0) Console.WriteLine("value of a =0"); else if (a == 1) Console.WriteLine("value of a is a=1"); else Console.WriteLine("Value of is not 0 or 1"); Console.Read();

5. Switch Statement
Switch statement evaluates the condition against a case specified in its block. If it matches with any of the case, it executes that block. If does not matches with any of the cases, it executes the default block specified in the last.
int a = 4; switch (a) { case 1 : Console.WriteLine("a break; case 2: Console.WriteLine("a break; case 3: Console.WriteLine("a break; case 4: Console.WriteLine("a break; default: Console.WriteLine("a break; } Console.Read();

value is 1");

value is 2");

value is 3");

value is 4");

value does not matches with any of the cases");

6. While Loop
While-loop is a entry control iteration statement. It evaluates the condition and executes the block of statements in it until the condition becomes false.
int count = 1; while (count < 10) { Console.WriteLine("Count value : " + count); count++; } Console.Read();

7. Do-While Loop
Do while loop is a exit control iteration statement. It executes the code first and evaluates the condition. It executes the block of statements inside it until the condition becomes false. However this loop executes the block of statements at least once.
int count = 1; do { Console.WriteLine("Count value : " + count); count++; } while (count<10); Console.Read();

8. for Loop
By using a for loop, you can run a statement or a block of statements repeatedly until a specified expression evaluates to false. This kind of loop is useful for iterating over arrays and for other applications in which you know in advance how many times you want the loop to iterate.
int[] a = { 1, 2, 3, 4, 5, 6 }; for (int i = 0; i < a.Length; i++) { Console.WriteLine("a[{0}] value : {1}", i, a[i]); } Console.Read();

9. for- each loop


Foreach is a loop construct. It does not use an integer index. Instead, it is used on a collection and returns each element in order. This is called enumeration. It eliminates errors caused by incorrect index handling.
int[] a = { 1, 2, 3, 4, 5, 6 }; Console.WriteLine("The values in the array are : "); foreach(int value in a) { Console.WriteLine(value); } Console.Read();

Thank You

Contact For more information, contact gsl.cdsfiodg@tcs.com(Email Id of ISU)

About Tata Consultancy Services (TCS) Tata Consultancy Services is an IT services, consulting and business solutions organization that delivers real results to global business, ensuring a level of certainty no other firm can match. TCS offers a consulting-led, integrated portfolio of IT and ITenabled infrastructure, engineering and assurance services. This is delivered through its TM unique Global Network Delivery Model , recognized as the benchmark of excellence in software development. A part of the Tata Group, Indias largest industrial conglomerate, TCS has a global footprint and is listed on the National Stock Exchange and Bombay Stock Exchange in India. For more information, visit us at www.tcs.com.

IT Services Business Solutions Consulting


All content / information present here is the exclusive property of Tata Consultancy Services Limited (TCS). The content / information contained here is correct at the time of publishing. No material from here may be copied, modified, reproduced, republished, uploaded, transmitted, posted or distributed in any form without prior written permission from TCS. Unauthorized use of the content / information appearing here may violate copyright, trademark and other applicable laws, and could result in criminal or civil penalties. Copyright 2011 Tata Consultancy Services Limited

Вам также может понравиться