Socket Programming Laboratory 2 LogicalConditional Operations The if

Socket Programming Laboratory - 2 Logical/Conditional Operations

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

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

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

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

Equality and Relational Operators Question: if (grade = 100) Console. Write. Line( “Great!” );
![If Statement (Example) using System; class Comparison { static void Main(string[] args) { int If Statement (Example) using System; class Comparison { static void Main(string[] args) { int](http://slidetodoc.com/presentation_image_h2/63c3b2fbaeede701fd1564892f42117d/image-7.jpg)
If Statement (Example) using System; class Comparison { static void Main(string[] args) { int number 1, number 2; // read in first number from user Console. Write("Please enter first integer: "); number 1 = Int 32. Parse(Console. Read. Line()); // read in second number from user Console. Write("n. Please enter second integer: "); number 2 = Int 32. Parse(Console. Read. Line()); if (number 1 == number 2) Console. Write. Line(number 1 + " == " + number 2);

If Statement (Example) if (number 1 != number 2) Console. Write. Line(number 1 + " != " + number 2); if (number 1 < number 2) Console. Write. Line(number 1 + " < " + number 2); } } if (number 1 > number 2) Console. Write. Line(number 1 + " > " + number 2); if (number 1 <= number 2) Console. Write. Line(number 1 + " <= " + number 2); if (number 1 >= number 2) Console. Write. Line(number 1 + " >= " + number 2);

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 9

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 10

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

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

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

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 14

Logical and Conditional Operators

Logical and Conditional Operators

Program & Test

The switch Statement 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 � The 18

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

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

Example – 1 (Switch) using System; class Switch. Select { public static void Main() { string my. Input; int my. Int; Console. Write("Please enter a number between 1 and 3: "); my. Input = Console. Read. Line(); my. Int = Int 32. Parse(my. Input); // switch with integer type switch (my. Int) { case 1: Console. Write. Line("Your number is {0}. ", my. Int); break; case 2: Console. Write. Line("Your number is {0}. ", my. Int); break; case 3: Console. Write. Line("Your number is {0}. ", my. Int); break; default: Console. Write. Line("Your number {0} is not between 1 and 3. ", my. Int); break; } } }

Example – 2 (Switch) using System; class Switch. Select { public static void Main() { string my. Input; int my. Int; begin: Console. Write("Please enter a number between 1 and 3: "); my. Input = Console. Read. Line(); my. Int = Int 32. Parse(my. Input); // switch with integer type switch (my. Int) { case 1: Console. Write. Line("Your number is {0}. ", my. Int); break; case 2: Console. Write. Line("Your number is {0}. ", my. Int); break; case 3: Console. Write. Line("Your number is {0}. ", my. Int); break; default: Console. Write. Line("Your number {0} is not between 1 and 3. ", my. Int); break; }

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

Example – 3 (Switch) using System; class Switch. Test { static void Main() { Console. Write. Line("Coffee sizes: 1=Small 2=Medium 3=Large"); Console. Write("Please enter your selection: "); string s = Console. Read. Line(); 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. Write. Line("Invalid selection. Please select 1, 2, or 3. "); break; } if (cost != 0) { Console. Write. Line("Please insert {0} cents. ", cost); } Console. Write. Line("Thank you for your business. "); } }

Tracing and Debugging Option Description Step Into If the current line contains a method, the execution point will traverse deeper into the method definition. Step Over If the current line contains a method, the execution point will not move further into the method definition Step Out If the current line is within a method definition, the execution point will move out of the current method and back to the point before calling the method
- Slides: 25