Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
Introduction to C Programming
CSCI 130 Chapter 4 Statements, Expressions, and Operators.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Random Functions Selection Structure Comparison Operators Logical Operator
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.
Rational Expressions relational operators logical operators order of precedence.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Control Structures I
Chapter 7: Expressions and Assignment Statements
Chapter 4: Making Decisions.
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.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Topics The if Statement The if-else Statement Comparing Strings
Expression Review what is the result and type of these expressions?
Computers & Programming Languages
Relational Operators Operator Meaning < Less than > Greater than
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Lecture Notes – Week 2 Lecture-2
Beginning C Lecture 3 Lecturer: Dr. Zhao Qinpei
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005

Topics  A compound (block) statement  Variable scope  Relational expression  Logical expression  The if statement  The switch statement  The break statement  The conditional expression

A compound statement  Consists of zero or more statements enclosed in open/close braces  Are commonly called “blocks”.

Compound statements  A program accomplishes several tasks Ex. Recall the program in your homework1 that reads the grades of a student’s assignments and tests from the keyboard, computes WAT and WATA, computes the finalScore of this student, transforms the finalScore to a letter (finalGrade), and displays the finalGrade.

Compound statements void main() { double hw1,hw2,hw3,hw4,hw5,hw6,t1,t2,t3; //read inputs from the keyboard for assignments and tests { … } //computes the weighted average of tests (WAT) and WATA double WAT,WATA; { … } //computes the finalScore from WAT and WATA double finalScore; { … } //transforms finalScore to finalGrade---a letter char finalGrade; { … } //display the finalGrade { … }

Scope of a variable  Scope of a variable is the block in which it is defined, from the point of definition to the end of the block  Usually defined at beginning of function  May be defined close to first use

Still More About Variable Definitions and Scope  Variables defined inside { } have local or block scope void main() { //read inputs from the keyboard for assignments and tests { double hw1,hw2,hw3,hw4,hw5,hw6,t1,t2,t3; … } //computes the weighted average of tests (WAT) and WATA double WAT,WATA; { … } … }

Still More About Variable Definitions and Scope  When a block is nested inside another block, a variable defined in the inner block may have the same as a variable defined in the outer block When in inner block, outer definition is not available Should avoid give same names

Ex  For any given non-zero integer i, compute the expression 15.0/i, and output the result.

Relational Operators  Used to compare numbers to determine relative order  Operators: > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to

Relational Expressions  Boolean expressions – true or false  Examples: 12 > 5 is true 7 <= 5 is false if x is 10, then x == 10 is true, x != 8 is true, and x == 8 is false

Relational Expressions  Can be assigned to a variable: result = x <= y;  Assigns 0 for false, 1 for true  Do not confuse = and ==

Relational Expressions  Type compatibility of relational operators  Precedence of relational operators  Associativity of relational operators priorityOperator 1Arithmetical operator 2 >= 3== != 4= += -= *= /= %=

Logical Operators  Used to create relational expressions from other relational expressions  Unary logical operator not: operator meaning explanation ! NOTReverses the value of an expression – true expression becomes false, and false becomes true

Logical Operators  Binary logical operator and: operator meaning explanation && AND New relational expression is true if both expressions are true aba && b true false true false

Logical Operators  Binary logical operator or: operator meaning explanation || OR New relational expression is true if either expression is true aba || b true false true false

Logical Operators - notes  ! has highest precedence, followed by &&, then ||  If the value of an expression can be determined by evaluating just the sub-expression on left side of a logical operator, then the sub- expression on the right side will not be evaluated (short circuit evaluation)

Precedence and associativity PriorityOperators 1! 2Arithmetic operators 3Inequality relational > >= < <= 4Equality and inequality == != 5&& 6|| 7Assignment operators