SlideShare a Scribd company logo
Welcome
http://www.mahergelle.com
By Eng.Adulahi M. Adan
C++
Chapter Three: -
Loops, Arrays and
Functions
C++ Loops
 Often when you write code, you want the same block of
code to run over and over again in a row. Instead of adding
several almost equal code-lines in a script, we can use
loops to perform a task like this.
 In C++, we have the following looping statements:
 while - loops through a block of code as long as the
specified condition is true
 do...while - loops through a block of code once, and then
repeats the loop as long as the specified condition is true
 for - loops through a block of code a specified number of
times
Contents
C++ Loops
 The C++ while Loop
 The while loop executes a block of code as long as the
specified condition is true.
 Syntax
 while (condition) {
// code block to be executed
}
 Example
Contents
int i = 0;
while (i < 5) {
cout << i << "n";
i++; }
C++ Loops
 The C++ do...while Loop
 The do/while loop is a variant of the while loop. This loop
will execute the code block once, before checking if the
condition is true, then it will repeat the loop as long as the
condition is true.
 Syntax Example
 do {
// code block to be executed
}
while (condition);
Contents
int i = 0;
do {
cout << i << "n";
i++;
}
while (i < 5);
C++ Loops
 The C++ for Loop
 When you know exactly how many times you want to loop through
a block of code, use the for loop instead of a while loop:
 Syntax
 for (statement 1; statement 2; statement 3) {
// code block to be executed
}
 Example
Contents
for (int i = 0; i < 5; i++) {
cout << i << "n";
}
C++ Arrays
 Arrays are used to store multiple values in a single
variable, instead of declaring separate variables for each
value.
 To declare an array, define the variable type, specify the
name of the array followed by square brackets and
specify the number of elements it should store
 string cars[4];
 We have now declared a variable that holds an array of
four strings. To insert values to it, we can use an array
literal - place the values in a comma-separated list, inside
curly braces:
 string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
Contents
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[4] = {"Volvo",
"BMW", "Ford", "Mazda"};
cout << cars[0];
return 0;
}
C++ Arrays
 Access the Elements of an Array
 You access an array element by referring to the index
number inside square brackets [].
 This statement accesses the value of the first
element in cars:
 Example
Contents
C++ Functions
 A function is a block of code which only runs when it is
called.
 You can pass data, known as parameters, into a function.
 Functions are used to perform certain actions, and they are
important for reusing code: Define the code once, and use
it many times.
Contents
C++ Functions
 Create a Function
 C++ provides some pre-defined functions, such as main(),
which is used to execute code. But you can also create
your own functions to perform certain actions.
 To create (often referred to as declare) a function, specify
the name of the function, followed by parentheses ():
 Syntax
 void myFunction() {
// code to be executed
}
Contents
C++ Functions
 Call a Function
 Declared functions are not executed immediately. They are
"saved for later use", and will be executed later, when they are
called.
 To call a function, write the function's name followed by two
parentheses () and a semicolon ;
 In the following example, myFunction() is used to print a text
(the action), when it is called
 Function Declaration and Definition
 A C++ function consist of two parts:
 void myFunction() { // declaration
// the body of the function (definition)
}
Contents
C++ Functions
 Example
Contents
#include <iostream>
using namespace std;
void myFunction() {
cout << "I just got executed!";
}
int main() {
myFunction();
return 0;
}
C++ Function Parameters
 Information can be passed to functions as a parameter.
Parameters act as variables inside the function.
 Parameters are specified after the function name, inside
the parentheses. You can add as many parameters as you
want, just separate them with a comma
 Syntax
 void functionName(parameter1, parameter2, parameter3) {
// code to be executed
}
Contents
C++ Function Parameters
 Example
Contents
#include <iostream>
using namespace std;
void myFunction(string fname) {
cout << fname << " Refsnesn";
}
int main() {
myFunction("Liam");
myFunction("Jenny");
myFunction("Anja");
return 0; }
C++ Default Parameters
 Default Parameter Value
 You can also use a default parameter value, by
using the equals sign (=).
 If we call the function without an argument, it
uses the default value ("Norway"):
Contents
C++ Default Parameters
 Example
Contents
#include <iostream>
using namespace std;
void myFunction(string country = "Norway") {
cout << country << "n";
}
int main() {
myFunction("Sweden");
myFunction("India");
myFunction();
myFunction("USA");
return 0; }
Multiple Parameters
 Multiple Parameters
 Inside the function, you can add as many
parameters as you want:
Contents
Multiple Parameters
 Example
#include <iostream>
using namespace std;
void myFunction(string fname, int age) {
cout << fname << " Refsnes. " << age << " years old. n";
}
int main() {
myFunction("Liam", 3);
myFunction("Jenny", 14);
myFunction("Anja", 30);
return 0;
}
Contents
Return Values
 The void keyword, used in the previous examples, indicates
that the function should not return a value.
 If you want the function to return a value, you can use a data
type (such as int, string, etc.) instead of void, and use the
return keyword inside the function. Example
Contents
#include <iostream>
using namespace std;
int myFunction(int x) {
return 5 + x;
}
int main() {
cout << myFunction(3);
return (0);
}
Return Values
This example returns the sum of a function with two parameters:
Contents
#include <iostream>
using namespace std;
int myFunction(int x, int y) {
return x + y;
}
int main() {
cout << myFunction(5, 3);
return 0;
}
Return Values
You can also store the result in a variable:
Contents
#include <iostream>
using namespace std;
int myFunction(int x, int y) {
return x + y;
}
int main() {
int z = myFunction(5, 3);
cout << z;
return 0;
}
Chapter End
Final Exam
PHP AND C++
Part One: - Multiple Choose (15 * 1 = 15 M) 2page
Part Two : - Define (5 *2 = 10 M) 1 page
Part Three: - Code (5 * 3 = 15 M)
Part Four: - True or False (10 M) 1page
COMPUTER HARDWARE AND DATABASE 1
Part One: - Multiple Choose (10 * 2 = 20 M)
Part Two: - True or False (10*1=10 M)
Part Three: - Match(10*1=10 M)
Part Four: - Define (5*2= 10 M)
Thank you
@Eng.Abdulahi Mohamed

More Related Content

PPT
PDF
PPTX
Classes function overloading
PPTX
Functions1
PPTX
Chapter 1 (2) array and structure r.pptx
PPTX
Loops in C# for loops while and do while loop.
DOCX
Bc0037
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
Classes function overloading
Functions1
Chapter 1 (2) array and structure r.pptx
Loops in C# for loops while and do while loop.
Bc0037
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf

Similar to C++ Functions C++ Conditions.pptx vd.pptx (20)

PPTX
Programming in C (part 2)
PPTX
6. Functions in C ++ programming object oriented programming
PDF
C++ Course - Lesson 3
PPTX
Fundamental of programming Fundamental of programming
PPTX
JavaScript Arrays and its types .pptx
PPTX
Getting started with ES6
PPTX
OOPS USING C++(UNIT 2)
DOCX
18 dec pointers and scope resolution operator
DOCX
Arrry structure Stacks in data structure
PPTX
Object Oriented Programming using C++ Unit 1
PPTX
ppt8-string-1.pptx
PPTX
OOC MODULE1.pptx
PPTX
9 functions.pptxFunction that are used in
PPTX
Chapter1.pptx
PPTX
Object oriented programming system with C++
DOCX
Functions in c++
PPT
C++ Functions.ppt
PPTX
Function C++
PDF
Object Oriented Programming (OOP) using C++ - Lecture 1
PPTX
TEMPLATES IN JAVA
Programming in C (part 2)
6. Functions in C ++ programming object oriented programming
C++ Course - Lesson 3
Fundamental of programming Fundamental of programming
JavaScript Arrays and its types .pptx
Getting started with ES6
OOPS USING C++(UNIT 2)
18 dec pointers and scope resolution operator
Arrry structure Stacks in data structure
Object Oriented Programming using C++ Unit 1
ppt8-string-1.pptx
OOC MODULE1.pptx
9 functions.pptxFunction that are used in
Chapter1.pptx
Object oriented programming system with C++
Functions in c++
C++ Functions.ppt
Function C++
Object Oriented Programming (OOP) using C++ - Lecture 1
TEMPLATES IN JAVA
Ad

More from MAHERMOHAMED27 (20)

PPTX
SDA Chap 5.pptxSDA Chap 5.pptxSDA Chap 5.pptx
PPTX
Our-ICT-Journey.pptxOur-ICT-Journey.pptx
PPTX
My_First_ASP.NET_Page My PRoject for A.pptx
PPTX
Introduction-to-ASPNET-Core ASP.NET.pptx
PPTX
Linux Operaring System chapter one Introduction.pptx
PPTX
C++ Introduction C+ Conditions.pptx.pptx
PPTX
C++ Condition C++ Conditionsd.pptxs.pptx
PPTX
Financial Accounting Chapter two-1-1.pptx
PPT
Data Structure and AlgorithmChapter1.ppt
PPTX
Software-Modeling-in-Software-Engineering.pptx
PPTX
Networking lesson 4 chaoter 1 Module 4-1.pptx
PPTX
ERD_Introduction_Presentation for Er diag
PPTX
Introduction to databae eChapter 1-.pptx
PPTX
Introduction to database Chapter One DB1.pptx
PPTX
introduction to Data Base Forms and reports.pptx
PPTX
CH3 PHOTOSHOP TOOLBOX photoshop is the best desing tool
PPTX
Networking Lesson 1 up to 4 neyworking is one the of thebesy
PPTX
Ch-2 final exam documet compler design elements
PPTX
System Analysis and Design Project documentation
PPTX
Computer Maintenance Presentation.pptx
SDA Chap 5.pptxSDA Chap 5.pptxSDA Chap 5.pptx
Our-ICT-Journey.pptxOur-ICT-Journey.pptx
My_First_ASP.NET_Page My PRoject for A.pptx
Introduction-to-ASPNET-Core ASP.NET.pptx
Linux Operaring System chapter one Introduction.pptx
C++ Introduction C+ Conditions.pptx.pptx
C++ Condition C++ Conditionsd.pptxs.pptx
Financial Accounting Chapter two-1-1.pptx
Data Structure and AlgorithmChapter1.ppt
Software-Modeling-in-Software-Engineering.pptx
Networking lesson 4 chaoter 1 Module 4-1.pptx
ERD_Introduction_Presentation for Er diag
Introduction to databae eChapter 1-.pptx
Introduction to database Chapter One DB1.pptx
introduction to Data Base Forms and reports.pptx
CH3 PHOTOSHOP TOOLBOX photoshop is the best desing tool
Networking Lesson 1 up to 4 neyworking is one the of thebesy
Ch-2 final exam documet compler design elements
System Analysis and Design Project documentation
Computer Maintenance Presentation.pptx
Ad

Recently uploaded (20)

PDF
Deliverable file - Regulatory guideline analysis.pdf
PPTX
Negotiation and Persuasion Skills: A Shrewd Person's Perspective
PDF
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
PPT
Lecture 3344;;,,(,(((((((((((((((((((((((
PDF
Blood Collected straight from the donor into a blood bag and mixed with an an...
PPTX
2025 Product Deck V1.0.pptxCATALOGTCLCIA
PDF
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
PDF
How to Get Business Funding for Small Business Fast
PDF
How to Get Approval for Business Funding
PDF
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
PDF
Module 2 - Modern Supervison Challenges - Student Resource.pdf
PDF
How to Get Funding for Your Trucking Business
PDF
1911 Gold Corporate Presentation Aug 2025.pdf
PDF
Building a Smart Pet Ecosystem: A Full Introduction to Zhejiang Beijing Techn...
PPTX
TRAINNING, DEVELOPMENT AND APPRAISAL.pptx
PDF
Digital Marketing & E-commerce Certificate Glossary.pdf.................
PPTX
Slide gioi thieu VietinBank Quy 2 - 2025
PDF
Charisse Litchman: A Maverick Making Neurological Care More Accessible
PDF
NewBase 12 August 2025 Energy News issue - 1812 by Khaled Al Awadi_compresse...
PDF
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
Deliverable file - Regulatory guideline analysis.pdf
Negotiation and Persuasion Skills: A Shrewd Person's Perspective
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
Lecture 3344;;,,(,(((((((((((((((((((((((
Blood Collected straight from the donor into a blood bag and mixed with an an...
2025 Product Deck V1.0.pptxCATALOGTCLCIA
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
How to Get Business Funding for Small Business Fast
How to Get Approval for Business Funding
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
Module 2 - Modern Supervison Challenges - Student Resource.pdf
How to Get Funding for Your Trucking Business
1911 Gold Corporate Presentation Aug 2025.pdf
Building a Smart Pet Ecosystem: A Full Introduction to Zhejiang Beijing Techn...
TRAINNING, DEVELOPMENT AND APPRAISAL.pptx
Digital Marketing & E-commerce Certificate Glossary.pdf.................
Slide gioi thieu VietinBank Quy 2 - 2025
Charisse Litchman: A Maverick Making Neurological Care More Accessible
NewBase 12 August 2025 Energy News issue - 1812 by Khaled Al Awadi_compresse...
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi

C++ Functions C++ Conditions.pptx vd.pptx

  • 3. Chapter Three: - Loops, Arrays and Functions
  • 4. C++ Loops  Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal code-lines in a script, we can use loops to perform a task like this.  In C++, we have the following looping statements:  while - loops through a block of code as long as the specified condition is true  do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true  for - loops through a block of code a specified number of times Contents
  • 5. C++ Loops  The C++ while Loop  The while loop executes a block of code as long as the specified condition is true.  Syntax  while (condition) { // code block to be executed }  Example Contents int i = 0; while (i < 5) { cout << i << "n"; i++; }
  • 6. C++ Loops  The C++ do...while Loop  The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.  Syntax Example  do { // code block to be executed } while (condition); Contents int i = 0; do { cout << i << "n"; i++; } while (i < 5);
  • 7. C++ Loops  The C++ for Loop  When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:  Syntax  for (statement 1; statement 2; statement 3) { // code block to be executed }  Example Contents for (int i = 0; i < 5; i++) { cout << i << "n"; }
  • 8. C++ Arrays  Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.  To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store  string cars[4];  We have now declared a variable that holds an array of four strings. To insert values to it, we can use an array literal - place the values in a comma-separated list, inside curly braces:  string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"}; Contents
  • 9. #include <iostream> #include <string> using namespace std; int main() { string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"}; cout << cars[0]; return 0; } C++ Arrays  Access the Elements of an Array  You access an array element by referring to the index number inside square brackets [].  This statement accesses the value of the first element in cars:  Example Contents
  • 10. C++ Functions  A function is a block of code which only runs when it is called.  You can pass data, known as parameters, into a function.  Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times. Contents
  • 11. C++ Functions  Create a Function  C++ provides some pre-defined functions, such as main(), which is used to execute code. But you can also create your own functions to perform certain actions.  To create (often referred to as declare) a function, specify the name of the function, followed by parentheses ():  Syntax  void myFunction() { // code to be executed } Contents
  • 12. C++ Functions  Call a Function  Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are called.  To call a function, write the function's name followed by two parentheses () and a semicolon ;  In the following example, myFunction() is used to print a text (the action), when it is called  Function Declaration and Definition  A C++ function consist of two parts:  void myFunction() { // declaration // the body of the function (definition) } Contents
  • 13. C++ Functions  Example Contents #include <iostream> using namespace std; void myFunction() { cout << "I just got executed!"; } int main() { myFunction(); return 0; }
  • 14. C++ Function Parameters  Information can be passed to functions as a parameter. Parameters act as variables inside the function.  Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma  Syntax  void functionName(parameter1, parameter2, parameter3) { // code to be executed } Contents
  • 15. C++ Function Parameters  Example Contents #include <iostream> using namespace std; void myFunction(string fname) { cout << fname << " Refsnesn"; } int main() { myFunction("Liam"); myFunction("Jenny"); myFunction("Anja"); return 0; }
  • 16. C++ Default Parameters  Default Parameter Value  You can also use a default parameter value, by using the equals sign (=).  If we call the function without an argument, it uses the default value ("Norway"): Contents
  • 17. C++ Default Parameters  Example Contents #include <iostream> using namespace std; void myFunction(string country = "Norway") { cout << country << "n"; } int main() { myFunction("Sweden"); myFunction("India"); myFunction(); myFunction("USA"); return 0; }
  • 18. Multiple Parameters  Multiple Parameters  Inside the function, you can add as many parameters as you want: Contents
  • 19. Multiple Parameters  Example #include <iostream> using namespace std; void myFunction(string fname, int age) { cout << fname << " Refsnes. " << age << " years old. n"; } int main() { myFunction("Liam", 3); myFunction("Jenny", 14); myFunction("Anja", 30); return 0; } Contents
  • 20. Return Values  The void keyword, used in the previous examples, indicates that the function should not return a value.  If you want the function to return a value, you can use a data type (such as int, string, etc.) instead of void, and use the return keyword inside the function. Example Contents #include <iostream> using namespace std; int myFunction(int x) { return 5 + x; } int main() { cout << myFunction(3); return (0); }
  • 21. Return Values This example returns the sum of a function with two parameters: Contents #include <iostream> using namespace std; int myFunction(int x, int y) { return x + y; } int main() { cout << myFunction(5, 3); return 0; }
  • 22. Return Values You can also store the result in a variable: Contents #include <iostream> using namespace std; int myFunction(int x, int y) { return x + y; } int main() { int z = myFunction(5, 3); cout << z; return 0; }
  • 24. Final Exam PHP AND C++ Part One: - Multiple Choose (15 * 1 = 15 M) 2page Part Two : - Define (5 *2 = 10 M) 1 page Part Three: - Code (5 * 3 = 15 M) Part Four: - True or False (10 M) 1page COMPUTER HARDWARE AND DATABASE 1 Part One: - Multiple Choose (10 * 2 = 20 M) Part Two: - True or False (10*1=10 M) Part Three: - Match(10*1=10 M) Part Four: - Define (5*2= 10 M)