Flow Control (C#)

2,535 views

Published on

Session covers various flow control statements available in C#. Like if-else, switch-case, for, while, do-while, foreach, goto, break, continue, return, throw.

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,535
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
171
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Flow Control (C#)

  1. 1. Bhushan Mulmule bhushan.mulmule@dotnetvideotutorial.com www.dotnetvideotutorial.com
  2. 2. For video visit www.dotnetvideotutorial.com
  3. 3. FlowControl Selection Statements If / if – else / else if switch - case Loops for while do while for each Jump Statements goto break continue Return throw Agenda
  4. 4. Selection Statements  A selection statement causes the program control to be transferred to a specific flow based upon whether a certain condition is true or not.  The following keywords are used in selection statements:  if  else  switch  case  default www.dotnetvideotutorial.com
  5. 5. If-else  The if statement selects a statement for execution based on the value of a Boolean expression.  Executes if block if condition is true and else block if condition is false  else block is optional www.dotnetvideotutorial.com
  6. 6. int no1, no2; Console.Write("Enter Number1: "); no1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter Number2: "); no2 = Convert.ToInt32(Console.ReadLine()); if (no1 > no2) { Console.WriteLine("no1 is greater than no2"); } else { Console.WriteLine("no2 is greater than no1"); } if - else Optional www.dotnetvideotutorial.com
  7. 7. Nested if-else  if-else can be nested either inside other if or else block www.dotnetvideotutorial.com
  8. 8. Nested if if (no1 > no2) { Console.WriteLine("no1 is greater than no2"); } else { if(no1 < no2) Console.WriteLine("no2 is greater than no1"); else Console.WriteLine("no1 is equal to no2"); } www.dotnetvideotutorial.com
  9. 9. else-if  else –if can be use to avoid deep nesting in case of multiple conditions www.dotnetvideotutorial.com
  10. 10. else if if (no1 > no2) { Console.WriteLine("no1 is greater than no2"); } else if (no2 > no1) { Console.WriteLine("no2 is greater than no1"); } else { Console.WriteLine("no1 is equal to no2"); } www.dotnetvideotutorial.com
  11. 11. switch-case  The switch statement is a control statement that handles multiple selections and enumerations by passing control to one of the case statements within its body www.dotnetvideotutorial.com
  12. 12. switch-case int no1, no2, result = 0; char op; Console.Write("Enter Number1: "); no1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter Number2: "); no2 = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter Operator (+,-,*,/): "); op = Convert.ToChar(Console.ReadLine()); www.dotnetvideotutorial.com
  13. 13. switch (op) { case '+': result = no1 + no2; break; case '-': result = no1 - no2; break; case '*': result = no1 * no2; break; case '/': result = no1 / no2; break; default: Console.WriteLine("Invalid Operator"); break; } Console.WriteLine("Result = " + result); break is compulsory default is optional
  14. 14. Swith-case fallthrough  Swith-case fallthrough can be used to execute same block for multiple cases.  It can be achived using empty case block www.dotnetvideotutorial.com
  15. 15. Console.Write("Enter Ratings (0 to 5): "); int ratings = Convert.ToInt32(Console.ReadLine()); switch(ratings) { case 0: case 1: Console.WriteLine("Poor"); break; case 2: case 3: Console.WriteLine("Average"); break; case 4: Console.WriteLine("Good"); break; } Only empty case allow block without break statement Swith-case fallthrough
  16. 16. FlowControl Selection Statements If / if – else / else if switch - case Loops for while do while for each Jump Statements goto break continue Return throw Agenda
  17. 17. Loops  Loop executes a block of code repeatedly until a certain condition is met.  C# provides four loops  for  while  do. . .while  foreach www.dotnetvideotutorial.com
  18. 18. for for (initializer; condition; iterator) { statement(s) } The initializer is the expression evaluated before the first loop is executed The condition is the expression checked before each new iteration of the loop The iterator is an expression evaluated after each iteration www.dotnetvideotutorial.com
  19. 19. for for (int i = 0; i < 10; i++) { Console.WriteLine("Hello"); } www.dotnetvideotutorial.com
  20. 20. while int i = 0; while(i < 10) { Console.WriteLine(i); i++; } Initializer Condition Iterator www.dotnetvideotutorial.com
  21. 21. do...while int i = 0; do { Console.WriteLine("Hello " + i); i++; } while (i < 10); Initializer Condition Iterator www.dotnetvideotutorial.com
  22. 22. foreach int[] marks = { 50, 35, 65, 43, 65 }; foreach (int m in marks) { Console.WriteLine(m); } 50 35 65 43 65 m marks www.dotnetvideotutorial.com
  23. 23. Nested Loop for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { Console.WriteLine("i={0} j={1}",i,j); } Console.WriteLine(); } www.dotnetvideotutorial.com
  24. 24. FlowControl Selection Statements If / if – else / else if switch - case Loops for while do while for each Jump Statements goto break continue Return throw Agenda
  25. 25. Jump Statements  Branching is performed using jump statements, which cause an immediate transfer of the program control. The following keywords are used in jump statements:  break  continue  goto  return  throw www.dotnetvideotutorial.com
  26. 26. goto static void Main(string[] args) { label1: Console.WriteLine("Do you like this tutorial?"); Console.Write("Enter Y or N: "); char response = Convert.ToChar(Console.ReadLine()); if (response == 'N' || response == 'n') goto label1; Console.WriteLine("O Thanks!!!"); } www.dotnetvideotutorial.com
  27. 27. break for (int i = 0; i < 10; i++) { if (i == 6) break; Console.WriteLine("i = " + i); } www.dotnetvideotutorial.com
  28. 28. continue for (int i = 0; i < 10; i++) { if (i == 6) continue; Console.WriteLine("i = " + i); } www.dotnetvideotutorial.com
  29. 29. Bhushan Mulmule bhushan.mulmule@dotnetvideotutorial.com www.dotnetvideotutorial.com

×