Flow of Control and Program Style

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

description

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 - PowerPoint PPT Presentation

Transcript of Flow of Control and Program Style

Page 1: Flow of Control and Program                   Style

Flow of Control and Program Style

Nested if statements

C++ struct Program Style Lab Exercise

Page 2: Flow of Control and Program                   Style

Nested if statements/Multi-way branching

Page 3: Flow of Control and Program                   Style

if (logical expression) { if (logical expression) { statement(s); } else { statement(s); } }else { statement(s); }

Multiway Branchingand nested statements: noteif contained within if logic

Page 4: Flow of Control and Program                   Style

Evaluate:

if(temp > 70) { if(temp < 32) { cout<<“freezing temperature”<<endl; } } else { cout<<“temperature above 70<<endl”; }

Correct? Incorrect? Why?

Page 5: Flow of Control and Program                   Style

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

Page 6: Flow of Control and Program                   Style

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

Page 7: Flow of Control and Program                   Style

Struct

A C++ type A way to handle

related data

Page 8: Flow of Control and Program                   Style

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 specifiedusing the structure variable followed by a “.” and then the member variable name.

Page 9: Flow of Control and Program                   Style

Structure Exampleint 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;

Page 10: Flow of Control and Program                   Style

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.

Page 11: Flow of Control and Program                   Style

Program Style

Indentation

Comments

Pre/Post

Page 12: Flow of Control and Program                   Style

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

Page 13: Flow of Control and Program                   Style

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

Page 14: Flow of Control and Program                   Style

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

Page 15: Flow of Control and Program                   Style

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

Page 16: Flow of Control and Program                   Style

PostconditionPostcondition

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

Page 17: Flow of Control and Program                   Style

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

Page 18: Flow of Control and Program                   Style

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 incomplete3. Test at the very least - one case4. Minimal driver programs (main) or temporary tool for

testing is advised5. Make use of cout statements and/or VDE debug features e.g. step step over and trace into features

Page 19: Flow of Control and Program                   Style

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