Thanachat Thanomkulabut

Slides:



Advertisements
Similar presentations
2 nd Semester Selection Statement Computer and Programming (204111)
Advertisements

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.
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
1 BUILDING JAVA PROGRAMS CHAPTER 4 CONDITIONAL EXECUTION.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
Implementing Control Logic in C# Svetlin Nakov Telerik Corporation
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Introduction to C# Programming ณภัทร สักทอง Application Program Development.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
Computer Science Selection Structures.
C# Programming: Expressions, conditions, and repetitions Computer and Programming.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
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.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
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.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
Copyright 2010 by Pearson Education The if/else statement reading: 4.1, 4.6.
1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved
How to Calculate Your Body Mass Index (BMI)
Chapter 3 Control Statements
Repeating Code Multiple Times
Elementary Programming
LESSON 4 Decision Control Structure
Decisions Chapter 4.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Introduction to programming in java
Building Java Programs Chapter 4
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 6: Conditional Statements and Loops
SELECTION STATEMENTS (1)
Control Statement Examples
Building Java Programs Chapter 4
Introduction to Programming in Java
Other Conditional Methods
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
SELECTION STATEMENTS (2)
Enumerations CS Fall 1999 Enumerations.
C# Programming.
Self study.
Building Java Programs
Module5 Looping Techniques: Part 2
Module5 Looping Techniques: Part 2
Lecture Notes – Week 3 Lecture-1
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
Factoring if/else code
Building Java Programs
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Iteration (Loop) partII
Building Java Programs
Building Java Programs
Building Java Programs
Iteration (Loop) part II
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
Factoring if/else code
Selection Control Structure
Presentation transcript:

Thanachat Thanomkulabut Selection Statement Thanachat Thanomkulabut

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

Boolean Expression Operators Comparison Boolean Equal == Not equal != Less < Greater > Less than or equal to <= Greater than or equal to >= Boolean And && Or || Not ! ! Exclamation mark 0 and 0 = 0 0 and 1 = 0 1 and 0 = 0 1 and 1 = 1 0 or 0 = 0 0 or 1 = 1 1 or 0 = 1 1 or 1 = 1 not 0 = 1 not 1 = 0

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

Example: Boolean Expressions double x = 4.0; Expression Value x < 5.0 ___________ x > 5.0 ___________ x <= 5.0 ___________ 5.0 == x ___________ x != 5.0 ___________ (3!=4)&&(7<5) ___________ (4>4)||(5<=10) ___________ true false true false true false true

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

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

if statement if (condition) statement; condition if (condition) { False True Statement if (condition) { statement; }

if statement if (condition) { statement1; statement2; condition } True False True Statement1 Statement2

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

if statement example double BMI = W /(H*H); if(BMI<18.5) Weight Status Below 18.5 Underweight 18.5 – 24.9 Normal 25.0 – 29.9 Overweight 30.0 and Above Obese (Extremely Fat) 11 BMI = Weight in Kilograms (Height in Meters) X (Height in Meters) double BMI = W /(H*H); if(BMI<18.5) Console.WriteLine(“Underweight”); if(BMI>=18.5 && BMI<=24.9) Console.WriteLine(“Normal”); if(BMI>=25.0 && BMI<=29.9) Console.WriteLine(“Overweight”); if(BMI>=30.0) Console.WriteLine(“Obese”);

Test I Selfish Ratio Recieving Selfish Ratio = Giving Ratio Output if statement Test I 12 Selfish Ratio Selfish Ratio = Recieving Giving Ratio Output More than 1 You are selfish 1 You break even Less than 1 You are giver

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

if…else… statement if (condition) statement1; //true else if statement if…else… statement condition False True Statement2 Statement1 if (condition) statement1; //true else statement2; //false

if…else… statement if (condition) statement1; //true else { if statement if…else… statement condition False True Statement2 Statement1 Statement3 if (condition) statement1; //true else { statement2; //false statement3; //false }

if…else… statement example if statement if…else… statement example Write the program which check input number. input : integer number output : message to inform that number is odd or even. Value in N Output Even Number It’s even number. Odd Number It’s odd number.

if…else… statement example if statement if…else… statement example Value in N Output Even Number It’s even number. Odd Number It’s odd number. if(n%2 == 0) Console.WriteLine(“It’s even number”); else Console.WriteLine(“It’s odd number”);

Test II Write the program which find the value of function if statement Test II Write the program which find the value of function input : number output : f(x) x x2+1 x<0 x+2 x≥0

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

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

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

Nested if statement   int N; N = int.Parse(Console.ReadLine()); 3 -5 -5 3 N is positive number N is zero number N is negative number 22 int N; N = int.Parse(Console.ReadLine()); if (N >= 0) { if (N==0) Console.WriteLine(“N is zero number”); else Console.WriteLine(“N is positive number”); } Console.WriteLine(“N is negative number”); 3 >= 0 0 >= 0 -5 >= 0  False True  False True

Nested if statement int N; N = int.Parse(Console.ReadLine()); 3 -4 3 -4 N is negative number N is zero number N is positive number 23 int N; N = int.Parse(Console.ReadLine()); if (N > 0) Console.WriteLine(“N is positive number”); else if (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is negative number”); 3 > 0 0 > 0 -4 > 0 True False True False

Exercise 1: Separated IF (simple) 24 1. Have you eaten lunch? 2. Do you like noodle? if (eaten==true) {Console.WriteLine(“ I’ve already eaten”);} else {Console.WriteLine(“ I’ve not eat yet”);} if (like_noodle==true) {Console.WriteLine(“I like noodle”);} {Console.WriteLine(“I don’t like noodle”);}

Exercise 2: Related two IF (full version) 25 1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice? if (like_noodle==true) {Console.WriteLine(“I like noodle”);} else { Console.WriteLine(“I don’t like noodle”); if (like_friedrice==true) {Console.WriteLine(“I like friedrice”);} {Console.WriteLine(“I don’t like friedrice”);} }

Exercise 2: Related two IF (short version) 26 1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice? if (like_noodle==true) {Console.WriteLine(“I like noodle”);} else if (like_friedrice==true) {Console.WriteLine(“I like friedrice”);} else {Console.WriteLine(“I don’t like friedrice”);}

Exercise 3: Nested two IF (short version) 27 1. Do you like noodle? 1.1 If you like noodle, do you love Sen-Yai? 1.2 If you don’t like noodle, do you like fried rice? if (like_noodle==true) {Console.WriteLine(“I like noodle”); if (love_SenYai==true) {Console.WriteLine(“I love Sen-Yai”);} else {Console.WriteLine(“I don’t love Sen-Yai”);} } else if (like_friedrice==true) {Console.WriteLine(“I like fried rice”);} {Console.WriteLine(“I don’t like fried rice”);}

Nested if statement example Write the program which show student’s grade input : score of student output : Score Grade 80 – 100 A 70 – 79 B 60 – 69 C 50 – 59 D 0 – 49 F

Nested if statement Test III Write the program which calculation value of following function input : value of x output : function value of x according with ... f(x) = 2x+10, x ≤ 5 x2+10, 5 < x ≤ 20 x3+10, x > 20

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

เปรียบเทียบ “Read” แต่ละแบบ Console.Read() ผู้ใช้ใส่สตริงใดๆเข้ามา รอจนกว่าผู้ใช้จะเคาะ Return แล้วค่อยทำคำสั่งต่อไป ให้ค่า int กลับมา เป็น int ที่แทนรหัส ASCII ของ character ตัวหน้าสุด Console.Read() ถูกเรียกอีกครั้ง จะให้ค่า ASCII ของ character ถัดไป เมื่อถึงตัวสุดท้าย รอผู้ใช้เคาะ Return อีกครั้งจึงจะทำคำสั่งต่อไป Console.ReadLine() ให้ค่าสตริงตามที่ผู้ใช้พิมพ์เข้ามา รอจนกว่าเคาะ Return แล้วทำคำสั่งต่อไป Console.ReadKey() ให้ค่า character ที่ผู้ใช้พิมพ์ และทำคำสั่งต่อไปทันที

using System;     class Program     {         public static void Main(string[] args)         {             Console.Write("Input any string: ");             char ch = (char)(Console.Read());             Console.WriteLine(ch);             ch = (char)(Console.Read());             Console.WriteLine(ch);             ch = (char)(Console.Read());             Console.WriteLine(ch);             ch = (char)(Console.Read());             Console.WriteLine(ch);             ch = (char)(Console.Read());             Console.WriteLine(ch);             Console.WriteLine("Hello World!");             Console.ReadKey(true);         }     }

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

Example: switch-case (1) switch...case statement Example: switch-case (1) 34 int day_num; Console.Write("Input the day"); day_num = int.Parse(Console.ReadLine()); switch(day_num) {case 1: Console.Write ("Today is Sunday"); break; case 2: Console.Write("Today is Monday"); : default : Console.Write ("I don’t know"); } int day_num; day name 1 Sunday 2 Monday 3 Tuesday 4 Wednesday 5 Thursday 6 Friday 7 Saturday 2 1 30 <expression> <constant-expression>

Test III Write the program which show numbers of day in each months switch...case statement Test III Write the program which show numbers of day in each months input : Abbreviation of months output : numbers of day in each months Input Output Jan 31 Feb 28 or 29 Mar Api 30 May ...

Example: switch-case (2) switch...case statement Example: switch-case (2) 36 string month; Console.Write("Input Month"); month = Console.ReadLine(); switch(month) {case “Jan”: case “Mar”: case “May”: Console.Write("This month has 31days"); break; case “Apr”: case “Jun”: Console.Write("This month has 30days"); default : Console.Write (“Wrong Input"); } Input Output Jan 31 Feb 28 or 29 Mar Api 30 May ...

Example: switch-case (3) switch...case statement Example: switch-case (3) 37 <expression> must be int, char, string char version int version char op; Console.Write("Select + - / * :"); op=char.Parse(Console.ReadLine()); switch(op) { case '+': Console.Write("{0}+{1}={2}", x,y,x+y); break; case '-': Console.Write("{0}-{1}={2}", x,y,x-y); : default: Console.Write("Try again"); } int day_num; day_num= int.Parse(Console.ReadLine()); switch(day_num ) { case 1: Console.Write ("Sunday"); break; case 2: console.Write("Monday"); : default : Console.Write(“Try again"); } <expression> <constant-expression>

Example: switch-case (4) switch...case statement Example: switch-case (4) 38 <expression> must be int, char, string string version string op; Console.Write("Select + - / * :"); op=Console.ReadLine(); switch(op) { case “+”: Console.Write("{0}+{1}={2}", x,y,x+y); break; case “-”: Console.Write("{0}-{1}={2}", x,y,x-y); : default: Console.Write("Try again"); } <expression> <constant-expression>

Convert switch-case to if else switch...case statement Convert switch-case to if else 39 switch version with default if (a == 1 || a == 2) Console.WriteLine("Hi"); else if (a == 3 || a == 4) Console.WriteLine("Hello"); else Console.WriteLine("Bye"); if else version int a; a= int.Parse(Console.ReadLine()); switch (a) { case 1 : case 2 : Console.WriteLine("Hi"); break; case 3 : case 4 : Console.WriteLine("Hello"); default : Console.WriteLine("Bye"); } switch (a) { case 1 : case 2 : Console.WriteLine("Hi"); break; } switch version without default

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

Flowchart Symbols Overview 41 Graphical representation Terminator Process Input/output Condition ข้าวหลามตัด = diamond Connector Flow line

Program Flowchart Example 42 Start Statement1 Statement2 Statement3 Statement4 End

if statement flowchart Start 43 statement1; if (condition) statement2; //true else { statement3; //false } statement4; Statement1 Condition true false Statement2 Statement3 Statement4 End

if statement flowchart Start n= int.Parse(Console.ReadLine()); if (n>0) n= 2*n+5; else { Console.Write(“Go”); n = n%4; } n=int.Parse(Console.ReadLine()); n>0 false true Console. Write(“Go”); n=2*n+5; n=n%4; End

Test IV Write flow chart which accord to following program n= int.Parse(Console.ReadLine()); n = n%5; if (n==2) Console.WriteLine(“Remainder is 2”); else { n = n*10; } Console.WriteLine(“Program End”);

Any question?