CPS120: Introduction to Computer Science Decision Making in Programs.

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
If Statements & Relational Operators Programming.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
C++ for Engineers and Scientists Third Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
UNIT II Decision Making And Branching Decision Making And Looping
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 4 Selection Structures: Making Decisions.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Operations Lecture 9.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Chapter 05 (Part III) Control Statements: Part II.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Control Structures CPS120: Introduction to Computer Science Lecture 5.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
CPS120 Introduction to Computer Science Iteration (Looping)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 4 Program Control Structure
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
Decision Statements, Short- Circuit Evaluation, Errors.
CPS120: Introduction to Computer Science Decision Making in Programs.
Decision Making and Branching
CPS120 Introduction to Computer Science Exam Review Lecture 18.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
CPS120 Introduction to Computer Science
CNG 140 C Programming (Lecture set 3)
A mechanism for deciding whether an action should be taken
Lecture 3- Decision Structures
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
CPS120: Introduction to Computer Science
Chapter 4: Control Structures I (Selection)
Control Statements Paritosh Srivastava.
Controlling Program Flow
Presentation transcript:

CPS120: Introduction to Computer Science Decision Making in Programs

Decision Making In Computers  A circuit quite simply allows one out of two choices to be made depending on its inputs  When decisions are made in a computer program, they are simply the result of a computation in which the final result is either TRUE or FALSE  The value zero (0) is considered to be FALSE by C++. Any positive or negative value is considered to be TRUE

Programming & Decisions  Practically all computer programs, when modeled with a flowchart, demonstrate that branching occurs within their algorithms.

Decision Making in C++ 1. if statement 2. switch statement 3. ? conditional operator statement 4. goto statement

Using Relational Operators  Relational operators provide the tools with which programs make decisions with true and false evaluations == equal to NOTE: this is two equals symbols next to each other, not to be confused with the assignment operator, = > greater than = greater than or equal to <= less than or equal to != not equal to

Using Logical Operators  When complex decisions must be coded into an algorithm, it may be necessary to "chain together" a few relational expressions (that use relational operators)  This is done with logical operators (also called Boolean operators.) && is the logical AND operator || is the logical OR operator ! is the logical NOT operator

Truth Tables  Use this truth table to determine the results of the logical operators. In this table, 1 represents TRUE and 0 represents FALSE.  Note that the ! symbol (the logical NOT operator) changes a TRUE to a FALSE

Order of Logical Operations  Logical operators may be mixed within evaluation statements but the following order of preference must be respected: 1. NOT operator (!) 2. AND operator (&&) 3. OR operator (||)  Short-circuit evaluation in C++ allows the compiler to quickly evaluate a logical expression in some cases without having to completely evaluate each part of the expression

Complete order of operations  The complete order of operations including all of the arithmetic, relational, and logical operators including all of the basic arithmetic, relational, & logical operators is: *, /, % +, -, =, ==, != ! && ||

Compare and Branch  A program can instruct a computer to compare two items and do something based on a match or mismatch which, in turn, redirect the sequence of programming instructions.  There are two forms:  IF-THEN  IF-THEN-ELSE

Levels of Complexity for if  Simple if statement  if … else statement  Nested if … else statement  else..if ladder

IF-THEN Test condition p falsetrue Entry Exit True statement a

Use the “IF” structure  Practically all computer languages have some sort of if structure. In C++, the if structure is a one-way selection structure: if (number == 3) { cout << "The value of number is 3"; }  IF structures use a control expression to determine if the code in the braces is to be executed or not

Compound Conditionals  You must use the AND operator (&&) to form a compound relational expression: if (0 < number && number < 10) { cout << number << " is greater than 0 but less than 10." << endl; }

Coding IF Structures  Place a semicolon at the end of each statement within the braces, which can contain many executable statements  Use curly braces around the body of an IF structure even if there is only one statement

IF…ELSE falsetrue Entry Exit Test condition p “true” statement a “false” statement a

General Form if (test expression) { True-block statements; } else { False-block statements; } next statement;

The If … Else statement  Two-way selection structure since either the block of code after the "if" part will be executed or the block of code after the "else" part will be executed  The “If" part is executed if the control expression is TRUE while the "else" part is executed if the "if" part is FALSE, guaranteeing one part of the expression to be executed or the other

if… else if Ladder: General Form if (condition 1) statement 1; else if (condition 2) statement 2; else if (condition 3) statement 3; else if (condition n) statement n; else default statement; statement x;

if/else if statement  As soon as a true condition is found, the statement associated with it is executed and control is transferred to the statement after the ladder  The else clause is optional just as it is with an if statement. if (number > 10) { cout << number << " is greater than 10." << endl; } else if (number = 6) { cout << number << " is greater than 5 and less than 10." << endl; } else { cout << number << "must be less than 6." << endl; } number = number + 1;

Nested If Statements  If structures and if/else statements can be nested within one another in order to model complex decision structures.  Use the braces and semicolons properly when coding such structures.

General Form if (test condition 1) { // true-block1 statements if (test condition 2) { true-block2 statements; } else { false-block2 statements; } else { false-block1 statements; }

Switch Structure  The switch structure is a multiple-selection structure that allows even more complicated decision statements than a two-way if/else structure allows.  It chooses one of the "cases" depending on the result of the control expression.  Only variables with the INT or CHAR data types may be used in the control expressions (i.e. parentheses) of switch statements.  Single quotes must be used around CHAR variables  Single quotes are NOT used around the integer values

Sample Switch Structure switch (character_entered) { case ‘A’ : cout << “The character entered was A.\n”; break; case ‘B’: cout << “The character entered was B.\n”; default: cout << “Illegal entry\n”; }

Switch Structure  The break statement must be used within each case if you do not want following cases to evaluate once one case is found. When the break statement is executed within a switch, C++ will execute the next statement outside of the switch statement.  The default case, if present, will result if none of the prior cases apply

Grouping Cases switch (number) { case 1: case 3: case 5: case 7: case 9: cout << number << " is an even number." << endl; break; case 2: case 4: case 6: case 8: cout << number << " is an odd number. " << endl; break; default: cout << number << " is not a value between or including 1 and 9." << endl; break; }

Conditional Operator  General form: conditional expression ? exp-1:exp-2 ;  The conditional expression is evaluated first. If the result is non-zero, exp-1 is evaluated and is returned as the value of the conditional expression. Otherwise, exp-2 is evaluated and its value is returned f=(c = = 'y') ? 1: 0;

GOTO Statement  Unconditional branching GOTO label; … label: statement;  Not recommended but may be used occasionally