1 nd Semester 2007 1 Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.

Slides:



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

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Selection Statement – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Java Programming Practice.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Chapter 4: Control Structures: Selection
C++ for Engineers and Scientists Third Edition
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Implementing Control Logic in C# Svetlin Nakov Telerik Corporation
Introduction to C# Programming ณภัทร สักทอง Application Program Development.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Subroutines in Computer Programming Svetlin Nakov Telerik Corporation
Introduction to Information and Computer Science Computer Programming Lecture d This material (Comp4_Unit5d) was developed by Oregon Health and Science.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
BIM313 – Advanced Programming Techniques Flow Control 1.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
CONTROLLING PROGRAM FLOW
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
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.
Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Week 8: Decisions Bryan Burlingame 21 October 2015.
Selection Statement Thanachat Thanomkulabut. Outline 2  Boolean expression  if  if statement  nested if  nested if statement  switch case  switch.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Monday Schedule Monday: Rational Equations Tuesday: Rational Equations Wednesday: In class Activity, Factoring Quiz Thursday: Quiz Friday:
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
COMP Loop Statements Yi Hong May 21, 2015.
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.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
1 INPUT/OUTPUT Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
2 nd Semester Module3 Selection Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Weightproblems by teenagers
Thanachat Thanomkulabut
.Net Programming with C#
C# Programming.
Lab5 PROGRAMMING 1 Loop chapter4.
Module5 Looping Techniques: Part 2
Module5 Looping Techniques: Part 2
SELECTIONS STATEMENTS
Iteration (Loop) partII
Selection Control Structure
Presentation transcript:

1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND

1 nd Semester Outline  Boolean expression  if  if statement  nested if  nested if statement  switch case  switch case statement

1 nd Semester Boolean Expression  Operators Comparison ==  Equal == !=  Not equal != <  Less < >  Greater > <=  Less than or equal to <= >=  Greater than or equal to >= Boolean &&  And && ||  Or || !  Not !

1 nd Semester 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

1 nd Semester 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

1 nd Semester Outline  Boolean expression  if  if statement  nested if  nested if statement  switch case  switch case statement

1 nd Semester if statement ”condition”true  Execute the specific statement when the ”condition” becomes true  Syntax: if (condition) statement; //true statement; //true if (condition) { statement1; //true statement1; //true statement2; //true statement2; //true}

1 nd Semester if statement example  BMI (Body Mass Index) BMIWeight 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)

1 nd Semester if…else… statement conditiontrue statement1  If condition is true  execute statement1 conditionfalse statement2  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 }

1 nd Semester if…else… statement example  Question Value in variable N is Odd or Even Number? Value in NOutput 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.”);

1 nd Semester Quiz  Fill the following blank  What x, y, z are called ?  Rewrite this sentence  = ? if (___________________) Console.WriteLine(“It’s even number.”); Console.WriteLine(“It’s even number.”);else Console.WriteLine(“It’s odd number.”); x1x1x1x1 x2x2x2x2 z y int Width,High; Width=10;High=5; int _______; Hint: = ? X%2 == 0

1 nd Semester Outline  Boolean expression  if  if statement  nested if  nested if statement  switch case  switch case statement

1 nd Semester 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

1 nd Semester Nested IF Overviewif#1if#2 if#3 if#1if#2 if#3 else#1 =

1 nd Semester Nested if statement f(x) = 2x+10, x ≤ 5 x 2 +10, 5 < x ≤ 20 x 3 +10, x > 20 RangeBoolean Expression x ≤ 5 (x <= 5) (x <= 5) 5 < x ≤ 20 ((5 < x) && (x <= 20)) x > 20 (x > 20)

1 nd Semester 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()); #1 if ( ) #2 fx = 2*x + 10; #3 else if ( ) #4 fx = x*x + 10; #5 else #6 fx = x*x*x + 10; #7 #8 Console.WriteLine(“f(x) = {0}”, fx); X <= 5 5 < x <= 20

1 nd Semester Outline  Boolean expression  if  if statement  nested if  nested if statement  switch case  switch case statement

1 nd Semester 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

1 nd Semester 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>

1 nd Semester Example: switch-case (2) int month; month 1January 2February 3March 4April …….. … 12December int month; Console.Write("Input Month"); month = int.Parse(Console.ReadLine()); month switch(month) 1: {case 1: 3: case 3: 5: case 5: Console.Write("This month has 31day"); break; 4: case 4: 6: case 6: Console.Write("This month has 30day"); break; : default : Console.Write ("Input again"); break; }

1 nd Semester 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

1 nd Semester 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>

1 nd Semester 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 <expression> must be int, char, string 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

1 nd Semester Flowchart Symbols Overview  Graphical representation Terminator Process Input/output Condition Connector Flow line

1 nd Semester Program Flowchart Example START statement1 statement2 statement3 statement4 END

1 nd Semester if statement flowchart statement1 if (condition) statement2; //true statement2; //true else { statement3; //false statement3; //false} statement4; true false CONDITION START statement1 statement2 statement3 statement4

1 nd Semester Quiz2 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);  Fill blanks in the program.  Write the program flowchart.

1 nd Semester Summary  Boolean Expression  Selection Statements if...else... Statement switch-case Statement if…else… Selection Problems switch

1 nd Semester Exercise1 (Homework) 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. Try to use both if and case !