Decisions, decisions, decisions

Slides:



Advertisements
Similar presentations
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Advertisements

Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Chapter 5: Control Structures II (Repetition)
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
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.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Chapter 05 (Part III) Control Statements: Part II.
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.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
A First Book of C++ Chapter 4 Selection.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection)
Topic 4: Looping Statements
More on the Selection Structure
REPETITION CONTROL STRUCTURE
Selection (also known as Branching) Jumail Bin Taliba by
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Branching Constructs Review
Control Structures II (Repetition)
JavaScript: Control Statements.
I Know What I Want to Do – Now What??
Control Structures - Repetition
Conditinoal Constructs Review
Chapter 4: Control Structures I (Selection)
IF if (condition) { Process… }
CS1100 Computational Engineering
Selection CSCE 121 J. Michael Moore.
Conditinoal Constructs Review
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 8: More on the Repetition Structure
Chapter 4 Selection.
Three Special Structures – Case, Do While, and Do Until
Chapter 6: Repetition Statements
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
Repetition Statements (Loops) - 2
CSC215 Lecture Control Flow.
Presentation transcript:

Decisions, decisions, decisions (it’s all in the bool!!)

Review – Boolean expressions Boolean expressions are created using relational and logical operators There are six relational operators – ==, >, <, >=, <=, != There are four logical operators (logical connectors) but we mostly use three &&, ||, !, ^

Assume the following variable declarations: int var1 = 10; int var2 = 15; bool result; result = (var1 == 10) ; // is a true assertion so result gets a true (non-zero) value Result = (var2 == 10); // is NOT a true assertion so result gets a false (0) value

Assume the following variable declarations: int var1 = 10; int var2 = 15; bool result; result = (var1 == 10) && (var1 != var2); // value of result ?? result = (var2 < var1) || (var2 != var1); // value of result?? result = (var2 > var1) ! (var2 == 25); // value of result??

Use of Boolean expressions to control operations Decision structures If (condition) { true statements } else false statements

While structure while ( condition ) true statement; or { true statements; }

Three Considerations when using Iterative control structures Initialize the control variable – prior to entering the control structure the controlling variable MUST be initialized (given a starting value) Test the controlling variable in the condition – if not sure use a constant for the limit then substitute a limiting variable Modify the control variable within the controlled block of code – otherwise you may generate an “infinite loop”.

Some examples: int limit = 10; int ctr = 0; while( ctr < 10 ) { cout << “counter = “ << ctr << endl; ctr++; } OR cout << “counter = “ << ctr++ << endl;

} while( ctr < limit ); The do – while – the condition (test) is after the execution of the block. Example: do { cout << “counter = “ << ctr << endl; ctr++; } while( ctr < limit ); Note the test is after the block and there is a semi-colon at the end of the condition.

The BIG 3 Summary and Comparison

And the switch() Used for multiple tests Sometimes used instead of nested if – else Uses an ordinal expression (variable) instead of a Boolean Uses four terms (or words) – in no special order – switch() - starts the switch structure and defines the ordinal statement case – identifies the beginning of the programming sequence to be executed break – terminates the execution of the local code block default –executes if no match is found

switch() example switch(resp.at(0)) do { cout << "Enter 1, 2, 3 or <E>xit: "; getline(cin,resp); switch(resp.at(0)) { // start of switch block case '1': cout << "A one was entered" << endl; break; case '2': cout << "A two was typed in" << endl; case '3': cout << "A three was encountered" << endl; case 'E': case 'e': default: cout << "ERROR - Will Robinson" << endl; cout << "Enter 1, 2, 3 or <E>xit: "; getline(cin, resp); }while((resp.at(0)!= '1')&&(resp.at(0)!= '2')&&(resp.at(0)!= '3')&&(resp.at(0)!= 'E')&&(resp.at(0)!= 'e')); } }while( (resp.at(0)!= 'E')&&(resp.at(0)!= 'e') );

Check out Rock, Paper, Scissors