1 / 59

Logical Operators and Control Statements

Logical Operators and Control Statements. Boolean. Boolean takes true or false values. Used in deciding states. Commonly used in conditional statements and loops. A logical operation is a formula that has a true or false result. Boolean variables are defined with “ bool ” keyword.

khalil
Download Presentation

Logical Operators and 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. Logical Operators and Control Statements

  2. Boolean • Boolean takes true or false values. • Used in deciding states. • Commonly used in conditional statements and loops. • A logical operation is a formula that has a true or false result. • Boolean variables are defined with “bool” keyword. • bool bVal = true;

  3. Example

  4. Boolean Operators • Used for constructing Boolean expressions. • and  && • or  || • not  ! • equal  == • Not equal  != • Comparison  <, >, <=, >=

  5. And (&&) Both values are to be “true”. • true && true = true • false && true = false • true && false = false • false && false = false

  6. Or (||) At least one value is to be “true”. • true || true = true • false || true = true • true || false = true • false || false = false

  7. Not (!) Reverses the value. • !true = false • !false = true

  8. Equal (==) Returns true if two values are equal. • 1 == 2 • 1 == 0 • 42 == 42 • variable1 == otherVariable

  9. Not Equal (!=) Returns true if two values are not equal. • 1 != 2 • 1 != 0 • 42 != 42 • a != variable

  10. Comparison (>,<,>=,<=) • 1 < 2 • 0 > 1 • 42 <= 42 • age>= 18

  11. Operator Priority • Parantheses • Not (!) • Comparison(<, >, <=, >=) • Equals(==) • Not Equals(!=) • And(&&) • Or(||) Highest Lowest

  12. Example

  13. Common Mistakes • Writing logical expressions without taking operator priorities in to account. • Writing just one equals(=) instead of two (==).

  14. Control statements

  15. if – else • Decides which code block will run depending on the result of logical expression. • Logical expressions retuns boolean values. If return value is true then code blocks within th if statement will be executed. Otherwise else statement will be executed.

  16. if – else • A simple if case decides whether corresponding code block will be executed or not. • A structure that is composed of ifandelse will execute two different code blocks depending on the logical expression. • if/elsestatement can be used in nested forms to express more complex stuations.

  17. If case • If control statements has two forms: if ( expression ) { codes; } • or if ( expression ) code;

  18. Example

  19. if – else if – else • Common if use case : • We can also write if – else as: No semicolons here!!!! if ( expression ) statement; No semicolons here!!!! if (expression ) statement1; else statement2; No semicolons here!!!!

  20. if – else if – else if ( boolean expression is true ) statement ; else if (boolean expression is true) statement ; else statement ;

  21. Multiple if – else if – else • When there are multiple conditions with one line of statements, we can use if-else structure seen below: if (boolean expression is true ) statement; else if (boolean expression is true ) statement; else if (boolean expression is true ) statement; else statement;

  22. if – else if – else blocks if (boolean expression is true ) { statement blocks; } else if (boolean expression is true ) { statement blocks; } else { statement blocks; }

  23. Example: Comparing two numbers

  24. Exercise • Meteorogy department asks for a program that converts humudity values to a human readable format. • Program will write to output the following results depending input ranges between 0 and 100. • %20 and lower “too dry" • %21 - %40: “dry" • %41 - %60: “little dry" • %61 - %80: “little moist" • %81 and higher: “moist“ • Write a program that takes a humudiy value from user and writes it’s corresponding human readable output.

  25. Nested if conditions • C# compiler matches else condition to nearest if condition. Because of this, using {} paranthesis makes life easier and your code readable. if (humudity < 20) if (temperature <= 0) Console.WriteLine(“A cold and dry day.") if (ruzgar < 10) Console.WriteLine(“Wonderful, no wind!"); else Console.WriteLine(“low moist and higher than 0 degrees"); else if (humudity < 60) if (temperature <= 0) Console.WriteLine(“cold and moderate moisture."); else Console.WriteLine(“Higher than 0, moderate moisture."); ?

  26. Nested if Conditions if (humudity < 20) { if (temperature <= 0) { Console.WriteLine("A cold and dry day.") if (ruzgar < 10) { Console.WriteLine(“Wonderful, no wind!"); } } else { Console.WriteLine(“low moist and higher than 0"); } } else if (humudity < 60) { if (temperature <= 0) { Console.WriteLine(“cold and moderate moist."); } else{ Console.WriteLine(“Hidher than 0, moderate moist"); } }

  27. switch – case conditions Selection control using multiple values

  28. switch - case • Used if the value of a variable is used for controlling the program flow. • May execute different code blocks for each different value of the variable. • C#language offers this functionality with switch-case structure.

  29. Notation switch (variable ) { caseconstant_value1 : statements; break; caseconstant_value2 : statements; break; case constant_value3 : statements; break; default : statements; break; }

  30. switch - case • switchis the starting point of the structre. • A variable should be provided after switch. • This variable could be numeric or character. • We should use only constant value in case sections, no expressions. • All cases should end with break keywords. • No need to use { } paranthesis aftes cases.

  31. Example • Write a program that takes the course grade (just one letter) from user.Your program will write the following result to the output depending on the grade. • A: “best" • B: “good" • C: “all right" • D: “not bad" • F: “bad"

  32. Example Cont.

  33. Example • Meteorogy department asks for a program that converts humudity values to a human readable format. • Program will write to output the following results depending input ranges between 0 and 100. • %20 and lower “too dry" • %21 - %40: “dry" • %41 - %60: “little dry" • %61 - %80: “little moist" • %81 and higher: “moist“ • Write a program that takes a humudiy value from user and writes it’s corresponding human readable output by using switch /case control structure.

  34. Switch – Case Notes • double,decimal types are not used within switch paranthesis. • Any number of statements can be used within the case element. • Using default: element help us to detect defects. • Forgetting to use breaks is the most common coding bad habbit. Check for breaks after constructing the switch structure.

  35. Loops while, do-while and for loops

  36. Loops • Loops are used for executing code blocks repeatedly. • Decision of continuing loop is given by boolean expression. If boolean expression is false, then the code block is not executed. • For loops repeats the execution of code blocks in a certain number. Count controlled loop. • while and do-while loops repeats code execution in an unknown number of iterations. Condition controlled loop.

  37. Loops • In for and while loops, logical expressions are tested first. If expression is true then the code block inside the loop is executed, else program doesn’t enter the loop. • In do-whileloops code block is executed without checking boolean expression result for once. Then before second iteration boolean expression result is checked, and loop continuation is decided.

  38. while loop • Two types of uses: while (logical expression is true) statement; while (logical expression is true) { statement; statement; }

  39. Example

  40. do-while loop do statement; while(logical expression is true); do { statement; statement; } while(logical expression is true) ;

  41. Example

  42. For loop • initializationexpressions are executed once before loop starts. If there are multiple initialization expressions, they are separated with a comma ",". • increment,expression are executed after each iteration If there are multiple increment expressions, they are separated with a comma ",". Execution order is from left to right. • After increment operations, logical expression is evaluated and loop terminates if logical expression is false. Else loop enters next iteration. for( initialization; logical expr.(termination);increment) { statement; }

  43. for Loop • for (expression1; expression2 ; expresion3) • { • statements; • } • expression1, executed once before for loop starts. It could be one or multiple mathematical or other operations.

  44. for Loop • for (expresion1 ;expression2; expresion3) • { • statements; • } • expression2, is a logical operation that returns true or false result. It is not a requirement that expression2 includes variables used in expression1.

  45. for Loop • for (expression1; expression2;expression3) • { • statements ; • } • expression3, usually changes result of expression2, but it is not a necessity. It is executed after each iteration.

  46. Example for (int counter = 1; counter <= 10; counter ++) { Console.WriteLine(counter.ToString()); }

  47. Example int counter = 1; for (; counter <= 10;) { Console.WriteLine(counter.ToString()); counter ++; } int counter = 1; for (;;) { Console.WriteLine(counter.ToString()); counter ++; if (counter>10) break; }

  48. Notes • expression1, expression2, and expression3 within the for loop could be empty but, we must use semi colons. • If epression1 and epression3 are not used, for loop behaves like while loop. • We can not know when will the loop terminated if expression2 is not used. In this case loop can be terminated by break expression. • expression1 and expression3 might include more than one expressions. The intention of this type of usage is simplifying program code.

  49. for while expression1; while (expression2) { statements; expression3; } Her iki örnek de birbirinin aynısı şekilde çalışır. for (expression1; expression2; expression3) { statements; }

  50. Example int counter; for(counter = 0; counter <= 10; counter++) { Console.WriteLine(“The value of counter :” + counter); } int counter; counter = 1 ; while (counter <= 10 ) { Console.WriteLine(“The value of counter :” + counter); counter++ ; }

More Related