Pascal Programming

Conditional statements
Dept. of Computer Engineering
Faculty of Engineering, Kasetsart University
Bangkok, Thailand
1
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");
2
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
3
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);
}
4
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
5
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
6
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");
}
7
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
8
Boolean Expression
- Boolean Expressions with Character
Operands


ASCII – American Standard Code for
Information Interchange
Contains 128 characters
9
Boolean Expression
- Boolean Expressions with Character Operands
 Characters are compared using their code values according
to certain code system.
Boolean Expression
ASCII of First
Operand
ASCII of Second
Operand
Boolean
Expression
’\’ != ’j’
92
106
true
’9’ > ’4’
57
52
true
’A’ >= ’a’
65
97
false
10
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')
11
Compound Boolean Expressions
- Logical Operators
 Truth Table for ‘and’ operator
Boolean expression P
Boolean expression Q
P && Q
TRUE
TRUE
TRUE
TRUE
FALSE
FALSE
FALSE
TRUE
FALSE
FALSE
FALSE
FALSE
 Truth Table for ‘or’ operator
Boolean expression P
Boolean expression Q
P || Q
TRUE
TRUE
TRUE
TRUE
FALSE
TRUE
FALSE
TRUE
TURE
FALSE
FALSE
FALSE
12
Compound Boolean Expressions
- Logical Operators
 Truth table for ‘not’ operator
Boolean Expression P
!Q
TRUE
FALSE
FALSE
TRUE
13
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);
14
Boolean Expression Evaluation
!('F' != 'M') || (19 > 12) && (19 < 18)
!( true ) || (true) && (false)
( false ) || (true) && (false)
( false ) || (
(
false
false
)
)
15
Flowcharts
 Graphical representation
Terminator
Process
Input/output
Condition
Connector
Flow line
16
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
17