Top Banner
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements iv) Functions and passing parameter v) Structures
27

1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

Jan 04, 2016

Download

Documents

Jody Lamb
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

1

Lecture 04

Structural Programming

in C++

You will learn:i) Operators: relational and logicalii) Conditional statementsiii) Repetitive statementsiv) Functions and passing parameterv) Structures

Page 2: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

2/22

Structural Programming in C and C++

• Even though object-oriented programming is central to C++, you still need to know basic structural constructs to do the job.

• The implementation of the member functions of a class (i.e. methods in OOP term) is largely structural programming.

• Almost all the structural programming constructs in C are also valid in C++.

Page 3: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

3/22

Relational Operators

. . . int n; cout << "Enter a number: "; cin >> n; cout << "n<10 is " << (n < 10) << endl; cout << "n>10 is " << (n > 10) << endl; cout << "n==10 is " << (n == 10) << endl; . . .

A Sample Run:Enter a number: 20 n<10 is 0 n>10 is 1 n==10 is 0

Page 4: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

4/22

Relational Operators(cont.)

• Displaying the results of relational operations, or even the values of type bool variables, with cout << yields 0 or 1, not false and true.

• The relational operators in C++ include: >, <, ==, !=, >=, <=.

• Any value other than 0 is considered true, only 0 is false.

Page 5: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

5/22

Logical Operators

Logical AND Operator: &&

if ( x == 7 && y == 11 )   statement;

• Logical OR Operator: ||

if ( x < 5 || x 15 ) statement;

Logical NOT Operator: !

if !(x == 7)

statement;

Page 6: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

6/22

Operator Precedence

SUM = SUM + 5OR

SUM =+ 5

Page 7: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

7/22

Conditional Statement: ifSyntax

Page 8: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

8/22

Conditional Statement: if…elseSyntax

If (x > 100) statement;

else statement;

A statement can be a single statement or a compound statement using { }.

Page 9: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

9/22

Conditional Statement: switchSyntax

switch(speed) {case 33:

statement;break;

case 45:statement;break;

case 78:statement;break;

}

Int a,b,c; char op;

cin >> a >>op >>b;

switch(op)

case ‘+’:

c= a+b;break;

case ‘-’:

c= a-b;break;

default:

cout <<“unknown operator”;

}

Page 10: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

10/22

Conditional OperatorSyntax

Page 11: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

11/22

Conditional Operator Syntax

result = (alpha < 77) ? beta : gamma;

is equivalent to

if (alpha < 77)

result = beta;

else

result = gamma;

Page 12: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

12/22

Result = (num > 0)?‘positive’: (num<0) ?’negative’: ’zero’

is equivalent to

if num>0

result = ‘positive’;

else

if num <0

result = ‘negative’;

else

result = ‘zero’;

Example

Page 13: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

13/22

The for LoopSyntax

Page 14: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

14/22

The for LoopControl Flow

Page 15: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

15/22

Example

For (I=1;I<=10;I++) For (I=1;I<=10;I++)

cout << I; cout << “*”

cin>>n; For (I=1;I<=10;I++)

For (I=1;I<=n;I++) cout << “*” <<endl;

sum = sum +I;

cout << sum;

For (I=1;I<=3;I++)For (j=1;j<=10;j++)cout << I << “*” <<j<<“=“<<I*j;

Page 16: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

16/22

The while LoopSyntax

Page 17: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

17/22

The while LoopControl Flow

Page 18: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

18/22

Example

i = 1;

While (i<=10)

cout << i;

Cin >> n; I = 1;

While (I<=n)

{ sum = sum + I;

cout << sum

}

While ( I<=10 )

cout << “*”;

Page 19: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

19/22

The do LoopSyntax

Page 20: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

20/22

The do LoopControl Flow

Page 21: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

21/22

Example

Do

{cout <<I;

I = I +1;

} while (I<=10);

Cin >> n;Do{

cout <<I;I ++;

} while (I<=n)

Page 22: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

22/22

Functions

Def : Set of statements used for a specific task

Syntax: returntype fName( arguments)

{… statements return variblename}

Types of functions:

1. Function with no arguments and no return

2. Functions with argument and no return

3. Functions with argument and a return

Page 23: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

23/22

Using Functions To AidModularity

. . . void starline(); // function prototype

int main() { . . . starline(); . . . starline(); return 0; }

void starline() // function definition{ for(int j=0; j<45; j++) cout << '*'; cout << endl; }

Page 24: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

24/22

Passing Arguments To Functions

void repchar(char, int);

int main() { char chin; int nin; cin >> chin; cin >> nin; repchar(chin, nin); return 0; }

void repchar(char ch, int n) { for(int j=0; j < n; j++) cout << ch; cout << endl; }

Page 25: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

25/22

Returning Values From Functions

float lbstokg(float);

int main() { float lbs; cout << "Enter your weight in pounds: "; cin >> lbs; cout << "Your weight in kg is " << lbstokg(lbs) << endl; return 0; }

float lbstokg(float pounds) { return 0.453592 * pounds; }

Page 26: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

26/22

Using Structures To Group Data

struct part { // declare a structure int modelnumber; // ID# of widget int partnumber; // ID# of widget part float cost; // cost of part };

int main() { part part1; part1.modelnumber = 6244; part1.partnumber = 373; part1.cost = 217.55; cout << "Model " << part1.modelnumber << ", part " << part1.partnumber << ", cost $" << part1.cost << endl; return 0; }

Page 27: 1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.

27/22

Structures Within Structures

struct Distance { int feet; float inches; }; struct Room { Distance length; Distance width; };

int main() { Room dining={ {13, 6.5},{10, 0.0} }; float l = dining.length.feet + dining.length.inches/12; float w = dining.width.feet + dining.width.inches/12; cout << "Dining room area is " << l * w << " square feet" << endl; return 0; }