Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012.

Slides:



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

BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
1 BUILDING JAVA PROGRAMS CHAPTER 4 CONDITIONAL EXECUTION.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
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.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Conditionals (Cont’d). 2 Nested if/else question Formula for body mass index (BMI): Write a program that produces output like the following: This program.
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.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
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
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
How to Calculate Your Body Mass Index (BMI)
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.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
1 Conditional Statements + Loops ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
Lesson thirteen Conditional Statement "if- else" ©
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 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Copyright 2010 by Pearson Education The if/else statement reading: 4.1, 4.6.
CSc 110, Autumn 2016 Lecture 9: input ; if/else Adapted from slides by Marty Stepp and Stuart Reges.
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)
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Building Java Programs Chapter 4
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Introduction to Flowcharting
Weightproblems by teenagers
Thanachat Thanomkulabut
SELECTION STATEMENTS (1)
Building Java Programs Chapter 4
Outline Altering flow of control Boolean expressions
C# Programming.
Building Java Programs
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Factoring if/else code
Relational Operators.
Decisions, decisions, decisions
Building Java Programs
Building Java Programs
Introduction to Flowcharting
Factoring if/else code
Presentation transcript:

Selection Statement – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

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

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 4 int Y; Y Is Y greater than 3? Y>3 Is Y less than 5? Y<5 Is Y between 3 and 5? (Y<5)(Y>3)&& Boolean Expression

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

Boolean variable  Used for storing Boolean value  true  False  Keyword: bool  Usage example: bool MyVar = true; //Boolean variable declaration MyVare = (X>0); //Boolean expression 7 Boolean variable

Outline 8  Boolean expression  Flowchart  if  if statement  if…else…  if…else… statement

Flowchart symbols overview  Graphical representation Terminator Process Input/output Condition Connector Flow line 9 Flowchart

Program flowchart example Start Statement1 Statement2 Statement3 Statement4 End 10 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 11 Flowchart

if statement flowchart 12 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”); Flowchart

Outline 13  Boolean expression  Flowchart  if  if statement  if…else…  if…else… statement

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;} 14 if statementtrue true

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

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

if statement with Boolean variable 17 bool Y; int X = int.Parse(Console.ReadLine()); Y = (X > 10); if (Y) console.Write(“X is greater than 10”); else console.Write(“X is less than or equal to 10”); if statement

18 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) 18

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) 19 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 20 Selfish Ratio =RecievingGiving

Outline 21  Boolean expression  Flowchart  if  if statement  if…else…  if…else… statement

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 } 22 if…else… statement

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

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

if…else… statement example 25  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…else… statement

if…else… statement example 26 Value in NOutput Even NumberIt’s even number. Odd NumberIt’s odd number. 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”); if…else… statement

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

Summary 28  Boolean expression  FlowChart  if  if statement  if…else…  if…else… statement  nested if  nested if statement  switch case  switch case statement

Any question?