210 likes | 354 Views
SWE 344 Internet Protocols & Client Server Programming. Control Statements. Conditional Statements. A conditional statement is an expression that produces a true or false result. 1- Simple if statement 2- if…else statement 3- if…else…if statement. Simple if statement. Its formula is:
E N D
SWE 344 Internet Protocols & Client Server Programming Control Statements
Conditional Statements A conditional statement is an expression that produces a true or false result. 1-Simple if statement 2- if…else statement 3- if…else…if statement
Simple if statement • Its formula is: • if(Condition) Statement; • If the Condition produces a true result, then the compiler executes the Statement.
Example: Simple if statement using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespace ConsoleApplication3 { classProgram { staticvoid Main(string[] args) { var x = 0; Console.WriteLine("Enter the value of x : "); x = int.Parse(Console.ReadLine()); if (x == 1) Console.WriteLine("x is equal to 1"); Console.ReadLine(); } } }
Example: Simple if statement with braces using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespace ConsoleApplication3 { classProgram { staticvoid Main(string[] args) { var x = 0; Console.WriteLine("Enter the value of x : "); x= int.Parse(Console.ReadLine()); if (x== 1) { Console.WriteLine("x is equal to 1"); Console.WriteLine("the condition is true"); } Console.ReadLine(); } } } If more than one statement we use braces { }
if…else • if (Condition) • statement1; • else • statement2;
Example: else…if statement using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespace ConsoleApplication3 { classProgram { staticvoid Main(string[] args) { var x = 0; Console.WriteLine("Enter the value of x : "); x = int.Parse(Console.ReadLine()); if (x == 1) Console.WriteLine("x is equal to 1"); else Console.WriteLine("x is less than or greater than 1"); Console.ReadLine(); } } }
if…else…if • if (Condition1) • statement1; • else if (Condition2) • statement2; • else if (Condition3) • else • statement3;
Example: if…else…if using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespace ConsoleApplication3 { classProgram { staticvoid Main(string[] args) { var x = 0; Console.WriteLine("Enter the value of x : "); x = int.Parse(Console.ReadLine()); if (x == 1) Console.WriteLine("x is equal to 1"); elseif (x > 1) Console.WriteLine("x is greater than 1"); else Console.WriteLine("x is less than 1"); Console.ReadLine(); } } }
Example: Logical Conjunction: AND & OR using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespace ConsoleApplication3 { classProgram { staticvoid Main(string[] args) { var x = 0; var z = 0; Console.WriteLine("Enter the value of x : "); x = int.Parse(Console.ReadLine()); Console.WriteLine("Enter the value of z : "); z = int.Parse(Console.ReadLine()); Console.WriteLine("using AND"); if (x > z && x > 1) Console.WriteLine(" x is greater than " + z + " AND greater than 1");
Example: Logical Conjunction: AND & OR • Console.WriteLine("======================="); • Console.WriteLine("using OR"); • if (x > z || x > 1) • Console.WriteLine(" x is greater than " + z + " OR greater than 1"); • Console.ReadLine(); • } • } • }
Comprehensive Example: Logical Conjunction using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespace ConsoleApplication3 { classProgram { staticvoid Main(string[] args) { stringmyInput; intmyInt; Console.Write("Please enter a number: "); myInput = Console.ReadLine(); myInt = Int32.Parse(myInput); // Single Decision and Action with braces if (myInt > 0) { Console.WriteLine("Your number {0} is greater than zero.", myInt); } // Single Decision and Action without brackets if (myInt < 0) Console.WriteLine("Your number {0} is less than zero.", myInt);
Comprehensive Example: Logical Conjunction • // Either/Or Decision • if (myInt != 0) • { • Console.WriteLine("Your number {0} is not equal to zero.", myInt); • } • else • { • Console.WriteLine("Your number {0} is equal to zero.", myInt); • } • // Multiple Case Decision • if (myInt < 0 || myInt == 0) • { • Console.WriteLine("Your number {0} is less than or equal to zero.", myInt); • } • elseif (myInt > 0 && myInt <= 10) • { • Console.WriteLine("Your number {0} is in the range from 1 to 10.", myInt); • } • elseif (myInt > 10 && myInt <= 20) • { • Console.WriteLine("Your number {0} is in the range from 11 to 20.", myInt); • }
Comprehensive Example: Logical Conjunction • elseif (myInt > 20 && myInt <= 30) • { • Console.WriteLine("Your number {0} is in the range from 21 to 30.", myInt); • } • else • { • Console.WriteLine("Your number {0} is greater than 30.", myInt); • } • Console.ReadLine(); • } • } • }
Case Switches When defining an expression whose result would lead to a specific program execution, the switch statement considers that result and executes a statement based on the possible outcome of that expression, this possible outcome is called a case. • switch(Expression) • { • case Choice1: • Statement1; • break; • case Choice2: • Statement2; • break; • case Choice-n: • Statement-n; • break; • }
Example: Case Switches using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespace ConsoleApplication3 { classProgram { staticvoid Main(string[] args) { var x=0; Console.Write("Enter you choice? "); x= int.Parse(Console.ReadLine()); switch (x) { case 1: Console.WriteLine("x is equal to 1"); break; case 2: Console.WriteLine("x is equal to 2"); break; case 3: Console.WriteLine("x is equal to 3"); break; } Console.ReadLine(); } }}
Case Switches with default value • switch(Expression) • { • case Choice1: • Statement1; • break; • case Choice2: • Statement2; • break; • case Choice-n: • Statement-n; • break; • default: • Other-Possibility; • break; • }
Example: Case Switches with default value using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespace ConsoleApplication3 { classProgram { staticvoid Main(string[] args) { var x=0; Console.Write("Enter you choice? "); x= int.Parse(Console.ReadLine()); switch (x) { case 1: case 2: case 3: Console.WriteLine("Your number is {0}.", x); break; default: Console.WriteLine("Your number {0} is not between 1 and 3.", x); break; } Console.ReadLine(); } }}
Question#1: Find the greatest number among three numbers .Read the numbers from the user. Use if and logical operator to find the greatest. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lab3 { class greatest { public static void Main() { int x, y, z; Console.Write("Enter first number:"); x = Convert.ToInt16(Console.ReadLine()); Console.Write("Enter Second number:"); y = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter Third number:"); z = Convert.ToInt16(Console.ReadLine()); if (x > y && x > z) { Console.WriteLine("X is greatest:{0}", x); Console.ReadLine(); } else if (y > x && y > z) { Console.WriteLine("Y is greatest:{0}", y); Console.ReadLine(); } else { Console.WriteLine("Z is greatest:{0}", z); Console.ReadLine(); } } } }