250 likes | 321 Views
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.
E N D
Socket ProgrammingLaboratory - 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. The if Statement • The if statement has the following syntax: if ( condition ) statement;
Grade >= 60 print “Passed” true false if Selection Structure (cont’d) if (studentGrade >= 60) Console.WriteLine (“Passed”); // beginning of the next statement
Grade >= 60 print “Passed” true false if Selection Structure (cont’d) if (studentGrade >= 60) { Console.WriteLine (“Passed”); } // beginning of the next statement
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
Equality and Relational Operators Question: if (grade = 100) Console.WriteLine( “Great!” );
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);
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); } }
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
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
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
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
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
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
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
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 }
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
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; } } }
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; }
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; } } }
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."); } }