1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University...
-
Upload
megan-newman -
Category
Documents
-
view
212 -
download
0
Transcript of 1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University...
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;
elsefound = 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
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
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
Boolean Expression Value
2.5 == 5 false
4%10 > 1-3 true
5.7 >= -2 true
5/2 <= 1.1*2 false
999!=999.9 true
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.
Boolean Expression
’\’ != ’j’
’9’ > ’4’
’A’ >= ’a’
ASCII of First Operand
92
57
65
ASCII of Second Operand
106
52
97
Boolean Expression
true
true
false
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
Boolean expression P Boolean expression Q P && Q
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE
Boolean expression P Boolean expression Q P || Q
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TURE
FALSE FALSE FALSE
13
Compound Boolean Expressions- Logical Operators
Truth table for ‘not’ operatorBoolean Expression P ! Q
TRUE FALSE
FALSE TRUE
14
Precedence
The order of precedence is ‘!’, ‘&&’, ‘||’ The relational operators have lower precedence than logical operators
Operator Precedence 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
Boolean Expression Evaluation
!('F' != 'M') || (19 > 12) && (19 < 18)
!( true ) || (true) && (false)
( false ) || (true) && (false)
( false ) || ( false )
( false )
16
Flowcharts
Graphical representation
Terminator
Process
Input/output
Condition
Connector
Flow line
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