Upload
jenna
View
26
Download
1
Embed Size (px)
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 Presentation
Citation preview
Socket ProgrammingLaboratory - 2
Logical/Conditional Operations
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.
3
if Selection Structure (cont’d)
print “Passed”Grade >= 60true
false
if (studentGrade >= 60) Console.WriteLine (“Passed”);
// beginning of the next statement
4
if Selection Structure (cont’d)
print “Passed”Grade >= 60true
false
if (studentGrade >= 60){ Console.WriteLine (“Passed”);}
// beginning of the next statement
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
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!” );
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 Statement (Example)
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
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
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
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
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
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.
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).
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
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
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
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)
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)
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
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)
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