SlideShare a Scribd company logo
1 of 15
EEB 2209: OBJECT-ORIENTED PROGRAMMING (OOP)
MBEYA UNIVERSITY OF SCIENCE AND
TECHNOLOGY
COLLEGE OF INFORMATION AND COMMUNICATION TECHNOLOGY
DEPARTMENT OF INFORMATICS
Control Structures
 Conditional Structures: if and else
• The if keyword is used to execute a statement or block only if a
condition is fulfilled. Its form is:
if (condition) statement
* Where condition is the expression that is being evaluated.
• If this condition is true, statement is executed.
• If it is false, statement is ignored (not executed) and the program
continues right after this conditional structure.
• We can additionally specify what we want to happen if the
condition is not fulfilled by using the keyword else. Its form is:
if (condition) statement1 else statement2
Examples:
if (x == 100)
cout<< “x is 100”;
else
cout<< “x is not
100”;
if (x > 0)
cout<< “x is positive”;
else if (x < 0
cout<< “x is negative”;
else
cout<< “x is 0”;
if (x == 100)
cout<< “x is ”;
cout<< x;
 Iteration structures (loops) - Loops have as purpose to repeat a
statement a certain number of times or while a condition is
fulfilled.
• The while loop – Its format is:
while (expression) statement
• The do-while loop - Its format is:
do statement while (condition);
• The for loop - Its format is:
for (initialization; condition; increase) statement;
• while loop: Example– Write a program that countdown using a while-loop:
//custom countdown using while
#include<iostream>
Using namespace std;
int main ( )
{
int n;
cout<<“ Enter the starting number : “
cin>> n;
While (n > 0)
{
cout<< n << “ ,“;
--n;
}
cout << “FIRE!n”;
return 0;
}
The output will be as follows:
Enter the starting number : 9
9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
• do while - For example, the following example program echoes
any number you enter until you enter 0.
//number echoer
#include <iostream>
using namespace std;
int main ( ){
unsigned long n;
do {
cout << "Enter number (0 to end) : ";
cin >> n;
cout << "You entered: << n <<"n";
} while (n != 0);
return 0;
}
The output:
Enter number (0 to end) : 12345
You entered: 12345
Enter number (0 to end) : 160277
You entered: 160277
Enter number (0 to end) : 0
You entered: 0
• for loop - Here is an example of countdown using a for loop:
 Jump statements
• The break statement
Using break we can leave a loop even if the condition for its
end is not fulfilled. It can be used to end an infinite loop, or to
force it to end before its natural end.
Example of using break statement:
• The continue statement
The continue statement causes the program to skip the rest of
the loop in the current iteration as if the end of the statement
block had been reached, causing it to jump to the start of the
following iteration.
Example of using the continue statement:
• The goto statement
goto allows to make an absolute jump to another point in the
program.
 The destination point is identified by a label, which is then
used as an argument for the goto statement.
 A label is made of a valid identifier followed by a colon (:).
• Example program using goto statement
 The selective structure
• Switch: Its objective is to check
several possible constant values for
an expression.
• Its general form is:
switch (expression)
{
case constant1:
group of statements 1;
break;
case constant2:
group of statements 2;
break;
.
.
.
default:
default group of statements
}
• Example
Both of the following code fragments have the same behavior:

More Related Content

Similar to Lect3-C--EEB.pptx

Similar to Lect3-C--EEB.pptx (20)

Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
 
C++ loop
C++ loop C++ loop
C++ loop
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
CHAPTER-3a.ppt
CHAPTER-3a.pptCHAPTER-3a.ppt
CHAPTER-3a.ppt
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
Loops c++
Loops c++Loops c++
Loops c++
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 

Recently uploaded

Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilVinayVitekari
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxchumtiyababu
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadhamedmustafa094
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEselvakumar948
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 

Recently uploaded (20)

Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 

Lect3-C--EEB.pptx

  • 1. EEB 2209: OBJECT-ORIENTED PROGRAMMING (OOP) MBEYA UNIVERSITY OF SCIENCE AND TECHNOLOGY COLLEGE OF INFORMATION AND COMMUNICATION TECHNOLOGY DEPARTMENT OF INFORMATICS
  • 2. Control Structures  Conditional Structures: if and else • The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is: if (condition) statement * Where condition is the expression that is being evaluated. • If this condition is true, statement is executed. • If it is false, statement is ignored (not executed) and the program continues right after this conditional structure.
  • 3. • We can additionally specify what we want to happen if the condition is not fulfilled by using the keyword else. Its form is: if (condition) statement1 else statement2 Examples: if (x == 100) cout<< “x is 100”; else cout<< “x is not 100”; if (x > 0) cout<< “x is positive”; else if (x < 0 cout<< “x is negative”; else cout<< “x is 0”; if (x == 100) cout<< “x is ”; cout<< x;
  • 4.  Iteration structures (loops) - Loops have as purpose to repeat a statement a certain number of times or while a condition is fulfilled. • The while loop – Its format is: while (expression) statement • The do-while loop - Its format is: do statement while (condition); • The for loop - Its format is: for (initialization; condition; increase) statement;
  • 5. • while loop: Example– Write a program that countdown using a while-loop: //custom countdown using while #include<iostream> Using namespace std; int main ( ) { int n; cout<<“ Enter the starting number : “ cin>> n; While (n > 0) { cout<< n << “ ,“; --n; } cout << “FIRE!n”; return 0; } The output will be as follows: Enter the starting number : 9 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
  • 6. • do while - For example, the following example program echoes any number you enter until you enter 0. //number echoer #include <iostream> using namespace std; int main ( ){ unsigned long n; do { cout << "Enter number (0 to end) : "; cin >> n; cout << "You entered: << n <<"n"; } while (n != 0); return 0; } The output: Enter number (0 to end) : 12345 You entered: 12345 Enter number (0 to end) : 160277 You entered: 160277 Enter number (0 to end) : 0 You entered: 0
  • 7. • for loop - Here is an example of countdown using a for loop:
  • 8.  Jump statements • The break statement Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end.
  • 9. Example of using break statement:
  • 10. • The continue statement The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration.
  • 11. Example of using the continue statement:
  • 12. • The goto statement goto allows to make an absolute jump to another point in the program.  The destination point is identified by a label, which is then used as an argument for the goto statement.  A label is made of a valid identifier followed by a colon (:).
  • 13. • Example program using goto statement
  • 14.  The selective structure • Switch: Its objective is to check several possible constant values for an expression. • Its general form is: switch (expression) { case constant1: group of statements 1; break; case constant2: group of statements 2; break; . . . default: default group of statements }
  • 15. • Example Both of the following code fragments have the same behavior: