Flow of Control and Program Style n Nested if statements n C++ struct n Program Style n Lab Exercise.

Slides:



Advertisements
Similar presentations
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
1 Lab Session-1 CSIT221 Fall 2002 b Refresher slides b Practice Problem b Lab Exercise (Demo Required)
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
Lecture 1 The Basics (Review of Familiar Topics).
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
C++ for Engineers and Scientists Third Edition
Introduction to C Programming
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Programming Introduction to C++.
Chapter 6: Functions.
Chapter 7 Functions.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
High-Level Programming Languages: C++
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
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.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 4 Selection Structures: Making Decisions.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 11 Conditional.
Programming Principles Chapter 1. Objectives Discuss the program design process. Introduce the Game of Life. Discuss object oriented design. – Information.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Documentation and Programming Style Appendix A © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
CSC 212 – Data Structures Prof. Matthew Hertz WTC 207D /
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Copyright © Curt Hill The IF Revisited If part 4 Style and Testing.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
Functions Procedural Abstraction Flow of Control INFSY 307 Spring 2003 Lecture 4.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Basic concepts of C++ Presented by Prof. Satyajit De
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 4: Control Structures I (Selection)
Programming Introduction to C++.
Chapter 8 Functions.
Presentation transcript:

Flow of Control and Program Style n Nested if statements n C++ struct n Program Style n Lab Exercise

Nested if statements/Multi-way branching

if (logical expression) { if (logical expression) { statement(s); } else { statement(s); } else { statement(s); } Multiway Branching and nested statements: note if contained within if logic

Evaluate: if(temp > 70) { if(temp < 32) { cout<<“freezing temperature”<<endl; } else { cout<<“temperature above 70<<endl”; } Correct? Incorrect? Why?

Nested Statements n Always line up if with corresponding else n Compiler always matches else with nearest previous if n Indentation will not make a difference except for logical understanding of code! n Brackets around sub statements also aid readability and prevent logic errors

Multiway if-else Example If(age > 75) cout<<“golden years”; else if (age >50) cout<<“senior”; else if (age > 30) cout<<“middle age”; else cout<<“young adult”; Note: 1) use of special indentation 2) necessity of brackets with multiple statements

Struct n A C++ type n A way to handle related data

Structures A structure is a data type much like a class, an int, char, etc. Structure tag = the name of a structure type Members (variable names) are contained in brackets. Member variables in a program are specified using the structure variable followed by a “.” and then the member variable name.

Structure Example int test::input () { int an_age; cin>>p.social_security; an_age= read_convert_to_int () ; if(an_age== -1) { cout<<“age is incorrect”; p.age = 0; } else { p.age=an_age; } return 0; } In class definition: private: struct person { char social_security [10]; int age; char sex; }; person p;

Structure Purpose O Group associated variables. O Concept of a record, i.e. group of related fields or variables. O More relevant when we begin to group information to be written on a disk.

Program Style Indentation Comments Pre/Post

Indentation n Place braces on lines by themselves n Indent within braces n Consistency of indentation and indentation style is crucial n The Development Studio has default indentation built into it

Comments n Do not comment each line of the program n Comments may appear at the beginning of a program or function n Use comment for file name, program description, author n Pre/post conditions under prototype are crucial

Precondition/Postcondition n Should be a first step, ie prior to writing a function; important part of design n Place immediately after a function prototype n A comment

Precondition n Provides information about state of input argument(s) – WHAT! n Appear after function prototype n Tells what is assumed to be true before the function is executed

Postcondition u Tells what the function does u Tells what will be true after execution of the function u Describes state of variables after execution of the function u For functions that return a value, describes the return value

Examples void get_name_input (); //Precondition: A name is needed //Postcondition: The value of first name and last name is set int calc_average (); //Precondition: A sum and number are available for use //Postcondition: The average has been calculated by dividing // the sum by the number. //The average is returned to the calling program

Program Testing It is important to test your logic fully function by function: 1. Test each function as it is implemented and as a separate unit 2. Insert dummy code, eg a cout for funtions that are incomplete 3. Test at the very least - one case 4. Minimal driver programs (main) or temporary tool for testing is advised 5. Make use of cout statements and/or VDE debug features e.g. step step over and trace into features

TO DO in Lab: Page 429, Number 4 with changes: a. Variable for week-end or week-day: 1 for week-day and 2 for week-end b. Just read first part through “13:30” c. Create workspace d. Create a simple main function e. Create a header file