Download presentation
Presentation is loading. Please wait.
Published byMegan Newman Modified over 9 years ago
1
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand
2
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
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
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
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
6
6 Relational Operators They are used to compare data items of the compatible type Relational Operator in Pascal Meaning ==Equal to >Greater than <Less than >=Greater than or equal to <=Less than or equal to !=Not equal to
7
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
8 Boolean Expression - Boolean Expressions with Operands of Arithmetic Expressions Boolean Expression Value 2.5 == 5false 4%10 > 1-3true 5.7 >= -2true 5/2 <= 1.1*2false 999!=999.9true
9
9 Boolean Expression - Boolean Expressions with Character Operands ASCII – American Standard Code for Information Interchange Contains 128 characters
10
10 Boolean Expression - Boolean Expressions with Character Operands Characters are compared using their code values according to certain code system. Boolean Expression ’\’ != ’j’ ’9’ > ’4’ ’A’ >= ’a’ ASCII of First Operand 92 57 65 ASCII of Second Operand 106 52 97 Boolean Expression true false
11
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
12 Compound Boolean Expressions - Logical Operators Truth Table for ‘and’ operator Truth Table for ‘or’ operator Boolean expression PBoolean expression QP && Q TRUE FALSE TRUEFALSE Boolean expression PBoolean expression QP || Q TRUE FALSETRUE FALSETRUETURE FALSE
13
13 Compound Boolean Expressions - Logical Operators Truth table for ‘not’ operator Boolean Expression P! Q TRUEFALSE TRUE
14
14 Precedence The order of precedence is ‘!’, ‘&&’, ‘||’ The relational operators have lower precedence than logical operators OperatorPrecedence Level ( ) 1 ++, --, !, cast 2 *, /, % 3 +, - 4, >= 5 ==, != 6 && 7 || 8 int x = 3, y = 2, z = 4; Console.WriteLine(++x+3 >= y+6 && z>=4);
15
15 Boolean Expression Evaluation !('F' != 'M') || (19 > 12) && (19 < 18) !( true ) || (true) && (false) ( false ) || (true) && (false) ( false ) || ( false ) ( false )
16
16 Flowcharts Graphical representation Terminator Process Input/output Condition Connector Flow line
17
17 Conditional Statements Two kinds of conditional statements in C# are if statements and switch statements If statements If-else statements Nested if statements Switch statements
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.