1 / 17

Conditional statements

Conditional statements. Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand. Conditional statement. In the previous lessons, all the statements in the programs are executed sequentially without skipping Conditional statement

rhea
Télécharger la présentation

Conditional 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. Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand

  2. Conditional statement • In the previous lessons, all the statements in the programs are executed sequentially without skipping • Conditional statement • Now, some statements may be executed only if certain conditions are satisfied, otherwise, the statements will be skipped number = int.Parse(Console.ReadLine()); if(number>=0) Console.WriteLine("The number is positive"); else Console.WriteLine("The number is negative");

  3. Data type: bool • Boolean (bool when declared) is a logical data type. A value of a variable whose data type is boolean can be either true or false. Notes : A variable with type bool can not be read by input statement. However it can be displayed by output statement

  4. static void Main(string[] args) { bool found; string fact; int year; Console.Write("Please enter faculty's name : "); fact = Console.ReadLine(); fact = fact.ToLower(); Console.Write("please enter year studying : "); year = int.Parse(Console.ReadLine()); if (((fact == "engineer")&& (year >= 2)) || ((fact == "sciences") && (year >=3))) found = true; else found = false; Console.WriteLine(found); }

  5. class ClassBoolean { static void Main(string[] args) { Console.WriteLine(2+3 == 5); Console.WriteLine('A' < 'B'); Console.WriteLine("Engineer" == "engineer"); Console.WriteLine(3*4 == 7*4/2); } } Output : true true false false

  6. Relational Operators • They are used to compare data items of the compatible type

  7. Boolean Expression- Boolean Constants and Boolean Variable • Boolean constants and Boolean variables are of type bool static void Main(string[] args) { const bool sunRiseFromEast = true; const bool oneGreaterThanTwo = false; bool boy; Console.WriteLine("The sun rises from the east is " + sunRiseFromEast); boy = false; Console.WriteLine("It is " + boy + " that Paul is a boy"); }

  8. Boolean Expression- Boolean Expressions with Operands of Arithmetic Expressions

  9. Boolean Expression- Boolean Expressions with Character Operands • ASCII – American Standard Code for Information Interchange • Contains 128 characters

  10. Boolean Expression- Boolean Expressions with Character Operands • Characters are compared using their code values according to certain code system.

  11. Compound Boolean Expressions • Simple Boolean Expressions may be joined by logical operators to form compound Boolean expressions • Logical Operators • && (and) • || (or) • ! (not) • Examples • (age >= 18) && (age <=20) • (grade == 'F') || (grade =='W')

  12. Compound Boolean Expressions- Logical Operators • Truth Table for ‘and’ operator • Truth Table for ‘or’ operator

  13. Compound Boolean Expressions- Logical Operators • Truth table for ‘not’ operator

  14. Precedence • The order of precedence is ‘!’, ‘&&’, ‘||’ • The relational operators have lower precedence than logical operators int x = 3, y = 2, z = 4; Console.WriteLine(++x+3 >= y+6 && z>=4);

  15. Boolean Expression Evaluation !('F' != 'M') || (19 > 12) && (19 < 18) !( true ) || (true) && (false) ( false ) || (true) && (false) ( false ) || ( false ) ( false )

  16. Flowcharts Terminator • Graphical representation Process Input/output Condition Connector Flow line

  17. Conditional Statements • Two kinds of conditional statements in C# are ifstatements and switch statements • If statements • If-else statements • Nested if statements • Switch statements

More Related