socket programming laboratory - 2
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/1.jpg)
Socket ProgrammingLaboratory - 2
Logical/Conditional Operations
![Page 2: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/2.jpg)
2
The if statement has the following syntax:
The if Statement
if ( condition ) statement;
if is a C#reserved word
The condition must be a boolean expression.It must evaluate to either true or false.
If the condition is true, the statement is executed.If it is false, the statement is skipped.
![Page 3: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/3.jpg)
3
if Selection Structure (cont’d)
print “Passed”Grade >= 60true
false
if (studentGrade >= 60) Console.WriteLine (“Passed”);
// beginning of the next statement
![Page 4: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/4.jpg)
4
if Selection Structure (cont’d)
print “Passed”Grade >= 60true
false
if (studentGrade >= 60){ Console.WriteLine (“Passed”);}
// beginning of the next statement
![Page 5: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/5.jpg)
5
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
Boolean Expressions: Basics
![Page 6: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/6.jpg)
Equality and Relational Operators
Standard algebraic equality operator or relational operator
C# equality or relational operator
Example of C# condition
Meaning of C# condition
Equality operators == x == y x is equal to y != x != y x is not equal to y Relational operators > > x > y x is greater than y < < x < y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y Equality and relational operators.
Question: if (grade = 100) Console.WriteLine( “Great!” );
![Page 7: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/7.jpg)
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)
![Page 8: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/8.jpg)
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 Statement (Example)
![Page 9: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/9.jpg)
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
![Page 10: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/10.jpg)
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
![Page 11: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/11.jpg)
11
if/else Statement (cont’d)
Grade >= 60
print “Passed”print “Failed”
false true
if (studentGrade >= 60) Console.WriteLine (“Passed”);else Console.WriteLine (“Failed”);// beginning of the next statement
![Page 12: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/12.jpg)
12
if/else Statement (cont’d)
Grade >= 60
print “Passed”print “Failed”
false true
if (studentGrade >= 60){ Console.WriteLine (“Passed”);}else Console.WriteLine (“Failed”);// beginning of the next statement
![Page 13: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/13.jpg)
13
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
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
![Page 14: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/14.jpg)
14
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
More Complex (Compound) Boolean Expressions: Logical Operators
![Page 15: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/15.jpg)
Logical and Conditional Operatorsexpression1 expression2 expression1 &&
expression2 false false false false true false true false false true true true Truth table for the && (logical AND) operator.
expression1 expression2 expression1 || expression2
false false false false true true true false true true true true Truth table for the || (logical OR) operator.
![Page 16: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/16.jpg)
Logical and Conditional Operators
expression1 expression2 expression1 ^ expression2
false false false false true true true false true true true false Truth table for the logical exclusive OR (^) operator.
expression !expression false true True false Truth table for operator! (logical NOT).
![Page 17: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/17.jpg)
Program & Test
![Page 18: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/18.jpg)
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
![Page 19: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/19.jpg)
19
The switch Statement: Syntax
The general syntax of a switch statement is:
switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case ...
default : statement-list}
If expressionmatches value2,control jumpsto here
switchandcaseanddefaultarereservedwords
![Page 20: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/20.jpg)
20
The switch Statement
break;
case: a case a action(s)true
false
.
.
.
break;
case b action(s) break;
false
falsecase: z case z action(s) break;
default action(s)
true
true
case: b
![Page 21: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/21.jpg)
using System;
class SwitchSelect{ public static void Main() { string myInput; int myInt;
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 – 1 (Switch)
![Page 22: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/22.jpg)
using System;
class SwitchSelect{ public static void Main() { string myInput; int myInt;
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)
![Page 23: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/23.jpg)
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 – 2 (Switch) Cont’d
![Page 24: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/24.jpg)
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."); }}
Example – 3 (Switch)
![Page 25: Socket Programming Laboratory - 2](https://reader035.vdocuments.us/reader035/viewer/2022081520/56815f0c550346895dcdcbd7/html5/thumbnails/25.jpg)
Option DescriptionStep 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
Tracing and Debugging