1 / 19

Flow of Control and Program Style

Flow of Control and Program Style. Nested if statements C++ struct Program Style Lab Exercise. Nested if statements/Multi-way branching. if (logical expression) { if (logical expression) { statement(s); } else

Download Presentation

Flow of Control and Program Style

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Nested if statements/Multi-way branching

  3. 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

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

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

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

  7. Struct • A C++ type • A way to handle related data

  8. 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.

  9. 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;

  10. 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.

  11. Program Style Indentation Comments Pre/Post

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

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

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

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

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

  17. 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

  18. 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

  19. 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

More Related