The IF-statements 31-Jan-2005 Venkatesh Ramamoorthy.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Selection (decision) control structure Learning objective
Subject: Information Technology Grade: 10
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
CS 1400 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
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!=
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Programming.
Chapter 3 Making Decisions
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 4 Selection Structures: Making Decisions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
Chapter#3 Part1 Structured Program Development in C++
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Algorithms, Part 3 of 3 Topics In-Class Project: The Box
Topics: Selection Statements. Processing Involving Selecting Instructions An instruction that allows deviation and selection to take place uses the ‘IF’
CS 141 Computer Programming 1 Branching Statements.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Decision making If.. else statement.
Introduction to Computer Programming
Sequence, Selection, Iteration The IF Statement
Matlab Training Session 4: Control, Flow and Functions
Chapter 5: Loops and Files.
ALGORITHMS AND FLOWCHARTS
ALGORITHMS AND FLOWCHARTS
Decision making If statement.
Selection Control Structure
Selection Statements.
Chapter 3: Selection Structures: Making Decisions
Programming Concepts and Database
EECE.2160 ECE Application Programming
Chapter 3: Selection Structures: Making Decisions
Chapter 3: Selection Structures: Making Decisions
Selection Control Structure
Lecture 9: Implementing Complex Logic
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
Presentation transcript:

The IF-statements 31-Jan-2005 Venkatesh Ramamoorthy

Draw a flowchart and write a program that simulates the distance and fare computation for a car based upon its state. For this problem, we define the state of the car to be the quantity of gas the car contains. Inputs to the program The number of gallons of gas in the tank The fuel efficiency in miles per gallon The price of gas per gallon Outputs from the program How far the car can go with the gas in the tank The cost of gas per 100 miles Ensure that the input and output prompts are as user- friendly as possible. Project Name testsubmission2

Further Analysis Validations on inputs Ensure that all inputs are non- negative (greater than or equal to zero) Can you think of any other validation? Provide more if you can.

The pseudo-code – first cut 1.Define g = Number of Gallons in tank, e = Fuel efficiency (miles / gallon), p = Price / gallon, d = Distance and p_100 = Price / 100 miles 2.Input g, e, p 3.IF e is non-zero THEN 4.Compute d ← g × e 5.Compute p_100 ← (100 × p) / e 6.Display d, p_100 7.ELSE 8.Display error message 9.ENDIF 10.Stop

The pseudo-code – second cut 1.Define g = Number of Gallons in tank, e = Fuel efficiency (miles / gallon), p = Price / gallon, d = Distance and p_100 = Price / 100 miles 2.Input g, e, p 3.IF (e is zero) THEN { 4.Display error message 5.} 6.ELSE { 7.IF (g is negative) THEN { 8.Display error message 9.} 10.ELSE { 11.IF (p is negative) THEN { 12.Display error message 13.} 14.} 15.} 16.Compute d ← g × e 17.Compute p_100 ← (100 × p) / e 18.Display d, p_ Stop

Structure of the If-statement if ( logical-expression ) { C++ statement-set-1 ; } else { C++ statement-set-2 ; }

More on the if-statement The else part is not mandatory if ( logical-expression ) { C++ statement-set-1 ; } C++ statement-set-2 ;

Logical Expressions Operands –These operands are variables Logical Operators – connectives –These are logical operators

Types of logical expressions These evaluate to exactly one of the following: –TRUE –FALSE Simple –Operand1 logical–operator Operand2 Compound –Many simple expressions –Connected by connectives such as AND, OR

The exercise – using the if- statement int main() { if (efficiency != 0) { Execute the lines in your code } else { Display error message “one of your inputs is invalid” } return (0) ; }

The exercise - variant 1 input gasquantity, price and efficiency if (gasquantity <= 0) { display error } else { if (price < 0) { display error } else { if (efficiency == 0) { display error } else { Execute the calculations Execute the output display statements }

The exercise - variant 2 input gasquantity if (gasquantity <= 0) { display error “invalid gas quantity” } else { input price if (price < 0) { display error “invalid price” } else { input efficiency if (efficiency == 0) { display error “invalid efficiency” } else { Execute the calculations Execute the output display statements }

Multiple If’s and Else’s if (logical-expression1) { C++ statement-set-1 ; } else if (logical-expression2) { C++ statement-set-2 ; } else if (logical-expression3) { C++ statement-set-3 ; } … else { C++ statement-set-default ; }

Acknowledgments These exercises have been taken from "Computing Concepts with C++ Essentials" by Cay Horstmann, John Wiley & Sons, Inc., 1997