190 likes | 378 Views
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
E N D
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 • 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");
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
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); }
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
Relational Operators • They are used to compare data items of the compatible type
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"); }
Boolean Expression- Boolean Expressions with Operands of Arithmetic Expressions
Boolean Expression- Boolean Expressions with Character Operands • ASCII – American Standard Code for Information Interchange • Contains 128 characters
Boolean Expression- Boolean Expressions with Character Operands • Characters are compared using their code values according to certain code system.
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')
Compound Boolean Expressions- Logical Operators • Truth Table for ‘and’ operator • Truth Table for ‘or’ operator
Compound Boolean Expressions- Logical Operators • Truth table for ‘not’ operator
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);
Boolean Expression Evaluation !('F' != 'M') || (19 > 12) && (19 < 18) !( true ) || (true) && (false) ( false ) || (true) && (false) ( false ) || ( false ) ( false )
Flowcharts Terminator • Graphical representation Process Input/output Condition Connector Flow line
Conditional Statements • Two kinds of conditional statements in C# are ifstatements and switch statements • If statements • If-else statements • Nested if statements • Switch statements