Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES.

Slides:



Advertisements
Similar presentations
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Advertisements

Selection (decision) control structure Learning objective
Lesson 5 - Decision Structure By: Dan Lunney
Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Introduction to Computers and Programming Lecture 5 New York University.
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.
Computer Science A 3: 10/2. Logical Expressions Expressions that has a value which is either true or false. Logical expressions are essential in a program.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.
Chapter 4 Making Decisions
Geography 465 Assignments, Conditionals, and Loops.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Decision Structures and Boolean Logic
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Problem Solving with Decisions
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
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.
CPS120: Introduction to Computer Science
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
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.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
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.
Agenda Basic Logic Purpose if statement if / else statement
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
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.
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.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
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.
 Type Called bool  Bool has only two possible values: True and False.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
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.
Chapter 4: Making Decisions.
CMPT 201 if-else statement
Topics The if Statement The if-else Statement Comparing Strings
JavaScript conditional
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Control Structures I (Selection)
Computers & Programming Languages
Selection Statements.
Conditional Logic Presentation Name Course Name
ICT Programming Lesson 3:
Decision I (if Statement)
Understanding Conditions
The Data Element.
Chapter 5 Decisions.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
The Data Element.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Conditionals.
Presentation transcript:

Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES

Flow of Control  The order in which the computer executes statements in a program.  Normally sequential.  How to make it not sequential, we use control structures.  It is a statement used to alter the normally sequential flow of control.

Selection  Used when we want the computer to choose between alternative actions. Assertion Statement 1BStatement 1A false true

Bool Data Type  Built-in type consists of just TWO values, the constants true and false.  bool is short for boolean.  Used for testing conditions in a program so the computer can make a decision.  Ex:  bool dataOK;  bool done;  bool taxable;  True and false are not variable names and they aren’t strings. They are special constants in C++, and are reserved words.

Logical Expressions  These assertions are also called boolean expressions.  Assigning a Boolean variable:  done = true; // This is valid because we are assuming it is pre-declared.  Another way, with a relational operator:  lessThan = (5 < 3); // What do you think is in the variable lessThan?

Relational Operator  ==Equal to  !=Not equal to  >Greater than  <Less than  >=Greater than or equal to  <=Less than or equal to  An expressions follow by a relational operator followed by an expression is called a relational expression.

Relational Operator  Compare like types to avoid conflicts.  What do you think true and false represent in numerical form?  VERY VERY VERY VERY VERY VERY VERY VERY VERY IMPORTANT  (=) and (==) are COMPLETELY different.  (=) assigns a value to some variable.  (==) compares two variables. (equals-equals)

Comparing Strings  myStr < yourStr // string object and string object, OKAY  myStr >= “Johnson” // string object and c string, OKAY  “NO” <= “STOP NOW” // cannot both be C strings, NOOOOOO  String comparisons begin with the first character of each string.  Continues to compare a character at a time, until a mismatch is found or the final characters have been compared and are equal.  Character values based on ASCII chart.

If-then & if-then-else statement  if(expression)  statement1A  else  statement1B  Notice there are no (;) after the else or if!  Only used on simple statements such as assignment statements, input statements, and output statements.