190 likes | 277 Views
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
E N D
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 { statement(s); } } else { statement(s); } Multiway Branching and nested statements: note if contained within if logic
Evaluate: Correct? Incorrect? Why? if(temp > 70) { if(temp < 32) { cout<<“freezing temperature”<<endl; } } else { cout<<“temperature above 70<<endl”; }
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
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”;
Struct • A C++ type • 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 • 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
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
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
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
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
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