Download presentation
Presentation is loading. Please wait.
Published byVerity Thompson Modified over 9 years ago
1
Flow of Control and Program Style n Nested if statements n C++ struct n Program Style n 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: if(temp > 70) { if(temp < 32) { cout<<“freezing temperature”<<endl; } else { cout<<“temperature above 70<<endl”; } Correct? Incorrect? Why?
5
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
6
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
7
Struct n A C++ type n 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 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
13
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
14
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
15
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
16
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
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.