CONTROL STRUCTURES (SELECTION). PROGRAM COMPONENTS  SEQUENCE  Groups Of Sequential Steps  SELECTION  Making Choices  IF-THEN (one way)  IF-THEN-ELSE.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
If Statements & Relational Operators Programming.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
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,
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
The If/Else Statement, Boolean Flags, and Menus Page 180
Chapter 4: Control Structures: Selection
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Decision Structures and Boolean Logic
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection Structures (if & switch statements) (CS1123)
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
1 © 2002 John Urrutia. All rights reserved. Qbasic Chapter 4 IF Statements and READ & DATA.
© 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.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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 3 Selection Statements
Chapter 3 Control Statements
Basics programming concepts-2
Sequence, Selection, Iteration The IF Statement
A mechanism for deciding whether an action should be taken
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Life is Full of Alternatives
Presentation transcript:

CONTROL STRUCTURES (SELECTION)

PROGRAM COMPONENTS  SEQUENCE  Groups Of Sequential Steps  SELECTION  Making Choices  IF-THEN (one way)  IF-THEN-ELSE (two way or multi way)  SWITCH (multi way)  ITERATION(Looping)  Repeating Steps  WHILE – DO (top tested)  DO – WHILE (bottom tested)  FOR (fixed iteration)

BOOLEAN DATA TYPE  Two Values – true (1) or false (0)  Example: bool isOvertime = true; cout << “Is overtime “ << isOvertime << endl; isOvertime = false; cout << “Is overtime “ << isOvertime << endl;  Internal program value  Cannot be read in via keyboard entry  Only displayed as 1 or 0

BOOLEAN DATA TYPE  Boolean names should reflect true value For example: bool done = false; bool even = false; bool error = true;  Can be initialized with integer:  Using 0 intializes to false  Anything Else initializes to true  It is best to use values true or false

RELATIONAL / EQUALITY OPERATORS  Boolean expression is: A condition in which a relational operator tests the relationship between two values or expressions and returns a boolean result (true or false)  Relational / Equality operators: Allow comparison of the size, magnitude or equality of data items that are the same or compatible types.  For example:  less than ( < )…. 3 < 5 evaluates to true  bool isLessThan = 3 < 5;

RELATIONAL / EQUALITY OPERATORS OperatorMeaningTrue ExampleFalse Example <Less Than3 < 54 < 2 or 6 < 6 <=Less Than OR Equal To3 <= 4 or 3 <= 310 <= 5 >Greater Than8 > 79 > 10 or 4 > 4 >=Greater Than OR Equal To 5 >= 4 or 5 >= 55 > = 6 ==Equal To20 == 2010 == 11 !=Not Equal To10 != != 20 Evaluation of either a relational or equality operator always results to either true or false.

RELATIONAL / EQUALITY OPERATORS  Relational Operator Priority  (, >=) Are below the arithmetic operator  Equality Operator Priority  ( ==, !=) Are below the the relational operators

LOGICAL OPERATORS  Logical Operators ( !, &&, || ): Allow the combination of boolean values according to the rules of mathematical logic. Logical expressions evaluate to a boolean result (true or false)  Not (!)  Inverts the boolean value  true becomes false or false become true

LOGICAL OPERATORS  AND (&&)  Evaluates to true only if both values are true bool x; bool y; X&&Y== True&&True &&False &&TrueFalse &&False

LOGICAL OPERATORS  OR ( || )  Evaluates to false only if both values are false bool x; bool y; X||Y== True||True ||FalseTrue False||True False||False

OPERATOR PRECEDENCE  All operator precedence:

BOOLEAN, RELATIONAL, EQUALITY AND LOGICAL OPERATIORS  Example: bool flag1, flag2, flag3; int num1, num2; flag1 = (98 % 13 != 98 / 13) || (2 * 5 != 10); cout << "The truth value of variable flag1 is " << flag1 << endl; num1 = * 7; num2 = 10 + num1 % 3; flag2 = (num1 > num2) && (num2 < 12); cout << "The truth value of variable flag2 is " << flag2 << endl; flag3 = flag1 || !flag2; cout << "The truth value of variable flag3 is " << flag3 << endl;

SELECTION  MAKING CHOICES  One-Way (choose to do or not to do)  if  Two-Way (choose to do one or the other)  if – else  conditional operator  Multiple Selection(choose to do one of many)  nested if - else  switch

ONE-WAY SELECTION  SIMPLE  if ( RELATIONAL EXPRESSION(s) ) STATEMENT;  COMPOUND  if ( RELATIONAL EXPRESSION(s) ) { STATEMENT; STATEMENT(s); }

ONE WAY SELECTION  RULES 1.The statement(s) is/are executed if and only if the relational expression(s) evaluate(s) to true.

ONE WAY SELECTION  EXAMPLES: if (current_balance > 1000) interest = current_balance * 0.015; if (day == 7 && hours_worked > 40) { ot_pay = (hours_worked – 40) * pay_rate * 2; total_pay = 40 * pay_rate + ot_pay; }

ONE WAY SELECTION  EXAMPLE: int main() { bool isEven; int num; cout > num; cout 10) cout << num << " is greater than 10" << endl; isEven = (num % 2 == 0); if (isEven) cout << num << " is an EVEN number." << endl; if (!isEven) cout << num << " is an ODD number." << endl; system ("pause"); return 0; }

TWO WAY SELECTION  SIMPLE  if ( RELATIONAL EXPRESSION(s) ) Statement; else Statement;

TWO WAY SELECTION  COMPOUND  if ( RELATIONAL EXPRESSION(s) ) { Statement; Statement(s); } else { Statement; Statement(s); }

TWO WAY SELECTION  RULES 1.First statement(s) will be executed if and only if the evaluation of the relational expression(s) is/are true. 2.Second statement(s) will be excuted if and only if the evaluation of the relational expression(s) is/are false.

TWO WAY SELECTION  EXAMPLES charemployee; stringstate; floatot_pay, pay_rate, gross_pay; floathours_worked, tax; Example 1: if (state == “CO”) tax = 0.065; else tax = 0.05;

TWO WAY SELECTION  EXAMPLES Example 2: if (employee == ‘E’ || employee == ‘S’) gross_pay = 40 * pay_rate; else { reg_pay = pay_rate * 40; ot_pay = (hours_worked – 10) * pay_rate * 1.5; gross_pay = reg_pay + ot_pay; }

CONDITIONAL OPERATOR  Can Accomplish Two Way Selection Using The Conditional Operator (?:)  expression1 ? expression2 : expression3  Example state == “CO” ? tax = : tax = 0.05;  Cannot Include Compound Statements