1 / 25

Socket Programming Laboratory - 2

Socket Programming Laboratory - 2. Logical/Conditional Operations. The condition must be a boolean expression . It must evaluate to either true or false. if is a C# reserved word. If the condition is true, the statement is executed. If it is false, the statement is skipped.

jenna
Download Presentation

Socket Programming Laboratory - 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Socket ProgrammingLaboratory - 2 Logical/Conditional Operations

  2. The condition must be a boolean expression. It must evaluate to either true or false. if is a C# reserved word If the condition is true, the statement is executed. If it is false, the statement is skipped. The if Statement • The if statement has the following syntax: if ( condition ) statement;

  3. Grade >= 60 print “Passed” true false if Selection Structure (cont’d) if (studentGrade >= 60) Console.WriteLine (“Passed”); // beginning of the next statement

  4. Grade >= 60 print “Passed” true false if Selection Structure (cont’d) if (studentGrade >= 60) { Console.WriteLine (“Passed”); } // beginning of the next statement

  5. Boolean Expressions: Basics • A condition often uses one of C#’s equality operators (==, !=) or relational operators (<, >, <=, >=), which all return boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to

  6. Equality and Relational Operators Question: if (grade = 100) Console.WriteLine( “Great!” );

  7. If Statement (Example) using System; class Comparison { static void Main(string[] args) { int number1, number2; // read in first number from user Console.Write("Please enter first integer: "); number1 = Int32.Parse(Console.ReadLine()); // read in second number from user Console.Write("\nPlease enter second integer: "); number2 = Int32.Parse(Console.ReadLine()); if (number1 == number2) Console.WriteLine(number1 + " == " + number2);

  8. If Statement (Example) if (number1 != number2) Console.WriteLine(number1 + " != " + number2); if (number1 < number2) Console.WriteLine(number1 + " < " + number2); if (number1 > number2) Console.WriteLine(number1 + " > " + number2); if (number1 <= number2) Console.WriteLine(number1 + " <= " + number2); if (number1 >= number2) Console.WriteLine(number1 + " >= " + number2); } }

  9. if/else Statement if ( <test> ) <code executed if <test> is true> ; else <code executed if <test> is false> ; • The if/else structure • Alternate courses can be taken when the statement is false • Rather than one action there are two choices • Nested structures can test many cases

  10. if/else Statement (cont’d) if (<test>) { <code executed if <test> is true> ; …… } else { <code executed if <test> is false> ; …… } • Can use the block statement inside either branch

  11. false true Grade >= 60 print “Failed” print “Passed” if/else Statement (cont’d) if (studentGrade >= 60) Console.WriteLine (“Passed”); else Console.WriteLine (“Failed”); // beginning of the next statement

  12. false true Grade >= 60 print “Failed” print “Passed” if/else Statement (cont’d) if (studentGrade >= 60) { Console.WriteLine (“Passed”); } else Console.WriteLine (“Failed”); // beginning of the next statement

  13. Nested if/else Statements • The statement executed as a result of an if statement or else clause could be another if statement • These are called nested if /else statements if (studentGrade >= 90) Console.WriteLine(“A”); else if (studentGrade >= 80) Console.WriteLine(“B”); else if (studentGrade >= 70) Console.WriteLine(“C”); else if (studentGrade >= 60) Console.WriteLine(“D”); else Console.WriteLine(“F”); // beginning of the next statement

  14. More Complex (Compound) Boolean Expressions: Logical Operators • Boolean expressions can also use the following logical and conditional operators: ! Logical NOT & Logical AND | Logical OR ^ Logical exclusive OR (XOR) && Conditional AND || Conditional OR • They all take boolean operands and produce boolean results

  15. Logical and Conditional Operators

  16. Logical and Conditional Operators

  17. Program & Test

  18. The switch Statement • The switch statement provides another means to decide which statement to execute next • The switch statement evaluates an expression, then attempts to match the result to one of several possible cases • Each case contains a value and a list of statements • The flow of control transfers to statement list associated with the first value that matches

  19. switch and case and default are reserved words If expression matches value2, control jumps to here The switch Statement: Syntax The general syntax of a switch statement is: switch ( expression ) { casevalue1 : statement-list1 casevalue2 : statement-list2 case ... default : statement-list }

  20. true a case : a action(s) case break; false true b case : b action(s) break; case false . . . true z z action(s) case : case break; false action(s) default break; The switch Statement

  21. Example – 1 (Switch) using System; class SwitchSelect { public static void Main() { string myInput; intmyInt; Console.Write("Please enter a number between 1 and 3: "); myInput = Console.ReadLine(); myInt = Int32.Parse(myInput); // switch with integer type switch (myInt) { case 1: Console.WriteLine("Your number is {0}.", myInt); break; case 2: Console.WriteLine("Your number is {0}.", myInt); break; case 3: Console.WriteLine("Your number is {0}.", myInt); break; default: Console.WriteLine("Your number {0} is not between 1 and 3.", myInt); break; } } }

  22. Example – 2 (Switch) using System; class SwitchSelect { public static void Main() { string myInput; intmyInt; begin: Console.Write("Please enter a number between 1 and 3: "); myInput = Console.ReadLine(); myInt = Int32.Parse(myInput); // switch with integer type switch (myInt) { case 1: Console.WriteLine("Your number is {0}.", myInt); break; case 2: Console.WriteLine("Your number is {0}.", myInt); break; case 3: Console.WriteLine("Your number is {0}.", myInt); break; default: Console.WriteLine("Your number {0} is not between 1 and 3.", myInt); break; }

  23. Example – 2 (Switch) Cont’d decide: Console.Write("Type \"continue\" to go on or \"quit\" to stop: "); myInput = Console.ReadLine(); // switch with string type switch (myInput) { case "continue": goto begin; case "quit": Console.WriteLine("Bye."); break; default: Console.WriteLine("Your input {0} is incorrect.", myInput); goto decide; } } }

  24. Example – 3 (Switch) using System; class SwitchTest { static void Main() { Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large"); Console.Write("Please enter your selection: "); string s = Console.ReadLine(); int n = int.Parse(s); int cost = 0; switch (n) { case 1: cost += 25; break; case 2: cost += 25; goto case 1; case 3: cost += 50; goto case 1; default: Console.WriteLine("Invalid selection. Please select 1, 2, or 3."); break; } if (cost != 0) { Console.WriteLine("Please insert {0} cents.", cost); } Console.WriteLine("Thank you for your business."); } }

  25. Tracing and Debugging

More Related