SlideShare a Scribd company logo
1 of 28
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


                                       1
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
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
                                              3/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.
                                        4/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;                   5/22
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 statement can be a single statement or a
compound statement using { }.

                                      8/22
Conditional Statement:      switch
                     Syntax
switch(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
Conditional Operator
      Syntax




                   10/22
Conditional Operator Syntax



  result = (alpha < 77) ? beta : gamma;
is equivalent to
  if (alpha < 77)
     result = beta;
  else
     result = gamma;
                                          11/22
Example
Result = (num > 0): ‘positive’: (num<0) : ’negative’: ’zero’


is equivalent to
if num>0
       result = ‘positive’;
else
       if num <0
              result = ‘negative’;
       else
                                                        12/22
The for Loop
   Syntax




               13/22
The for Loop
 Control Flow




                14/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;       For (I=1;I<=3;I++)
                      For (j=1;j<=10;j++)
cout << sum;          cout << I << “*” <<j<<“=“<<I*j;

                                       15/22
The   while Loop
      Syntax




                   16/22
The while Loop
  Control Flow




                 17/22
Example
i = 1;               While ( I<=10 )
While (i<=10)              cout << “*”;
   cout << i;

Cin >> n; I = 1;
While (I<=n)
{ sum = sum + I;
    cout << sum
}
                                          18/22
The do Loop
  Syntax




              19/22
The do Loop
Control Flow




               20/22
Example

Do                   Cin >> n;
  {cout <<I;         Do
                     {
  I = I +1;
                          cout <<I;
} while (I<=10);          I ++;
                     } while (I<=n)



                                  21/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
                                              22/22
Using Functions To Aid
                  Modularity
. . .
void starline();        // function prototype

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

void starline()         // function definition
{
  for(int j=0; j<45; j++)
    cout << '*';
  cout << endl;
                                         23/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;                24/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;              25/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;
                                          26/22
}
Structures Within Structures

struct 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

More Related Content

What's hot

What's hot (20)

Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#
 
重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Powerpoint loop examples a
Powerpoint loop examples aPowerpoint loop examples a
Powerpoint loop examples a
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
A MuDDy Experience - ML Bindings to a BDD Library
A MuDDy Experience - ML Bindings to a BDD LibraryA MuDDy Experience - ML Bindings to a BDD Library
A MuDDy Experience - ML Bindings to a BDD Library
 
Javascript scoping
Javascript scopingJavascript scoping
Javascript scoping
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
What\'s New in C# 4.0
What\'s New in C# 4.0What\'s New in C# 4.0
What\'s New in C# 4.0
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 
Pointers
PointersPointers
Pointers
 
P1
P1P1
P1
 
Day 1
Day 1Day 1
Day 1
 
C++ practical
C++ practicalC++ practical
C++ practical
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
C++ programming
C++ programmingC++ programming
C++ programming
 
C programming
C programmingC programming
C programming
 
Asynchronous JS in Odoo
Asynchronous JS in OdooAsynchronous JS in Odoo
Asynchronous JS in Odoo
 
Lecture17
Lecture17Lecture17
Lecture17
 

Similar to Lecture04

Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 
Lecture#5 Operators in C++
Lecture#5 Operators in C++Lecture#5 Operators in C++
Lecture#5 Operators in C++NUST Stuff
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
4. programing 101
4. programing 1014. programing 101
4. programing 101IEEE MIU SB
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたAkira Maruoka
 
Chp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxChp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxssuser10ed71
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptxAqeelAbbas94
 

Similar to Lecture04 (20)

Lecture05
Lecture05Lecture05
Lecture05
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
Lecture#5 Operators in C++
Lecture#5 Operators in C++Lecture#5 Operators in C++
Lecture#5 Operators in C++
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
4. programing 101
4. programing 1014. programing 101
4. programing 101
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみた
 
Chp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxChp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptx
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
C++InputOutput.pptx
C++InputOutput.pptxC++InputOutput.pptx
C++InputOutput.pptx
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
C++InputOutput.PPT
C++InputOutput.PPTC++InputOutput.PPT
C++InputOutput.PPT
 
3.Loops_conditionals.pdf
3.Loops_conditionals.pdf3.Loops_conditionals.pdf
3.Loops_conditionals.pdf
 
Advanced pointer
Advanced pointerAdvanced pointer
Advanced pointer
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 

More from elearning_portal (11)

Lecture21
Lecture21Lecture21
Lecture21
 
Lecture19
Lecture19Lecture19
Lecture19
 
Lecture18
Lecture18Lecture18
Lecture18
 
Lecture10
Lecture10Lecture10
Lecture10
 
Lecture09
Lecture09Lecture09
Lecture09
 
Lecture07
Lecture07Lecture07
Lecture07
 
Lecture06
Lecture06Lecture06
Lecture06
 
Lecture20
Lecture20Lecture20
Lecture20
 
Lecture03
Lecture03Lecture03
Lecture03
 
Lecture02
Lecture02Lecture02
Lecture02
 
Lecture01
Lecture01Lecture01
Lecture01
 

Recently uploaded

GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 

Recently uploaded (20)

YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 

Lecture04

  • 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 1
  • 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. 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 3/22
  • 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. 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. Operator Precedence SUM = SUM + 5 OR SUM =+ 5 6/22
  • 7. Conditional Statement: if Syntax 7/22
  • 8. Conditional Statement: if…else Syntax If (x > 100) statement; else statement; A statement can be a single statement or a compound statement using { }. 8/22
  • 9. Conditional Statement: switch Syntax switch(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. Conditional Operator Syntax 10/22
  • 11. Conditional Operator Syntax result = (alpha < 77) ? beta : gamma; is equivalent to if (alpha < 77) result = beta; else result = gamma; 11/22
  • 12. Example Result = (num > 0): ‘positive’: (num<0) : ’negative’: ’zero’ is equivalent to if num>0 result = ‘positive’; else if num <0 result = ‘negative’; else 12/22
  • 13. The for Loop Syntax 13/22
  • 14. The for Loop Control Flow 14/22
  • 15. 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; For (I=1;I<=3;I++) For (j=1;j<=10;j++) cout << sum; cout << I << “*” <<j<<“=“<<I*j; 15/22
  • 16. The while Loop Syntax 16/22
  • 17. The while Loop Control Flow 17/22
  • 18. Example i = 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. The do Loop Syntax 19/22
  • 20. The do Loop Control Flow 20/22
  • 21. Example Do Cin >> n; {cout <<I; Do { I = I +1; cout <<I; } while (I<=10); I ++; } while (I<=n) 21/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 22/22
  • 23. Using Functions To Aid Modularity . . . void starline(); // function prototype int main() { . . . starline(); . . . starline(); return 0; } void starline() // function definition { for(int j=0; j<45; j++) cout << '*'; cout << endl; 23/22 }
  • 24. 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; 24/22 }
  • 25. 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; 25/22 }
  • 26. 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; 26/22 }
  • 27. Structures Within Structures struct 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