Conditional statements (1) +variable scope Based on slides by - - PowerPoint PPT Presentation

conditional statements 1 variable scope
SMART_READER_LITE
LIVE PREVIEW

Conditional statements (1) +variable scope Based on slides by - - PowerPoint PPT Presentation

Conditional statements (1) +variable scope Based on slides by Asst. Prof. Kitsana Waiyamai, Ph.D. Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand http://kdl.cpe.ku.ac.th If-Then-Else Statement 1


slide-1
SLIDE 1

If-Then-Else Statement 1

Conditional statements (1) +variable scope

Based on slides by

  • Asst. Prof. Kitsana Waiyamai, Ph.D.
  • Dept. of Computer Engineering

Faculty of Engineering, Kasetsart University Bangkok, Thailand

http://kdl.cpe.ku.ac.th

slide-2
SLIDE 2

If-Then-Else Statement 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");

slide-3
SLIDE 3

If-Then-Else Statement 3

Data type: bool

bool

A kind of logical data type with value either true or false

Console.WriteLine(2+3 == 5); Console.WriteLine(3*4 == 7*4/2); Console.WriteLine(100>90); Console.WriteLine("Pascal" == "Pascal");

True False True True

slide-4
SLIDE 4

If-Then-Else Statement 4

Boolean Expression

Either true or false depending on the result of the boolean expression evaluation Boolean expression consists of two operands joined by a relational operator

What can the operands be? Arithmetic expressions, e.g., 1+1*2/3 Characters, e.g. 'a' == 'b' Strings, e.g. "Pascal" == "Pascal" Variables

slide-5
SLIDE 5

If-Then-Else Statement 5

Boolean Expression

  • Relational Operators

They are used to compare data items of the compatible type

Relational Operator in Pascal Meaning == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to

slide-6
SLIDE 6

If-Then-Else Statement 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 Mary is a boy"); }

slide-7
SLIDE 7

If-Then-Else Statement 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 false

slide-8
SLIDE 8

If-Then-Else Statement 8

Boolean Expression

  • Boolean Expressions with Character

Operands

ASCII – American Standard Code for

Information Interchange

Contains 128 characters

slide-9
SLIDE 9

If-Then-Else Statement 9

Boolean Expression

  • Boolean Expressions with Character

Operands

Characters are compared using their ASCII code values.

Boolean Expression ’\’ != ’j’ ’9’ > ’4’ ’{’ >= ’}’ ASCII of First Operand 92 57 123 ASCII of Second Operand 106 52 125 Result true true false

slide-10
SLIDE 10

If-Then-Else Statement 10

Boolean Expression

  • Boolean Expressions with String

Operands

Strings are compared character by character from left to right

Boolean Expression Result Remark "Z" > "Other" true ‘Z’ has larger ASCII value then ‘O’ "small" > "smaller" false ‘small’ does not have the sixth character "Space" < "NoSpace" true The character ‘ ’ has smaller ASCII value than the character ‘N’ "Timmy" >= "Tommy" false The character ‘i’ of the first operand has smaller ASCII value then ‘o’ of the second

  • perand
slide-11
SLIDE 11

If-Then-Else Statement 11

Compound Boolean Expressions

Simple Boolean Expressions may be joined by logical/conditional operators to form compound Boolean expressions Conditional Operators

&& - conditional AND || - conditional OR ^ - logical exclusive OR (logical XOR) !

  • logical NOT

Examples

(age >= 18) && (age <= 20) (grade == 'F') || (grade == 'W')

slide-12
SLIDE 12

If-Then-Else Statement 12

Compound Boolean Expressions

  • Conditional Operators

Truth Table for conditional AND operator (&&) Truth Table for conditional 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 TRUE FALSE FALSE FALSE

slide-13
SLIDE 13

If-Then-Else Statement 13

Compound Boolean Expressions

  • Conditional Operators

Truth Table for logical exclusive OR operator Truth table for logical NOT operator

Boolean Expression P ! P

TRUE FALSE FALSE TRUE

Boolean expression P Boolean expression Q P ^ Q TRUE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE

slide-14
SLIDE 14

If-Then-Else Statement 14

Compound Boolean Expressions

  • Logical Operators

Logical AND (&) Logical OR (|) Similar to conditional AND (&&) and conditional OR (||) operators in terms of truth tables and associativity (left to right).

slide-15
SLIDE 15

If-Then-Else Statement 15

Difference between conditional and logical operators

conditional AND (&&) and conditional OR (||) stops evaluating if the leftmost condition is either false or true, respectively.

can reduce execution time

logical AND (&) and logical OR (|) always evaluate both sides of operands

slide-16
SLIDE 16

If-Then-Else Statement 16

Precedence and associativity of operators

Operators Associativity Type () ++ -- left to right right to left parentheses unary postfix ++ -- + - ! (type) right to left unary prefix * / % left to right multiplicative + - left to right additive < <= > >= left to right relational == != left to right equality & left to right logical AND ^ left to right logical XOR | left to right logical OR && left to right conditional AND || left to right conditional OR ?: right to left conditional = += -= *= /= %= right to left assignment

slide-17
SLIDE 17

If-Then-Else Statement 17

Boolean Expression Evaluation

!('F'!='M')||(19 > 12)&&(19 < 18) !( true )||(19 > 12)&&(19 < 18) ( false )||(19 > 12)&&(19 < 18) ( false )||( true )&&(19 < 18) ( false )||( true )&&( false ) ( false ) || ( false ) ( false )

slide-18
SLIDE 18

If-Then-Else Statement 18

Evaluation: Short-cutting (1)

!('F'=='M')||(19 > 12)&&(19 < 18) !( false )||(19 > 12)&&(19 < 18) ( true )||(19 > 12)&&(19 < 18) ( true ) (true) || ?? true

slide-19
SLIDE 19

If-Then-Else Statement 19

Evaluation: Short-cutting (2)

!('F‘!='M')||(19 < 12)&&(19 > 18) !( true )||(19 < 12)&&(19 > 18) ( false )||(19 < 12)&&(19 > 18) ( false ) ( false )||( false )&&(19 > 18) ( false )||( false )

slide-20
SLIDE 20

If-Then-Else Statement 20

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

slide-21
SLIDE 21

If-Then-Else Statement 21

Flowcharts

Graphical representation

Terminator Process Input/output Condition Connector Flow line

slide-22
SLIDE 22

If-Then-Else Statement 22

Conditional Statements

  • If-then statement

Example

If a student has score higher than 90,

then print he is “a very good student,”

  • therwise, do something else

Score > 90

‘very good student’ True False

slide-23
SLIDE 23

If-Then-Else Statement 23

If statement

Syntax:

if (<boolean value>) statement;

statement is executed, if the boolean value is true. If more than one statements are to be executed when the boolean value is true, we need to group them with { }. e.g.,

if (<boolean value>) { statement1; statement2; }

slide-24
SLIDE 24

If-Then-Else Statement 24

IF

class MainClass { static void Main() { int num; Console.Write("Please input a number :"); num = int.Parse(Console.ReadLine()); if(num < 0) Console.WriteLine("negative"); if(num == 0) Console.WriteLine("zero"); if(num > 0) Console.WriteLine("positive"); } }

slide-25
SLIDE 25

If-Then-Else Statement 25

Conditional Statements

  • If-then-else statement

If-then-else statement

When there is only one of two alternative statements to

be executed

Example

If Mark variable is greater

than or equal to 50,

  • utput ‘you pass the exam’,
  • therwise, output ‘you fail the exam’

Mark >= 50 Output ‘pass’ Output ‘fail’

True False

slide-26
SLIDE 26

If-Then-Else Statement 26

IF – ELSE

if(condition) statement1; else statement2;

condition False True Statement2 Statement1

slide-27
SLIDE 27

If-Then-Else Statement 27

Conditional Statements

  • If-then-else statement

When the condition is true, the statement after the condition will be executed; otherwise, the statement after the reserved word else will be executed

class PassOrFail { static void Main() { Console.Write("Enter mark: "); int mark = int.Parse(Console.ReadLine()); if(mark >= 50) Console.WriteLine("you pass the exam"); else Console.WriteLine("you fail the exam"); } }

slide-28
SLIDE 28

If-Then-Else Statement 28

IF – Then – Else (1)

class MainClass { static void Main() { int sum = 10; int num; Console.Write("Please input a number :"); num = int.Parse(Console.ReadLine()); if(num < 0) num = 0; else num = 20; sum += num; Console.WriteLine("Sum = " + sum); } }

slide-29
SLIDE 29

If-Then-Else Statement 29

Conditional Statements

  • If-then-else statement

How to produce the following results?

Enter mark: 80 Congratulation! You mark is greater than 50. You pass the examination ! Enter mark: 30 Sorry! You mark is less than 50. You fail the examination ! Mark >= 50

Statement 1 Statement 1 Statement 2 Statement 3 Statement 2 Statement 3

slide-30
SLIDE 30

If-Then-Else Statement 30

Conditional Statements

  • If-then-else statement

Compound statement

  • After the condition
  • r else, enclose them

by '{' and '}' to form a compound statement class PassOrFail { static void Main() { int mark = int.Parse(Console.ReadLine()); if(mark >= 50) { Console.WriteLine("congratulations!"); Console.WriteLine("your mark is greater than 50"); Console.WriteLine(‘you pass the exam’); } else { Console.WriteLine("sorry!"); Console.WriteLine("your mark is less than 50"); Console.WriteLine("you fail the exam"); } } }

slide-31
SLIDE 31

If-Then-Else Statement 31

class PassOrFail { static void Main() { int mark = int.Parse(Console.ReadLine()); if(mark >= 50) { int a; a = int.Parse(Console.ReadLine()); Console.WriteLine(a+100); } else { string line = Console.ReadLine(); Console.WriteLine("Sorry " + line); } } }

Scope: Declaring variables inside { } blocks

We can declare new variables inside the if-part or the else- part of the conditional statement. They will be available

  • nly in that block,

which is their scope.

scope of "line" scope of "a"

slide-32
SLIDE 32

If-Then-Else Statement 33

Conditional Statements

  • Nested If statement

If statement may be inserted in the then-part or the else-part of another if statement to form a nested if statement.

Condition 2 Statement a2 Statement a1 Condition 3 Statement b2 Statement b1 Condition 1

slide-33
SLIDE 33

If-Then-Else Statement 34

Conditional Statements

  • Nested If statement

The program receives three different numbers and displays the smallest number

static void Main(string[] args) { int a,b,c; a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); c = int.Parse(Console.ReadLine()); Console.Write("The smallest is "); if(a<b) if(a<c) Console.WriteLine(a); else Console.WriteLine(c); else if(b<c) Console.WriteLine(b); else Console.WriteLine(c); }

a<c c a b<c c b a < b True True True False False False