SlideShare une entreprise Scribd logo
1  sur  45
Microsoft Visual C# 2010
       Fourth Edition



        Chapter 4
     Making Decisions
Objectives
• Understand logic-planning tools and decision making
• Learn how to make decisions using the if statement
• Learn how to make decisions using the if-else
  statement
• Use compound expressions in if statements




Microsoft Visual C# 2010: Fourth Edition           2
Objectives (cont'd.)
• Make decisions using the switch statement
• Use the conditional operator
• Use the NOT operator
• Learn to avoid common errors when making
  decisions
• Learn about decision-making issues in GUI
  programs




Microsoft Visual C# 2010: Fourth Edition        3
Understanding Logic-Planning Tools
          and Decision Making
• Pseudocode
    – Tool that helps programmers plan a program’s logic by
      writing plain English statements
• Flowchart
    – You write the steps in diagram form as a series of
      shapes connected by arrows
• Sequence structure
    – One step follows another unconditionally
    – Sometimes, logical steps do not follow in an
      unconditional sequence

Microsoft Visual C# 2010: Fourth Edition                   4
Understanding Logic-Planning Tools
     and Decision Making (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   5
Understanding Logic-Planning Tools
     and Decision Making (cont'd.)
• Decision structure
    – Involves choosing between alternative courses of
      action based on some value within a program
    – All computer decisions are yes-or-no decisions
          • When reduced to their most basic form




Microsoft Visual C# 2010: Fourth Edition                 6
Understanding Logic-Planning Tools
     and Decision Making (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   7
Making Decisions Using the if
                  Statement
• if statement
    – Used to make a single-alternative decision
• Block
    – One or more statements contained within a pair of
      curly braces




Microsoft Visual C# 2010: Fourth Edition                  8
Making Decisions Using the if
              Statement (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   9
Making Decisions Using the if
              Statement (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   10
Making Decisions Using the if
              Statement (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   11
Making Decisions Using the if
              Statement (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   12
Making Decisions Using the if
              Statement (cont'd.)
• Nested if
    – One decision structure is contained within another




Microsoft Visual C# 2010: Fourth Edition                   13
Making Decisions Using the if
              Statement (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   14
Making Decisions Using the if
              Statement (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   15
Making Decisions Using the if-else
              Statement
• Dual-alternative decisions
    – Have two possible outcomes
• if-else statement
    – Used to perform one action when a Boolean
      expression evaluates as true
          • And an alternate action when it evaluates as false




Microsoft Visual C# 2010: Fourth Edition                         16
Making Decisions Using the if-else
         Statement (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   17
Making Decisions Using the if-else
         Statement (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   18
Using Compound Expressions in if
             Statements
• You can combine multiple decisions into a single if
  statement
    – Using a combination of AND and OR operators




Microsoft Visual C# 2010: Fourth Edition            19
Using the Conditional AND Operator

• Conditional AND operator
    – Determines whether two expressions are both true
    – Written as two ampersands (&&)
    – You must include a complete Boolean expression on
      each side of the operator
• Short-circuit evaluation
    – Expressions in each part of an AND expression are
      evaluated only as much as necessary
          • To determine whether the entire expression is true or
            false

Microsoft Visual C# 2010: Fourth Edition                        20
Using the Conditional AND Operator
                (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   21
Using the Conditional OR Operator

• Conditional OR operator
    – Used when you want some action to occur even if only
      one of two conditions is true
    – Written as ||




Microsoft Visual C# 2010: Fourth Edition               22
Using the Conditional OR Operator
                 (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   23
Using the Logical AND and OR
                   Operators
• Boolean logical AND (&) and Boolean logical
  inclusive OR (|) operators
    – Work just like their && and || (conditional AND and
      OR) counterparts
    – They do not support short-circuit evaluation
    – Can lead to a side effect (unintended consequence)
• Avoid writing expressions that contain side effects




Microsoft Visual C# 2010: Fourth Edition                24
Combining AND and OR Operators




Microsoft Visual C# 2010: Fourth Edition   25
Making Decisions Using the switch
               Statement
• switch structure
    – Tests a single variable against a series of exact
      matches
• Keywords
    – switch, case, break, and default
• “No fall through rule”
    – Not allowing code to reach the end of a case
    – Not allowed in C#
    – Use a break statement at the end of each case

Microsoft Visual C# 2010: Fourth Edition                  26
Making Decisions Using the switch
           Statement (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   27
Making Decisions Using the switch
           Statement (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   28
Making Decisions Using the switch
           Statement (cont'd.)
• A switch does not need a default case
    – Good programming practice to include one
• You can use multiple labels to govern a list of
  statements




Microsoft Visual C# 2010: Fourth Edition            29
Making Decisions Using the switch
           Statement (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   30
Using an Enumeration with a switch
             Statement
• Using an enumeration with a switch structure can
  often be convenient




Microsoft Visual C# 2010: Fourth Edition             31
Using an Enumeration with a switch
          Statement (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   32
Using the Conditional Operator

• Conditional operator
    – Used as an abbreviated version of the if-else
      statement
    – A ternary operator
• Syntax
          testExpression ? trueResult : falseResult;




Microsoft Visual C# 2010: Fourth Edition               33
Using the NOT Operator

• NOT operator
    – Written as an exclamation point (!)
    – Negates the result of any Boolean expression




Microsoft Visual C# 2010: Fourth Edition             34
Avoiding Common Errors When
                Making Decisions
• Most frequent errors include:
    – Using the assignment operator instead of the
      comparison operator
    – Inserting a semicolon after the Boolean expression in
      an if statement
    – Failing to block a set of statements with curly braces
    – Failing to include a complete Boolean expression on
      each side of an && or || operator



Microsoft Visual C# 2010: Fourth Edition                   35
Performing Accurate and Efficient
               Range Checks
• Range check
    – A series of if statements that determine whether a
      value falls within a specified range
• Problem




Microsoft Visual C# 2010: Fourth Edition                   36
Performing Accurate and Efficient
           Range Checks (cont'd.)
• Solution
          if(saleAmount >= 1000)
             commissionRate = 0.08;
          else if(saleAmount >= 500)
             commissionRate = 0.06;
          else commissionRate = 0.05;




Microsoft Visual C# 2010: Fourth Edition   37
Using && and II Appropriately

• Problem
    – Print an error message when an employee’s hourly
      pay rate is under $5.65 and when an employee’s
      hourly pay rate is over $60
• Solution
          if(payRate < 5.65 || payRate > 60)
             Console.WriteLine (“Error in pay rate”);




Microsoft Visual C# 2010: Fourth Edition                 38
Using the ! Operator Correctly

• Problem
    – Make sure that if the sales code is not ‘A’ or ‘B’, the
      customer gets a 10% discount
• Solutions
          if(salesCode != ‘A’ && salesCode != ‘B’)
             discount = 0.10;

          if(!(salesCode == ‘A’ || salesCode == ‘B’))
             discount = 0.10;




Microsoft Visual C# 2010: Fourth Edition                        39
Decision-Making Issues in GUI
                    Programs
• Making a decision within a method in a GUI
  application is no different from making one in a
  console application
• You can use if, if…else, and switch
  statements in the same ways




Microsoft Visual C# 2010: Fourth Edition             40
Decision-Making Issues in GUI
                Programs (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   41
Decision-Making Issues in GUI
                Programs (cont'd.)




Microsoft Visual C# 2010: Fourth Edition   42
You Do It

• Activities to explore
    – Using if-else Statements
    – Using AND and OR Logic




Microsoft Visual C# 2010: Fourth Edition         43
Summary

• A flowchart is a pictorial tool that helps you
  understand a program’s logic
• You use an if statement to make a single-
  alternative decision
• When you make a dual-alternative decision, you
  can use an if-else statement
• Conditional AND operator determines whether two
  expressions are both true
• Use the conditional OR operator when you want
  some action to occur when one or both of two
  conditions are true
Microsoft Visual C# 2010: Fourth Edition            44
Summary (cont'd.)

• AND operators take precedence
• The switch statement tests a single variable
  against a series of exact matches
• The conditional operator is used as an abbreviated
  version of the if-else statement
• You use the NOT operator to negate the result of
  any Boolean expression
• Common errors when making decisions
• Decisions within GUI methods are just like ones in
  console applications
Microsoft Visual C# 2010: Fourth Edition               45

Contenu connexe

Tendances

Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Warren0989
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
Computer Programming
Computer ProgrammingComputer Programming
Computer ProgrammingBurhan Fakhar
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#Prasanna Kumar SM
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06Terry Yoast
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12Terry Yoast
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit orderMalikireddy Bramhananda Reddy
 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#Prasanna Kumar SM
 
9781439035665 ppt ch06
9781439035665 ppt ch069781439035665 ppt ch06
9781439035665 ppt ch06Terry Yoast
 
Testing Model Transformations
Testing Model TransformationsTesting Model Transformations
Testing Model Transformationsmiso_uam
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13Terry Yoast
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- Cswatisinghal
 

Tendances (20)

Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...
 
Chap02
Chap02Chap02
Chap02
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
JavaScript functions
JavaScript functionsJavaScript functions
JavaScript functions
 
Chapter2
Chapter2Chapter2
Chapter2
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
Intake 38_1
Intake 38_1Intake 38_1
Intake 38_1
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
 
Programming in c notes
Programming in c notesProgramming in c notes
Programming in c notes
 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#
 
9781439035665 ppt ch06
9781439035665 ppt ch069781439035665 ppt ch06
9781439035665 ppt ch06
 
Testing Model Transformations
Testing Model TransformationsTesting Model Transformations
Testing Model Transformations
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- C
 

Similaire à Csc153 chapter 04

C# Tutorial MSM_Murach chapter-01-slides
C# Tutorial MSM_Murach chapter-01-slidesC# Tutorial MSM_Murach chapter-01-slides
C# Tutorial MSM_Murach chapter-01-slidesSami Mut
 
Refinery Blending Problems by Engr. Adefami Olusegun
Refinery Blending Problems by Engr. Adefami OlusegunRefinery Blending Problems by Engr. Adefami Olusegun
Refinery Blending Problems by Engr. Adefami OlusegunEngr. Adefami Segun, MNSE
 
First draft programming c++
First draft programming c++First draft programming c++
First draft programming c++藝輝 王
 
Using_PTC_Windchill_and_Creo_for_Creating_Customer-Driven_Product_Variants
Using_PTC_Windchill_and_Creo_for_Creating_Customer-Driven_Product_VariantsUsing_PTC_Windchill_and_Creo_for_Creating_Customer-Driven_Product_Variants
Using_PTC_Windchill_and_Creo_for_Creating_Customer-Driven_Product_VariantsVladimir Pezel
 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdfssusere19c741
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfmadihamaqbool6
 
Devry cis-170-c-i lab-6-of-7-menu
Devry cis-170-c-i lab-6-of-7-menuDevry cis-170-c-i lab-6-of-7-menu
Devry cis-170-c-i lab-6-of-7-menunoahjamessss
 
Devry cis-170-c-i lab-6-of-7-menu
Devry cis-170-c-i lab-6-of-7-menuDevry cis-170-c-i lab-6-of-7-menu
Devry cis-170-c-i lab-6-of-7-menucskvsmi44
 
Systematic error management - we ported rudder to zio
Systematic error management - we ported rudder to zioSystematic error management - we ported rudder to zio
Systematic error management - we ported rudder to ziofanf42
 
The Business side of a Software Architect - SATURN
The Business side of a Software Architect - SATURNThe Business side of a Software Architect - SATURN
The Business side of a Software Architect - SATURNTomer Peretz
 
COM1407: Structured Program Development
COM1407: Structured Program Development COM1407: Structured Program Development
COM1407: Structured Program Development Hemantha Kulathilake
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code cleanBrett Child
 
Branching and Merging Practices
Branching and Merging Practices Branching and Merging Practices
Branching and Merging Practices Rajesh Kumar
 
Tech Ed09 Net Best Practices Bijoy New
Tech Ed09 Net Best Practices Bijoy NewTech Ed09 Net Best Practices Bijoy New
Tech Ed09 Net Best Practices Bijoy Newrsnarayanan
 
Top Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To KnowTop Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To KnowKathy Brown
 
Programming_Lecture_1.pptx
Programming_Lecture_1.pptxProgramming_Lecture_1.pptx
Programming_Lecture_1.pptxshoaibkhan716300
 
Converting to the latest COBOL Compiler made simple with the right tools
Converting to the latest COBOL Compiler made simple with the right toolsConverting to the latest COBOL Compiler made simple with the right tools
Converting to the latest COBOL Compiler made simple with the right toolsDevOps for Enterprise Systems
 
Cis 170 ilab 2 of 7
Cis 170 ilab 2 of 7Cis 170 ilab 2 of 7
Cis 170 ilab 2 of 7solutionjug4
 

Similaire à Csc153 chapter 04 (20)

C# Tutorial MSM_Murach chapter-01-slides
C# Tutorial MSM_Murach chapter-01-slidesC# Tutorial MSM_Murach chapter-01-slides
C# Tutorial MSM_Murach chapter-01-slides
 
Refinery Blending Problems by Engr. Adefami Olusegun
Refinery Blending Problems by Engr. Adefami OlusegunRefinery Blending Problems by Engr. Adefami Olusegun
Refinery Blending Problems by Engr. Adefami Olusegun
 
First draft programming c++
First draft programming c++First draft programming c++
First draft programming c++
 
Using_PTC_Windchill_and_Creo_for_Creating_Customer-Driven_Product_Variants
Using_PTC_Windchill_and_Creo_for_Creating_Customer-Driven_Product_VariantsUsing_PTC_Windchill_and_Creo_for_Creating_Customer-Driven_Product_Variants
Using_PTC_Windchill_and_Creo_for_Creating_Customer-Driven_Product_Variants
 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdf
 
Devry cis-170-c-i lab-6-of-7-menu
Devry cis-170-c-i lab-6-of-7-menuDevry cis-170-c-i lab-6-of-7-menu
Devry cis-170-c-i lab-6-of-7-menu
 
Devry cis-170-c-i lab-6-of-7-menu
Devry cis-170-c-i lab-6-of-7-menuDevry cis-170-c-i lab-6-of-7-menu
Devry cis-170-c-i lab-6-of-7-menu
 
Systematic error management - we ported rudder to zio
Systematic error management - we ported rudder to zioSystematic error management - we ported rudder to zio
Systematic error management - we ported rudder to zio
 
Chapter 01.PPT
Chapter 01.PPTChapter 01.PPT
Chapter 01.PPT
 
The Business side of a Software Architect - SATURN
The Business side of a Software Architect - SATURNThe Business side of a Software Architect - SATURN
The Business side of a Software Architect - SATURN
 
COM1407: Structured Program Development
COM1407: Structured Program Development COM1407: Structured Program Development
COM1407: Structured Program Development
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
Rdlc (1)
Rdlc (1)Rdlc (1)
Rdlc (1)
 
Branching and Merging Practices
Branching and Merging Practices Branching and Merging Practices
Branching and Merging Practices
 
Tech Ed09 Net Best Practices Bijoy New
Tech Ed09 Net Best Practices Bijoy NewTech Ed09 Net Best Practices Bijoy New
Tech Ed09 Net Best Practices Bijoy New
 
Top Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To KnowTop Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To Know
 
Programming_Lecture_1.pptx
Programming_Lecture_1.pptxProgramming_Lecture_1.pptx
Programming_Lecture_1.pptx
 
Converting to the latest COBOL Compiler made simple with the right tools
Converting to the latest COBOL Compiler made simple with the right toolsConverting to the latest COBOL Compiler made simple with the right tools
Converting to the latest COBOL Compiler made simple with the right tools
 
Cis 170 ilab 2 of 7
Cis 170 ilab 2 of 7Cis 170 ilab 2 of 7
Cis 170 ilab 2 of 7
 

Dernier

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 

Dernier (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 

Csc153 chapter 04

  • 1. Microsoft Visual C# 2010 Fourth Edition Chapter 4 Making Decisions
  • 2. Objectives • Understand logic-planning tools and decision making • Learn how to make decisions using the if statement • Learn how to make decisions using the if-else statement • Use compound expressions in if statements Microsoft Visual C# 2010: Fourth Edition 2
  • 3. Objectives (cont'd.) • Make decisions using the switch statement • Use the conditional operator • Use the NOT operator • Learn to avoid common errors when making decisions • Learn about decision-making issues in GUI programs Microsoft Visual C# 2010: Fourth Edition 3
  • 4. Understanding Logic-Planning Tools and Decision Making • Pseudocode – Tool that helps programmers plan a program’s logic by writing plain English statements • Flowchart – You write the steps in diagram form as a series of shapes connected by arrows • Sequence structure – One step follows another unconditionally – Sometimes, logical steps do not follow in an unconditional sequence Microsoft Visual C# 2010: Fourth Edition 4
  • 5. Understanding Logic-Planning Tools and Decision Making (cont'd.) Microsoft Visual C# 2010: Fourth Edition 5
  • 6. Understanding Logic-Planning Tools and Decision Making (cont'd.) • Decision structure – Involves choosing between alternative courses of action based on some value within a program – All computer decisions are yes-or-no decisions • When reduced to their most basic form Microsoft Visual C# 2010: Fourth Edition 6
  • 7. Understanding Logic-Planning Tools and Decision Making (cont'd.) Microsoft Visual C# 2010: Fourth Edition 7
  • 8. Making Decisions Using the if Statement • if statement – Used to make a single-alternative decision • Block – One or more statements contained within a pair of curly braces Microsoft Visual C# 2010: Fourth Edition 8
  • 9. Making Decisions Using the if Statement (cont'd.) Microsoft Visual C# 2010: Fourth Edition 9
  • 10. Making Decisions Using the if Statement (cont'd.) Microsoft Visual C# 2010: Fourth Edition 10
  • 11. Making Decisions Using the if Statement (cont'd.) Microsoft Visual C# 2010: Fourth Edition 11
  • 12. Making Decisions Using the if Statement (cont'd.) Microsoft Visual C# 2010: Fourth Edition 12
  • 13. Making Decisions Using the if Statement (cont'd.) • Nested if – One decision structure is contained within another Microsoft Visual C# 2010: Fourth Edition 13
  • 14. Making Decisions Using the if Statement (cont'd.) Microsoft Visual C# 2010: Fourth Edition 14
  • 15. Making Decisions Using the if Statement (cont'd.) Microsoft Visual C# 2010: Fourth Edition 15
  • 16. Making Decisions Using the if-else Statement • Dual-alternative decisions – Have two possible outcomes • if-else statement – Used to perform one action when a Boolean expression evaluates as true • And an alternate action when it evaluates as false Microsoft Visual C# 2010: Fourth Edition 16
  • 17. Making Decisions Using the if-else Statement (cont'd.) Microsoft Visual C# 2010: Fourth Edition 17
  • 18. Making Decisions Using the if-else Statement (cont'd.) Microsoft Visual C# 2010: Fourth Edition 18
  • 19. Using Compound Expressions in if Statements • You can combine multiple decisions into a single if statement – Using a combination of AND and OR operators Microsoft Visual C# 2010: Fourth Edition 19
  • 20. Using the Conditional AND Operator • Conditional AND operator – Determines whether two expressions are both true – Written as two ampersands (&&) – You must include a complete Boolean expression on each side of the operator • Short-circuit evaluation – Expressions in each part of an AND expression are evaluated only as much as necessary • To determine whether the entire expression is true or false Microsoft Visual C# 2010: Fourth Edition 20
  • 21. Using the Conditional AND Operator (cont'd.) Microsoft Visual C# 2010: Fourth Edition 21
  • 22. Using the Conditional OR Operator • Conditional OR operator – Used when you want some action to occur even if only one of two conditions is true – Written as || Microsoft Visual C# 2010: Fourth Edition 22
  • 23. Using the Conditional OR Operator (cont'd.) Microsoft Visual C# 2010: Fourth Edition 23
  • 24. Using the Logical AND and OR Operators • Boolean logical AND (&) and Boolean logical inclusive OR (|) operators – Work just like their && and || (conditional AND and OR) counterparts – They do not support short-circuit evaluation – Can lead to a side effect (unintended consequence) • Avoid writing expressions that contain side effects Microsoft Visual C# 2010: Fourth Edition 24
  • 25. Combining AND and OR Operators Microsoft Visual C# 2010: Fourth Edition 25
  • 26. Making Decisions Using the switch Statement • switch structure – Tests a single variable against a series of exact matches • Keywords – switch, case, break, and default • “No fall through rule” – Not allowing code to reach the end of a case – Not allowed in C# – Use a break statement at the end of each case Microsoft Visual C# 2010: Fourth Edition 26
  • 27. Making Decisions Using the switch Statement (cont'd.) Microsoft Visual C# 2010: Fourth Edition 27
  • 28. Making Decisions Using the switch Statement (cont'd.) Microsoft Visual C# 2010: Fourth Edition 28
  • 29. Making Decisions Using the switch Statement (cont'd.) • A switch does not need a default case – Good programming practice to include one • You can use multiple labels to govern a list of statements Microsoft Visual C# 2010: Fourth Edition 29
  • 30. Making Decisions Using the switch Statement (cont'd.) Microsoft Visual C# 2010: Fourth Edition 30
  • 31. Using an Enumeration with a switch Statement • Using an enumeration with a switch structure can often be convenient Microsoft Visual C# 2010: Fourth Edition 31
  • 32. Using an Enumeration with a switch Statement (cont'd.) Microsoft Visual C# 2010: Fourth Edition 32
  • 33. Using the Conditional Operator • Conditional operator – Used as an abbreviated version of the if-else statement – A ternary operator • Syntax testExpression ? trueResult : falseResult; Microsoft Visual C# 2010: Fourth Edition 33
  • 34. Using the NOT Operator • NOT operator – Written as an exclamation point (!) – Negates the result of any Boolean expression Microsoft Visual C# 2010: Fourth Edition 34
  • 35. Avoiding Common Errors When Making Decisions • Most frequent errors include: – Using the assignment operator instead of the comparison operator – Inserting a semicolon after the Boolean expression in an if statement – Failing to block a set of statements with curly braces – Failing to include a complete Boolean expression on each side of an && or || operator Microsoft Visual C# 2010: Fourth Edition 35
  • 36. Performing Accurate and Efficient Range Checks • Range check – A series of if statements that determine whether a value falls within a specified range • Problem Microsoft Visual C# 2010: Fourth Edition 36
  • 37. Performing Accurate and Efficient Range Checks (cont'd.) • Solution if(saleAmount >= 1000) commissionRate = 0.08; else if(saleAmount >= 500) commissionRate = 0.06; else commissionRate = 0.05; Microsoft Visual C# 2010: Fourth Edition 37
  • 38. Using && and II Appropriately • Problem – Print an error message when an employee’s hourly pay rate is under $5.65 and when an employee’s hourly pay rate is over $60 • Solution if(payRate < 5.65 || payRate > 60) Console.WriteLine (“Error in pay rate”); Microsoft Visual C# 2010: Fourth Edition 38
  • 39. Using the ! Operator Correctly • Problem – Make sure that if the sales code is not ‘A’ or ‘B’, the customer gets a 10% discount • Solutions if(salesCode != ‘A’ && salesCode != ‘B’) discount = 0.10; if(!(salesCode == ‘A’ || salesCode == ‘B’)) discount = 0.10; Microsoft Visual C# 2010: Fourth Edition 39
  • 40. Decision-Making Issues in GUI Programs • Making a decision within a method in a GUI application is no different from making one in a console application • You can use if, if…else, and switch statements in the same ways Microsoft Visual C# 2010: Fourth Edition 40
  • 41. Decision-Making Issues in GUI Programs (cont'd.) Microsoft Visual C# 2010: Fourth Edition 41
  • 42. Decision-Making Issues in GUI Programs (cont'd.) Microsoft Visual C# 2010: Fourth Edition 42
  • 43. You Do It • Activities to explore – Using if-else Statements – Using AND and OR Logic Microsoft Visual C# 2010: Fourth Edition 43
  • 44. Summary • A flowchart is a pictorial tool that helps you understand a program’s logic • You use an if statement to make a single- alternative decision • When you make a dual-alternative decision, you can use an if-else statement • Conditional AND operator determines whether two expressions are both true • Use the conditional OR operator when you want some action to occur when one or both of two conditions are true Microsoft Visual C# 2010: Fourth Edition 44
  • 45. Summary (cont'd.) • AND operators take precedence • The switch statement tests a single variable against a series of exact matches • The conditional operator is used as an abbreviated version of the if-else statement • You use the NOT operator to negate the result of any Boolean expression • Common errors when making decisions • Decisions within GUI methods are just like ones in console applications Microsoft Visual C# 2010: Fourth Edition 45