DECISION MAKING STRUCTURES Computer Programming 2.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

5.04 Apply Decision Making Structures
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
Logical Operators and Conditional statements
C++ for Engineers and Scientists Third Edition
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
Visual Basic CDA College Limassol Campus COM123 Visual Basic Programming Semester C Lecture:Pelekanou Olga Week 4: Understand and implement Decisions.
Decision Statements, Short- Circuit Evaluation, Errors.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
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.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
Discussion 4 eecs 183 Hannah Westra.
5.04 Apply Decision Making Structures
Chapter 4: Control Structures I
Chapter 4: Control Structures I
Sequence, Selection, Iteration The IF Statement
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in JavaScript
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Introduction to MATLAB
Control Structures – Selection
Chapter 4: Control Structures I
Topics The if Statement The if-else Statement Comparing Strings
Quiz 1 96/07/23.
Visual Basic – Decision Statements
Selection Statements.
Chapter 4: Control Structures I (Selection)
SELECTIONS STATEMENTS
Conditionals.
The Selection Structure
Chapter 3: Selection Structures: Making Decisions
Presentation transcript:

DECISION MAKING STRUCTURES Computer Programming 2

If Statement Syntax: if (Boolean Expression) statement; Boolean Expression can be: Equivalent == Greater Than > Greater Than or Equal to >= Less Than < Less Than or Equal to <= Not Equivalent to !=

If - Else Statement Syntax – Single Statement: if (Boolean Expression) statement; else statement; Syntax – Multiple Statement: if (Boolean Expression) { statement; statement;Must have {} if more than 1 statement } else statement;

If – Else If Statement To check more than three conditions. Syntax – Multiple Statement: if (Boolean Expression) { statement; } else if (Boolean Expression) { statement; } else if (Boolean Expression) { statement; }

Switch Another way to make a decision is a switch statement. This statement is used in lieu of multiple else if statements. Syntax: switch (variable/expression) { case value: statements; break;//must have or all statements run... }

Example switch(intGrades) { case 10: case 9: strGrade="A"; break; case 8: strGrade="B"; break; case 7: strGrade = "C"; break; case 6: strGrade="D"; break; case 5: case 4: case 3: case 2: case 1: strGrade = "F"; break; }

COMPOUND BOOLEAN EXPRESSIONS Computer Programming 2

And Operator When using And or & with IF all statements MUST be true for the IF (or else if) statement to evaluate to true. So using our C# example again: if (number 5) If the number is not less than 15 AND greater than 5 this statement is false.

Or Operator Using Or if(number > 50 | number < 25) Now if any of the conditions are true, the statement evaluates to true.

Short Circuiting C# provides a way to “short circuit” long compound IF statements. The computer looks at the first statement and uses that information to decide if it needs to look at the rest of the statement. && If IsNumeric(number) && IsNumeric(num1) Then So if number is not numeric the statement is false and the computer does not waste time looking at num1. || If IsNumeric(number) || IsNumeric(num1) Then If number is numeric there is no need to check num1 because the statement is true.

Short Circuiting && (and) if (number 5) || (or) if(number > 50 || number < 25)

The Ternary Operator There is a shortcut for IF statements. This is called the ternary (three) operator. The ternary operator is a question mark (?). Syntax: varName = (comparison) ? Value if true : value if false; Example: int a = 0; int b= 10; string strName = (a < b) ? "less than 10" : "greater than 10"; //Assign a value to the string strName based on the comparison given. This is exactly how if statements are done in Excel.

Conclusion This PowerPoint provided an overview of decision making statements in Visual Studio C#.