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

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

4 Control Statements: Part 1.
Functions and Their Representations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
2. C# Language Fundamentals
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Semantic Analysis and Symbol Tables
Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Computer Programming 12 Mr. Jean April 2 nd, 2014.
© 2012 National Heart Foundation of Australia. Slide 2.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
While Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.while Loop Example #3.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
1 of 32 Images from Africa. 2 of 32 My little Haitian friend Antoine (1985)
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CS 240 Computer Programming 1
Chapter Three Arithmetic Expressions and Assignment Statements
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Decision Making with Control Structures and Statements (non-audio version) © Dr. David.
Types of selection structures
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Pointers and Arrays Chapter 12
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
PSSA Preparation.
Presentation 7: JavaScript continued Control structures and functions Fundamentals of Web-Centric Development.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Execute Blocks of Code Multiple Times Telerik Software Academy C# Fundamentals – Part 1.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Logic & program control part 3: Compound selection structures.
If and If-Else Statements 1. Motivation 2 Need to build an intelligent program for an A/C to set the temperature between deg C. How?? Too Cold Turn.
Selection Statement – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Introduction to C# Programming ณภัทร สักทอง Application Program Development.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
CONTROLLING PROGRAM FLOW
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
OBJECTIVES  Illustration of the Concept of Control Flow  Types of Control Flow  Knowledge about conditional and repetitive statements.  Make more.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
Selection Statement – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012.
Selection Statement Thanachat Thanomkulabut. Outline 2  Boolean expression  if  if statement  nested if  nested if statement  switch case  switch.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
Week 4 Program Control Structure
C# Programming: Basic Concept Part 2 Computer and Programming (204111)
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Thanachat Thanomkulabut
SELECTION STATEMENTS (1)
Control Statement Examples
OPERATORS (2) CSC 111.
Compound Assignment Operators in C++
SELECTION STATEMENTS (2)
SELECTIONS STATEMENTS
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Presentation transcript:

2 nd Semester Selection Statement Computer and Programming (204111)

2 Review Input Statement Input Statement Numeric Type Conversion Numeric Type Conversion More Arithmetic Expressions More Arithmetic Expressions Modify-n-Assign; e.g. sum += x; Modify-n-Assign; e.g. sum += x; Increment & Decrement; x++ and x-- Increment & Decrement; x++ and x-- Math Class Math Class string yourname; yourname = System.Console.ReadLine(); string yourname; yourname = System.Console.ReadLine();

3 Lab Review Statements Statements Input Statement Input 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’) 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 int Formula!

5 Outline Boolean expression ( นิพจน์ทาง ตรรกศาสตร์ ) Boolean expression ( นิพจน์ทาง ตรรกศาสตร์ ) if statement ( คำสั่งเงื่อนไข ) if statement ( คำสั่งเงื่อนไข ) nested if statement ( คำสั่งเงื่อนไขซ้อน ) nested if statement ( คำสั่งเงื่อนไขซ้อน ) switch case statement ( คำสั่งทางเลือก ) switch case statement ( คำสั่งทางเลือก )

6 Selection: Why do we need it? Everything in life involves selection Everything in life involves selection Hungry Check Money MK Steak Bar Mai

7 Boolean Expression Operators Operators Comparison Comparison 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 >= Boolean Boolean And && - Not ! And && - Not ! Or || Or ||

8 Boolean Expression Example From the equation: X 2 +9X+10 = 0 From the equation: X 2 +9X+10 = 0 How can we check that value of X is the answer for above equation? How can we check that value of X is the answer for above equation? Condition: Is the value Y an even number? Condition: Is the value Y an even number? //true if X is the answer ((X*X +9*X +10) == 0) //true if X is the answer //true if Y is even (Y%2 == 0) //true if Y is evenOR //true if Y is even (Y%2 != 1) //true if Y is even

9 Example: Boolean Expressions double x = 4.0; Expression Value x < 5.0 ___________ x > 5.0 ___________ x <= 5.0 ___________ 5.0 == x ___________ x != 5.0 ___________ true false true false true

10 Outline Boolean expression Boolean expression if statement if statement nested if statement nested if statement switch case statement switch case statement

11 if statement Execute the specific statement when the “condition” becomes true Execute the specific statement when the “condition” becomes true Syntax: Syntax: if (condition) statement; //true statement; //true if (condition) { statement1; //true statement1; //true statement2; //true statement2; //true}

12 if statement example BMI (Body Mass Index) BMI (Body Mass Index) BMI Weight Status Below 18.5 Underweight 18.5 – 24.9 Normal 25.0 – 29.9 Overweight 30.0 and Above Obese (Extremely Fat) BMI = Weight in Kilograms (Height in Meters) X (Height in Meters)

13 if…else… statement If condition is true  execute statement1 If condition is true  execute statement1 If condition is false  execute statement2 If condition is false  execute statement2 Syntax: Syntax: if (condition) statement1; //true statement1; //trueelse statement2; //false if (condition) statement1; //true statement1; //true else { statement2; //false statement2; //false statement3; //false }

14 if…else… statement example Question Question Value in variable N is Odd or Even Number? Value in variable N is Odd or Even Number? Value in N Output Even Number It’s even number. Odd Number It’s odd number. if (___________________) Console.WriteLine(“It’s even number.”); Console.WriteLine(“It’s even number.”);else Console.WriteLine(“It’s odd number.”);

15 Outline Boolean expression Boolean expression if statement if statement nested if statement nested if statement switch case statement switch case statement

16 Nested IF Overview if#1if#2 if#3 if#1if#2 if#3 else#1

17 Nested if statement int N; N = int.Parse(Console.ReadLine()); if (N >= 0) { if (N==0) if (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is zero number”); else else Console.WriteLine(“N is positive number”); Console.WriteLine(“N is positive number”);}else Console.WriteLine(“N is negative number”); Console.WriteLine(“N is negative number”); if#1 if#2

18 Nested if statement f(x) = 2x+10, x ≤ 5 x 2 +10, 5 < x ≤ 20 x 3 +10, x > 20 Range Boolean Expression x ≤ 5 (x <= 5) (x <= 5) 5 < x ≤ 20 ((5 < x) && (x <= 20)) x > 20 (x > 20)

19 Nested if statement f(x) = 2x+10, x ≤ 5 x 2 +10, 5 < x ≤ 20 x 3 +10, x > 20 double fx = 0; double x = double.Parse(Console.ReadLine()); if ( ) fx = 2*x + 10; fx = 2*x + 10; 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);

20 Outline Boolean expression Boolean expression if statement if statement nested if statement nested if statement switch case statement switch case statement

21 switch…case statement For selecting a statement where its label corresponds to the value of the switch expression. For selecting a statement where its label corresponds to the value of the switch expression. switch ( ) { case : ; ; break; [default: ; ; break; break;] } must be int, char, string must be int, char, string

22 Exercise 1: switch-case int day_num; string day_name; 1Sun 2Monday 3Tuesday 4Wednesday 5Thursday 6Friday 7Saturday How to write ”IF Statement” and ”Switch-case statement”?

23 Exercise 2 Ex1 Please input month: 5 Your month has 31 days. Input: month number (0-12) Output: #day in that month Ex2 Please input month: 2 Your month has 28 days.

24 Exercise 3 Calculate payment for Air-time usage Calculate payment for Air-time usage Operator: LEMON Input: Promotion Type & Usage time Output: Payment price Example 4 Program Promotion Type Usage time Payment price

25 Flowchart Symbols Overview Graphical representation Graphical representation Terminator Process Input/output Condition Connector Flow line

26 Program Flowchart Example START statement1 statement2 statement3 statement4 END

27 if statement flowchart START statement1 statement2 statement3 END CONDITION true false

28 Summary Boolean Expression Boolean Expression Selection Statements Selection Statements if...else... Statement if...else... Statement switch-case Statement switch-case Statement if…else… Selection Problems switch

29 Quiz 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. จงเขียนโปรแกรมเพื่อทำรับค่าอุณหภูมิองศา เซลเซียสแล้วทำการแปลงให้เป็นองศาฟาเรน ไฮท์โดยแสดงค่าออกที่หน้าจอ โปรแกรมที่เขียนขึ้นควรให้ลักษณะการใช้งาน ออกหน้าจอดังนี้ C x 9 = (F – 32) x 5 Please enter the temperature in Celsius: degrees in Celsius is equal to 95 degrees in Fahrenheit Please enter the temperature in Celsius: degrees in Celsius is equal to 95 degrees in Fahrenheit