1 / 21

Control Statements

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:

kamea
Télécharger la présentation

Control Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SWE 344 Internet Protocols & Client Server Programming Control Statements

  2. 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

  3. Simple if statement • Its formula is: • if(Condition) Statement; • If the Condition produces a true result, then the compiler executes the Statement.

  4. 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(); } } }

  5. 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 { }

  6. if…else • if (Condition) • statement1; • else • statement2;

  7. 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(); } } }

  8. if…else…if • if (Condition1) • statement1; • else if (Condition2) • statement2; • else if (Condition3) • else • statement3;

  9. 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(); } } }

  10. 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");

  11. 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(); • } • } • }

  12. 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);

  13. 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); • }

  14. 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(); • } • } • }

  15. 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; • }

  16. 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(); } }}

  17. 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; • }

  18. 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(); } }}

  19. 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());

  20. 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(); } } } }

  21. END

More Related