0
Lecture 04  Structural Programming                      in C++You will learn:i) Operators: relational and logicalii) Condi...
Structural Programming in C and C++• Even though object-oriented programming is  central to C++, you still need to know ba...
Relational Operators. . .int n;cout << "Enter   a number: ";cin >> n;cout << "n<10    is " << (n < 10) << endl;cout << "n>...
Relational Operators                         (cont.)• Displaying the results of relational operations,  or even the values...
Logical Operators• Logical AND Operator: &&        if ( x == 7 && y == 11 )          statement;• Logical OR Operator: ||  ...
Operator Precedence              SUM = SUM + 5                   OR                SUM =+ 5                   6/22
Conditional Statement:   if         Syntax                         7/22
Conditional Statement:   if…else                       Syntax    If (x > 100)        statement;    else        statement;A...
Conditional Statement:      switch                     Syntaxswitch(speed) {      Int a,b,c; char op;    case 33:         ...
Conditional Operator      Syntax                   10/22
Conditional Operator Syntax  result = (alpha < 77) ? beta : gamma;is equivalent to  if (alpha < 77)     result = beta;  el...
ExampleResult = (num > 0): ‘positive’: (num<0) : ’negative’: ’zero’is equivalent toif num>0       result = ‘positive’;else...
The for Loop   Syntax               13/22
The for Loop Control Flow                14/22
ExampleFor (I=1;I<=10;I++)   For (I=1;I<=10;I++) cout << I;            cout << “*”cin>>n;                For (I=1;I<=10;I+...
The   while Loop      Syntax                   16/22
The while Loop  Control Flow                 17/22
Examplei = 1;               While ( I<=10 )While (i<=10)              cout << “*”;   cout << i;Cin >> n; I = 1;While (I<=n...
The do Loop  Syntax              19/22
The do LoopControl Flow               20/22
ExampleDo                   Cin >> n;  {cout <<I;         Do                     {  I = I +1;                          cou...
FunctionsDef :   Set of statements used for a specific taskSyntax: returntype fName( arguments)          {… statements ret...
Using Functions To Aid                  Modularity. . .void starline();        // function prototypeint main(){      . . ....
Passing Arguments To Functionsvoid repchar(char, int);int main(){ char chin;  int nin;  cin >> chin;  cin >> nin;  repchar...
Returning Values From Functionsfloat lbstokg(float);int main(){  float lbs;  cout << "Enter your weight in pounds: ";  cin...
Using Structures To Group Datastruct part {         //   declare a structure   int modelnumber;   //   ID# of widget   int...
Structures Within Structuresstruct Distance {   int feet;   float inches;};struct Room {         int main()   Distance len...
28
Upcoming SlideShare
Loading in...5
×

Lecture04

78

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
78
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Lecture04"

  1. 1. Lecture 04 Structural Programming in C++You will learn:i) Operators: relational and logicalii) Conditional statementsiii) Repetitive statementsiv) Functions and passing parameterv) Structures 1
  2. 2. 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++. 2/22
  3. 3. 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: 20n<10 is 0n>10 is 1n==10 is 0 3/22
  4. 4. 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. 4/22
  5. 5. 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; 5/22
  6. 6. Operator Precedence SUM = SUM + 5 OR SUM =+ 5 6/22
  7. 7. Conditional Statement: if Syntax 7/22
  8. 8. Conditional Statement: if…else Syntax If (x > 100) statement; else statement;A statement can be a single statement or acompound statement using { }. 8/22
  9. 9. Conditional Statement: switch Syntaxswitch(speed) { Int a,b,c; char op; case 33: cin >> a >>op >>b; statement; switch(op) break; case ‘+’: case 45: c= a+b;break; statement; case ‘-’: break; c= a-b;break; case 78: statement; default: break; cout <<“unknown operator”;} } 9/22
  10. 10. Conditional Operator Syntax 10/22
  11. 11. Conditional Operator Syntax result = (alpha < 77) ? beta : gamma;is equivalent to if (alpha < 77) result = beta; else result = gamma; 11/22
  12. 12. ExampleResult = (num > 0): ‘positive’: (num<0) : ’negative’: ’zero’is equivalent toif num>0 result = ‘positive’;else if num <0 result = ‘negative’; else 12/22
  13. 13. The for Loop Syntax 13/22
  14. 14. The for Loop Control Flow 14/22
  15. 15. ExampleFor (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; For (I=1;I<=3;I++) For (j=1;j<=10;j++)cout << sum; cout << I << “*” <<j<<“=“<<I*j; 15/22
  16. 16. The while Loop Syntax 16/22
  17. 17. The while Loop Control Flow 17/22
  18. 18. Examplei = 1; While ( I<=10 )While (i<=10) cout << “*”; cout << i;Cin >> n; I = 1;While (I<=n){ sum = sum + I; cout << sum} 18/22
  19. 19. The do Loop Syntax 19/22
  20. 20. The do LoopControl Flow 20/22
  21. 21. ExampleDo Cin >> n; {cout <<I; Do { I = I +1; cout <<I;} while (I<=10); I ++; } while (I<=n) 21/22
  22. 22. FunctionsDef : Set of statements used for a specific taskSyntax: returntype fName( arguments) {… statements return variblename}Types of functions:1. Function with no arguments and no return2. Functions with argument and no return3. Functions with argument and a return 22/22
  23. 23. Using Functions To Aid Modularity. . .void starline(); // function prototypeint main(){ . . . starline(); . . . starline(); return 0;}void starline() // function definition{ for(int j=0; j<45; j++) cout << *; cout << endl; 23/22}
  24. 24. Passing Arguments To Functionsvoid 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; 24/22}
  25. 25. Returning Values From Functionsfloat 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; 25/22}
  26. 26. Using Structures To Group Datastruct 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; 26/22}
  27. 27. Structures Within Structuresstruct Distance { int feet; float inches;};struct Room { int main() Distance length; { Distance width; 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; 27/22 }
  28. 28. 28
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×