1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.

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

Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Introduction to C Programming
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
If Statements & Relational Operators Programming.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
CS150 Introduction to Computer Science 1
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
CPS120: Introduction to Computer Science Operations Lecture 9.
First steps Jordi Cortadella Department of Computer Science.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
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.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
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.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Selection in C++ If statements. Control Structures Sequence Selection Repetition Module.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
 Type Called bool  Bool has only two possible values: True and False.
Chapter 3 Selection Statements
Bools & Ifs.
Introduction to C++ Programming
Relational Operators Operator Meaning < Less than > Greater than
Unary Operators ++ and --
Bools & Ifs.
Selection Control Structure
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
CS150 Introduction to Computer Science 1
Life is Full of Alternatives
Understanding Conditions
Arithmetic Operations
Life is Full of Alternatives
Life is Full of Alternatives
Relational and Logical Operators
Relational and Logical Operators
Introduction to C Programming
Introduction to Python
Presentation transcript:

1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement

2 9/24/07CS150 Introduction to Computer Science 1 Conditionals  So far, we can Input, Output and Calculate  How can we explore relationships between data?  How can our program only do things sometimes?

3 9/24/07CS150 Introduction to Computer Science 1 Decisions!  Relational Expressions allow our program to make a decision o based on the data in the program  What are some decisions we might want out program to make?  We will use: o Relational Expressions o Relational Operators

4 9/24/07CS150 Introduction to Computer Science 1 Relational Expression  An expression is a statement that ________  Relational expression: an expression that uses a Relational Operator o its value is a Boolean value (True or False) int x=9, y=42; x > y y == x // y = x; is the assignment operator x <= (x * y + 99)

5 9/24/07CS150 Introduction to Computer Science 1 Relational Operators OperatorMeaning > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to o All are binary operators o Left to right associativity

6 9/24/07CS150 Introduction to Computer Science 1 Precedence (page 1101) Precedence Operators (Highest to Lowest) (unary negation) - * / % + - > >= < <= == != = += -= *= /= %= Relational Operators Assignment Operators Arithmetic Operators

7 9/24/07CS150 Introduction to Computer Science 1 Practice  What is the value of the following Relational Expressions? int x = 99, y = 42; x > y y <= x y != x x == (x + 1) y == y + 1 y == x - 45 Relational Operators work on Integers, Floating point numbers, and Characters.

8 9/24/07CS150 Introduction to Computer Science 1 Boolean value (True or False)  How does the computer represent True and False?  New data type: bool bool tValue = true; // 1 bool fValue = false; // 0

9 9/24/07CS150 Introduction to Computer Science 1 Practice bool value; int x=5, y=10; value = x > y; // value = ?? value = x == y; // value = ?? value = x == y - 5; // value = ?? // what does this output look like? cout << “Value is: “ << value;

10 9/24/07CS150 Introduction to Computer Science 1 Practice  What C++ statement would we write make the following determinations? bool value; int yourAge = 22, currentYear = 2008;  Are you old enough to vote?  Where you born before 1980?  Is you age evenly divisible by 7?

11 9/24/07CS150 Introduction to Computer Science 1 The if Statement  We execute each statement in our program in order.  What if we only want to execute a statement sometimes?  The if Statement! int x=5, y=10; if( x > y) { // do stuff }

12 9/24/07CS150 Introduction to Computer Science 1 Formally defined  What is an expression? if( expression ) { statement 1; statement 2;... statement n; } Just like a function, start at the top and execute in order to the bottom

13 9/24/07CS150 Introduction to Computer Science 1 Practice: What is the output? int x=5, y=10; bool value = x > y; if ( value ) { cout << “value is True” << endl; } if ( x < y ) { cout << x << “ < ” << y; cout << “ is true” << endl; }

14 9/24/07CS150 Introduction to Computer Science 1 Practice  For the problem below: o what data will you need? o what will you need to do conditionally?  what data will you use in your decision?  Calculate the average grade for all three exams in a course. Print a message showing the letter grade the student received and a message stating if the student passed the course.

15 9/24/07CS150 Introduction to Computer Science 1 Coding Standards if( expression ) { statement 1; } If you only have ONE statement in the body of the if, the { } are optional in C++. For this class, the { } must ALWAYS be used. Not using { } will result In a loss of style points. The { } must also be on their own line. Why? if( expression ) statement 1;

16 9/24/07CS150 Introduction to Computer Science 1 More on Truth  Expressions that evaluate to non-zero are considered true int x=5, y=0; if ( x + y) { // This will be executed cout << “x+y is True” << endl; } if ( y ) { // This will NOT be executed cout << “y is True” << endl; }

17 9/24/07CS150 Introduction to Computer Science 1 Practice  Write the C++ code for the following problem  Calculate the average grade for all three exams in a course. Print a message showing the letter grade the student received and a message stating if the student passed the course.