Selection Statement Thanachat Thanomkulabut. Outline 2  Boolean expression  if  if statement  nested if  nested if statement  switch case  switch.

Slides:



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

Logic & program control part 3: Compound selection structures.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4: Selections.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1.
Selection Statement – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012.
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Java Programming Practice.
Chapter 4: Control Structures: Selection
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
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.
C# Programming: Expressions, conditions, and repetitions Computer and Programming.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
Selection Structures (if & switch statements) (CS1123)
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.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Other Conditional Methods. Conditional Operator Conditional Operator (?:) Conditional operator ( ?: ) – Ternary operator: 3 arguments expression1 ? expression2.
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
CONTROLLING PROGRAM FLOW
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Compound Statements If you want to do more than one statement if an IF- else case, you can form a block of statements, or compound statement, by enclosing.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
How to Calculate Your Body Mass Index (BMI)
Boolean Expressions and if-else Statements Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
Selection Statement – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
COMP Loop Statements Yi Hong May 21, 2015.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
2 nd Semester Module3 Selection Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
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
Chapter 4 Selections © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Thanachat Thanomkulabut
SELECTION STATEMENTS (1)
Other Conditional Methods
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
SELECTION STATEMENTS (2)
C# Programming.
Module5 Looping Techniques: Part 2
Module5 Looping Techniques: Part 2
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
SELECTIONS STATEMENTS
Iteration (Loop) partII
Iteration (Loop) part II
Presentation transcript:

Selection Statement Thanachat Thanomkulabut

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

Boolean Expression  Operators  Comparison == Equal == != Not equal != < Less < > Greater > <= Less than or equal to <= >= Greater than or equal to >=  Boolean && And && || Or || ! Not ! 3 Boolean Expression 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: X 2 +9X+10 = 0  How can we check that value of X is the answer for above equation?  Condition: Is value Y 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 4 Boolean Expression

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 5 Boolean Expression false true

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

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

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

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

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

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

if statement Test I  Selfish Ratio RatioOutput More than 1You are selfish 1You break even Less than 1You are giver 12 Selfish Ratio =RecievingGiving

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

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

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

if…else… statement example 16  Write the program which check input number.  input : integer number  output : message to inform that number is odd or even. Value in NOutput Even NumberIt’s even number. Odd NumberIt’s odd number. if statement

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

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

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

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 20 Nested if statementif#2

int N; N = int.Parse(Console.ReadLine()); if (N > 0) Console.WriteLine(“N is positive number”); Console.WriteLine(“N is positive number”);else if (N==0) if (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is zero number”); else else Console.WriteLine(“N is negative number”); Console.WriteLine(“N is negative number”); if#1 if#2 21 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”); >= 0 False N is negative number N >= 0 True True N is zero number >= 0 False N is positive number 22

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

Exercise 1: Separated IF (simple) 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”);} else {Console.WriteLine(“I don’t like noodle”);} 1. Have you eaten lunch? 2. Do you like noodle? 24

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”);} else {Console.WriteLine(“I don’t like friedrice”);} } 1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice? Exercise 2: Related two IF (full version) 25

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”);} 1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice? Exercise 2: Related two IF (short version) 26

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”);} else {Console.WriteLine(“I don’t like fried rice”);} 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? Exercise 3: Nested two IF (short version) 27

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

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

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

switch…case statement  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 31 switch...case statement

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

Test III 33  Write the program which show numbers of day in each months  input : Abbreviation of months  output : numbers of day in each months InputOutput Jan31 Feb28 or 29 Mar31 Api30 May31... switch...case statement

34 Example: switch-case (2) string month; Console.Write("Input Month"); month = Console.ReadLine(); month switch(month) “Jan”: {case “Jan”: “Mar”: case “Mar”: “May”: case “May”: Console.Write("This month has 31days"); break; “Apr”: case “Apr”: “Jun”: case “Jun”: Console.Write("This month has 30days"); break; : default : Console.Write (“Wrong Input"); break; } 34 switch...case statement InputOutput Jan31 Feb28 or 29 Mar31 Api30 May31...

35 Example: switch-case (3) 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); break; : default: Console.Write("Try again"); break; } must be int, char, string must be int, char, string int day_num; day_num= int.Parse(Console.ReadLine()); switch( day_num ) { case 1: Console.Write ("Sunday"); break; case 2: console.Write("Monday"); break; : default : Console.Write(“Try again"); break; } <expression> int version char version <constant-expression> 35 switch...case statement

36 Example: switch-case (4) 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); break; : default: Console.Write("Try again"); break; } must be int, char, string must be int, char, string string version <expression> <constant-expression> 36 switch...case statement

Convert switch-case to if else int a; a= int.Parse(Console.ReadLine()); switch (a) { case 1 : case 2 : Console.WriteLine("Hi"); break; case 3 : case 4 : Console.WriteLine("Hello"); break; default : Console.WriteLine("Bye"); break; } 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 switch (a) { case 1 : case 2 : Console.WriteLine("Hi"); break; } switch version without default 37 switch...case statement

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

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

Program Flowchart Example Start Statement1 Statement2 Statement3 Statement4 End 40 Flowchart

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

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

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

Any question?