2 nd semester 2005 1 selection statement computer and programming (204111)

30
1 2 nd Semester 2005 Selection Selection Statement Statement Computer and Programming Computer and Programming (204111) (204111)

Upload: donte-pattison

Post on 14-Dec-2015

219 views

Category:

Documents


2 download

TRANSCRIPT

12nd Semester 2005

Selection Selection StatementStatement

Computer and ProgrammingComputer and Programming

(204111)(204111)

2

ReviewReview

Input StatementInput Statement

Numeric Type ConversionNumeric Type Conversion More Arithmetic ExpressionsMore Arithmetic Expressions

Modify-n-Assign; e.g. Modify-n-Assign; e.g. sum += x;sum += x; Increment & Decrement; Increment & Decrement; x++x++ and and x--x--

MathMath Class Class

string yourname;string yourname;yourname = yourname = System.Console.ReadLine()System.Console.ReadLine();;string yourname;string yourname;yourname = yourname = System.Console.ReadLine()System.Console.ReadLine();;

3

Lab ReviewLab Review

StatementsStatements Input StatementInput Statement

Fahrenheit to Celsius Conversion Fahrenheit to Celsius Conversion

int value;string input;input = System.Console.ReadLine();value = int.Parse(input);

int value;string input;input = System.Console.ReadLine();value = int.Parse(input);

4

Lab Review (Cont’)Lab Review (Cont’)using System;namespace C2F_DegreeConverter { class C2F_DegreeConverterClass { static void Main() { string input; float Ctemp, Ftemp; Console.Write("Please input farenheit: "); input = Console.ReadLine(); Ftemp = int.Parse(input); Ctemp = (Ftemp-32)*5/9; Console.WriteLine("{0} degrees in farenheit is" + "equivalent to {1} in celcius", Ftemp, Ctemp); } }}

using System;namespace C2F_DegreeConverter { class C2F_DegreeConverterClass { static void Main() { string input; float Ctemp, Ftemp; Console.Write("Please input farenheit: "); input = Console.ReadLine(); Ftemp = int.Parse(input); Ctemp = (Ftemp-32)*5/9; Console.WriteLine("{0} degrees in farenheit is" + "equivalent to {1} in celcius", Ftemp, Ctemp); } }}

Obtaining “F”

Converse string to intFormula!

5

OutlineOutline Boolean expression (Boolean expression (นิ�พจนิ�ทางนิ�พจนิ�ทาง

ตรรกศาสตร�ตรรกศาสตร�)) ifif statement ( statement (คำ�าส��งเง��อนิไขคำ�าส��งเง��อนิไข)) nested ifnested if statement ( statement (คำ�าส��งเง��อนิไขคำ�าส��งเง��อนิไข

ซ้�อนิซ้�อนิ)) switch caseswitch case statement ( statement (คำ�าส��งทางคำ�าส��งทาง

เลื�อกเลื�อก))

6

Selection: Why do we Selection: Why do we need it?need it?

Everything in life involves selectionEverything in life involves selection

HungryHungry Check MoneyCheck Money MKMK

SteakSteak

Bar MaiBar Mai

7

Boolean ExpressionBoolean Expression OperatorsOperators

ComparisonComparison Equal Equal ==== Not equal Not equal !=!= Less Less << Greater Greater >> Less than or equal to Less than or equal to <=<= Greater than or equal to Greater than or equal to >=>=

BooleanBoolean And And &&&& - - Not Not !! Or Or ||||

8

Boolean Expression Boolean Expression ExampleExample

From the equation: XFrom the equation: X22+9X+10 = 0+9X+10 = 0 How can we check that value of X is the How can we check that value of X is the

answer for above equation? answer for above equation?

Condition: Is the value Y an even Condition: Is the value Y an even number?number?

((X*X +9*X +10) == 0) //true if X is the //true if X is the answeranswer

(Y%2 == 0) //true if Y is even//true if Y is even

OROR

(Y%2 != 1) //true if Y is even//true if Y is even

9

Example: Boolean Example: Boolean ExpressionsExpressions

double x = 4.0;double x = 4.0; Expression ValueExpression Valuex < 5.0 x < 5.0 ______________________x > 5.0 x > 5.0 ______________________x <= 5.0 x <= 5.0 ______________________5.0 == x 5.0 == x ______________________x != 5.0 x != 5.0 ______________________

truefalsetrue

false

true

10

OutlineOutline Boolean expressionBoolean expression ifif statementstatement nested ifnested if statement statement switch caseswitch case statement statement

11

if statementif statement Execute the specific statement Execute the specific statement

when the when the “condition”“condition” becomes becomes truetrue

Syntax:Syntax:if if (condition)(condition)

statement;statement; //true//true

if if (condition)(condition) {{

statement1; statement1; //true//true

statement2; statement2; //true//true

}}

12

if statement exampleif statement example

BMI (Body Mass Index)BMI (Body Mass Index)

BMIBMI Weight StatusWeight Status

Below 18.5Below 18.5 UnderweightUnderweight

18.5 – 24.918.5 – 24.9 NormalNormal

25.0 – 29.925.0 – 29.9 OverweightOverweight

30.0 and Above30.0 and Above Obese (Extremely Fat)Obese (Extremely Fat)

BMI = Weight in KilogramsWeight in Kilograms

(Height in Meters) X (Height in Meters)(Height in Meters) X (Height in Meters)

13

if…else… statementif…else… statement If If conditioncondition is is truetrue execute execute

statement1statement1 If If conditioncondition is is falsefalse execute execute

statement2statement2 Syntax:Syntax:

if if (condition)(condition)

statement1; statement1; //true//true

elseelse

statement2; statement2; //false//false

if if (condition)(condition)

statement1; statement1; //true//true

elseelse { {

statement2; statement2; //false//false

statement3; statement3; //false//false

}}

14

if…else… statement if…else… statement exampleexample

QuestionQuestion Value in variable N is Odd or Even Value in variable N is Odd or Even

Number?Number?Value in NValue in N OutputOutputEven NumberEven Number It’s even number.It’s even number.

OddOdd NumberNumber It’s odd number.It’s odd number.if if (___________________)(___________________)

Console.WriteLine(“It’s even Console.WriteLine(“It’s even number.”);number.”);

elseelse

Console.WriteLine(“It’s odd Console.WriteLine(“It’s odd number.”);number.”);

15

OutlineOutline Boolean expressionBoolean expression ifif statement statement nested ifnested if statement statement switch caseswitch case statement statement

16

Nested IF OverviewNested IF Overviewif#1if#1

if#2if#2if#3if#3

if#1if#1

if#2if#2

if#3if#3

else#1else#1

17

Nested if statementNested if statement

int N;int N;N = int.Parse(Console.ReadLine());N = int.Parse(Console.ReadLine());

if (N >= 0) if (N >= 0) {{ if (N==0)if (N==0) Console.WriteLine(“N is zero number”);Console.WriteLine(“N is zero number”); elseelse Console.WriteLine(“N is positive number”); Console.WriteLine(“N is positive number”); }}else else Console.WriteLine(“N is negative number”);Console.WriteLine(“N is negative number”);

if#1if#1

if#2if#2

18

Nested if statementNested if statement

f(x) =f(x) =

2x+10, x ≤ 52x+10, x ≤ 5

xx22+10, 5 < x ≤ 20+10, 5 < x ≤ 20

xx33+10, x > 20+10, x > 20

RangeRange Boolean ExpressionBoolean Expression

x ≤ 5x ≤ 5 (x <= 5)(x <= 5)

5 < x ≤ 205 < x ≤ 20 ((5 < x) && (x <= 20))((5 < x) && (x <= 20))

x > 20x > 20 (x > 20)(x > 20)

19

Nested if statementNested if statement

f(x) =f(x) =2x+10, x ≤ 52x+10, x ≤ 5xx22+10, 5 < x ≤ 20+10, 5 < x ≤ 20xx33+10, x > 20+10, x > 20

double fx = 0;double fx = 0;double x = double.Parse(Console.ReadLine());double x = double.Parse(Console.ReadLine());

if ( )if ( ) fx = 2*x + 10;fx = 2*x + 10;else if ( )else if ( ) fx = x*x + 10;fx = x*x + 10; else else fx = x*x*x + 10;fx = x*x*x + 10;

Console.WriteLine(“f(x) = {0}”, fx);Console.WriteLine(“f(x) = {0}”, fx);

20

OutlineOutline Boolean expressionBoolean expression ifif statement statement nested ifnested if statement statement switch caseswitch case statement statement

21

switch…case statementswitch…case statement For selecting a statement where its For selecting a statement where its

label corresponds to the value of the label corresponds to the value of the switch switch expressionexpression..switch (<expression><expression>)

{ case <constant-<constant-expression>expression>: <statements>;<statements>; break;break; [default: <statements>;<statements>; break;break;]}<expression><expression> must be int, char, string must be int, char, string

22

Exercise 1: switch-caseExercise 1: switch-caseint day_num;int day_num; string day_name;string day_name;

11 SunSun

22 MondayMonday

33 TuesdayTuesday

44 WednesdayWednesday

55 ThursdayThursday

66 FridayFriday

77 SaturdaySaturday

How to write ”How to write ”IF StatementIF Statement” and ”” and ”Switch-case statementSwitch-case statement”?”?

23

Exercise 2Exercise 2

Ex1Ex1

Please input month: Please input month: 55

Your month has 31 Your month has 31 days.days.

Input: month number (0-12)

Output: #day in that month 

Ex2Ex2

Please input month: Please input month: 22

Your month has 28 Your month has 28 days.days.

24

Exercise 3Exercise 3 Calculate payment for Air-time Calculate payment for Air-time

usageusage Operator: LEMONOperator: LEMON

Input: Input: Promotion Type & Usage timePromotion Type & Usage timeOutput: Output: Payment pricePayment price

Example 4Example 4ProgramProgram

Promotion TypePromotion Type

Usage timeUsage timePayment pricePayment price

25

Flowchart Symbols Flowchart Symbols OverviewOverview

Graphical representationGraphical representation

Terminator

Process

Input/output

Condition

Connector

Flow line

26

Program Flowchart Program Flowchart ExampleExample

START

statement1statement1

statement2statement2

statement3statement3

statement4statement4

END

27

if statement flowchartif statement flowchart

START

statement1statement1

statement2statement2

statement3statement3

END

CONDITIONCONDITION

truetrue

falsefalse

28

SummarySummary Boolean ExpressionBoolean Expression Selection StatementsSelection Statements

if...else... Statementif...else... Statement switch-case Statementswitch-case Statement

if…else…if…else…

Selection ProblemsSelection Problems

switchswitch

29

QuizQuiz

1 .1 . จงเต�มคำ�าลืงในิช่�องว่�างจงเต�มคำ�าลืงในิช่�องว่�างusing System;namespace C2F_DegreeConverter { class C2F_DegreeConverterClass { static void Main() { string input; // declare a var as string for ReadLine() float Ctemp, Ftemp; // temp in Celsius & Fahrenheit Console.Write("Please input farenheit: "); // ……(1)…… input = Console.ReadLine(); // ……(2)…… Ftemp = int.Parse(input); // ……(3)…… Ctemp = (Ftemp-32)*5/9; // ……(4)…… Console.WriteLine("{0} degrees in farenheit is" + "equivalent to {1} in celcius", Ftemp, Ctemp); // ……(5)…… } }}

using System;namespace C2F_DegreeConverter { class C2F_DegreeConverterClass { static void Main() { string input; // declare a var as string for ReadLine() float Ctemp, Ftemp; // temp in Celsius & Fahrenheit Console.Write("Please input farenheit: "); // ……(1)…… input = Console.ReadLine(); // ……(2)…… Ftemp = int.Parse(input); // ……(3)…… Ctemp = (Ftemp-32)*5/9; // ……(4)…… Console.WriteLine("{0} degrees in farenheit is" + "equivalent to {1} in celcius", Ftemp, Ctemp); // ……(5)…… } }}

30

2.2. จงเข ยนิโปรแกรมเพ��อท�าร�บคำ�าอ&ณหภู*ม�องศาจงเข ยนิโปรแกรมเพ��อท�าร�บคำ�าอ&ณหภู*ม�องศาเซ้ลืเซ้ ยสแลื�ว่ท�าการแปลืงให�เป+นิองศาเซ้ลืเซ้ ยสแลื�ว่ท�าการแปลืงให�เป+นิองศาฟาเรนิไฮท�โดยแสดงคำ�าออกท �หนิ�าจอฟาเรนิไฮท�โดยแสดงคำ�าออกท �หนิ�าจอ

โปรแกรมท �เข ยนิข/0นิคำว่รให�ลื�กษณะการใช่�งานิโปรแกรมท �เข ยนิข/0นิคำว่รให�ลื�กษณะการใช่�งานิออกหนิ�าจอด�งนิ 0ออกหนิ�าจอด�งนิ 0

C x 9 = (F – 32) x 5C x 9 = (F – 32) x 5

Please enter the temperature in Celsius: 3232 degrees in Celsius is equal to 95 degrees in Fahrenheit

Please enter the temperature in Celsius: 3232 degrees in Celsius is equal to 95 degrees in Fahrenheit