SlideShare a Scribd company logo
1 of 29
Download to read offline
Introduction to
programming
with
C++
Day 2 (Conditions & Loops)
By : eng / Khaled ahmed
• Programming course :
• (C++ , problem solving) :
1- Introduction
2-Conditions & Loops
3-Arrays
4-Strings
5-Functions
6- struct & review
7- OOP in C++
8- INHERITANCE IN C++
9-files ( input & output )
10-General Review
ARITHMETIC OPERATIONS 3
▪For example, to multiply a times the quantity b + c
we write a * ( b + c ).
▪There is no arithmetic operator for exponentiation in C++,
so x2 is represented as x * x.
PRECEDENCE OF ARITHMETIC OPERATIONS
? = 1 + 2 * (3 + 4)
 Evaluated as 1 +(2 * (3+4))and the result is 15
? = 5 * 2 + 9 % 4
 Evaluated as (5*2) + (9 % 4) and the result is 11
4
INCREMENT AND DECREMENT
OPERATORS
Increment operator: increment variable by 1
 Pre-increment: ++variable
 Post-increment: variable++
Decrement operator: decrement variable by 1
 Pre-decrement: --variable
 Post-decrement: variable --
 Examples :
++K , K++ k= K+1
--K , K-- K= K-1
5
SPECIAL ASSIGNMENT STATEMENTS
C++ has special assignment statements called
compound assignments
+= , -= , *= , /= , %=
Example:
X +=5 ; means x = x + 5;
x *=y; means x = x * y;
x /=y; means x = x / y;
6
SELECTION STATEMENTS : IF
STATEMENT
Selection statements are used to choose
among alternative courses of action.
For example, suppose the passing mark on
an exam is 60. The pseudocode statement
 If student’s marks is greater than or equal to 60
Then
Print “Passed”
7
8
if ( Expression)
action statement ;
if ( Expression)
{
action statement 1 ;
action statement 2 ;
.
.
action statement n ;
}
In C++ , The syntax for the If statement
if ( grade >= 60 )
cout <<"Passedn“;
RELATIONAL EXPRESSION AND
RELATIONAL OPERATORS
Relational expression is an expression which
compares 2 operands and returns a TRUE or
FALSE answer.
Example : a >= b , a == c , a >= 99 ,
‘A’ > ‘a’
Relational expressions are used to test the
conditions in selection, and looping statements.
9
Operator Means
== Equal To
!= Not Equal To
< Less Than
<= Less Than or Equal To
> Greater Than
>= Greater Than or Equal To
10
if ( Expression)
action statement ;
Else
action statement ;
if ( Expression)
{
action statements 1 ;
.
action statement n ;
}
Else
{
action statements 1 ;
.
action statement n ;
}
In C++ , The syntax for the If…Else statement
if ( grade >= 60 )
cout <<"Passedn“;
Else
cout <<“Failedn”
IF – ELSE IF STATEMENT 11
• For example, write a program that ask the user to Enter 2 numbers and
print out whether they are equal or there is one which is greater than the
other.
Int main()
{
int num1, num2;
cout <<"Enter Number 1 , Number2 n";
cin >>num1>>num2;
if ( num1 == num2 )
cout << "Both Are Equal n";
else if (num1 > num2 )
cout <<"Number 1 is greater than number 2 n";
else
cout <<"Number 2 is greater than number 1 n";
}
COMBINING MORE THAN ONE
CONDITION
12
Operator Means Description
&& And The Expression Value Is true If and Only IF both Conditions
are true
|| OR The Expression Value Is true If one Condition Is True
Example : check whether num1 is between 0 and 100
IF ( (num1 >= 0) && (num1 <=100) )
Cout <<“The Number Is between 1 and 100” ;
Else
Cout <<“ The Number Is Larger Than 100”;

13
Loops
■ Repetition is referred to the ability of
repeating a statement or a set of
statements as many times this is
necessary.
14
Loops
while()
do – while()
for( ; ; )
15
The while() Loop
while (<Condition>)
<Loop body> ;
Con
ditio
n
Loop
Body
true
false
16
#include <iostream>
using namespace std;
int main()
{
cout << ” * n”;
cout << ” *** n”;
cout << ”*****n”;
cout << ” * n”;
cout << ” * n”;
cout << endl;
}
*
***
*****
*
*
*
***
*****
*
*
*
***
*****
*
*
*
***
*****
*
*
*
***
*****
*
*
17
i
Variable
#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 4)
{
cout << ” * n”;
cout << ” *** n”;
cout << ”*****n”;
cout << ” * n”;
cout << ” * n”;
cout << endl;
i++;
}
}
*
***
*****
*
*
*
***
*****
*
*
*
***
*****
*
*
*
***
*****
*
*
true
false
1
2
3
4
5
Screen
18
i
Variable
#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i < 11)
{
i += 3;
cout << i << endl;
}
}
4
7
10
13
1
4
7
10
13
Screen
19
Examples
int main()
{
int i = 1;
while (i<=5)
{
cout << i << ’ ’;
i++;
}
}
int main()
{
int i = 1;
while (i<=5)
{
cout << ’i’ << ’ ’;
++i;
}
}
1 2 3 4 5
i i i i i
20
More Examples
int main()
{
int i = 1;
while (i<=5)
cout << i << ’ ’;
i++;
}
int main()
{
int i = 1;
while (i<=5)
cout << i++ << ’ ’;
}
1 1 1 1 1 1 …
1 2 3 4 5
21
The do–while() Loop
do
<Loop body> ;
while (<Condition>); Con
ditio
n
Loop
Body
true
false
22
The do – while() Loop
do
<Statement> ;
while (<Condition>) ;
do {
<Statement 1> ;
<Statement 2> ;
. . .
} while (<Condition>) ;
23
i
Variable
#include <iostream>
using namespace std;
int main()
{
int i = 1;
do {
cout << ” * n”;
cout << ” *** n”;
cout << ”*****n”;
cout << ” * n”;
cout << ” * n”;
cout << endl;
i++;
} while(i <= 4);
}
*
***
*****
*
*
*
***
*****
*
*
*
***
*****
*
*
*
***
*****
*
*
1
2
3
4
5
Screen
24
i
Variable
#include <iostream>
using namespace std;
int main()
{
int i = 1;
do
{
i += 3;
cout << i << endl;
} while(i < 11);
}
4
7
10
13
1
4
7
10
13
Screen
25
The for( ; ; ) Loop
for (<init> ; <condition> ; <change>)
<Loop body> ;
init is usually an assignment to give a loop counter an initial
value. Executed ONLY when entering the loop. Can also declare
variables.
condition is any statement returning an integral value and for
as long as it is true, the statement will be executed. Executed at
every pass.
change is a statement normally to modify the loop counter, so
that eventually it will make condition false and the loop will
terminate. Executed at every pass after the execution of the
loop body.
26
Example 1
1 2 3 4 5 6
int main()
{
int i;
for(i=1; i<7; i++)
cout << i << ” ”;
}
27
Example 2
1 2 3 4 5 6
int main()
{
int i;
for(i=1; i<7; ++i)
cout << i << ” ”;
}
28
Example 3
9 8 7 6
int main()
{
int i;
for(i=9; i>5; --i)
cout << i << ” ”;
}
lesson 2.pptx

More Related Content

Similar to lesson 2.pptx

Similar to lesson 2.pptx (20)

Oop1
Oop1Oop1
Oop1
 
L05if
L05ifL05if
L05if
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture04
Lecture04Lecture04
Lecture04
 
Looping
LoopingLooping
Looping
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
 
C++ loop
C++ loop C++ loop
C++ loop
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
Loops
LoopsLoops
Loops
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
C.pdf
C.pdfC.pdf
C.pdf
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 

Recently uploaded

2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdfAndrey Devyatkin
 
What is Mendix and the concept of low-code development.docx
What is Mendix and the concept of low-code development.docxWhat is Mendix and the concept of low-code development.docx
What is Mendix and the concept of low-code development.docxTechnogeeks
 
Santander Stream Processing with Apache Flink
Santander Stream Processing with Apache FlinkSantander Stream Processing with Apache Flink
Santander Stream Processing with Apache Flinkconfluent
 
Effort Estimation Techniques used in Software Projects
Effort Estimation Techniques used in Software ProjectsEffort Estimation Techniques used in Software Projects
Effort Estimation Techniques used in Software ProjectsDEEPRAJ PATHAK
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptxVinzoCenzo
 
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...OnePlan Solutions
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
OpenMetadata Community Meeting - 4th April, 2024
OpenMetadata Community Meeting - 4th April, 2024OpenMetadata Community Meeting - 4th April, 2024
OpenMetadata Community Meeting - 4th April, 2024OpenMetadata
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxRTS corp
 
Understanding Plagiarism: Causes, Consequences and Prevention.pptx
Understanding Plagiarism: Causes, Consequences and Prevention.pptxUnderstanding Plagiarism: Causes, Consequences and Prevention.pptx
Understanding Plagiarism: Causes, Consequences and Prevention.pptxSasikiranMarri
 
oracle 23c new features for developer and dba
oracle 23c new features for developer and dbaoracle 23c new features for developer and dba
oracle 23c new features for developer and dbaRemote DBA Services
 
Mastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxMastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxAS Design & AST.
 
Revolutionize Your Video Editing with InVideo.io: A Comprehensive Review
Revolutionize Your Video Editing with InVideo.io: A Comprehensive ReviewRevolutionize Your Video Editing with InVideo.io: A Comprehensive Review
Revolutionize Your Video Editing with InVideo.io: A Comprehensive Reviewjw364beach
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shardsChristopher Curtin
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
logical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptxlogical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptxRemote DBA Services
 
Key Steps in Agile Software Delivery Roadmap
Key Steps in Agile Software Delivery RoadmapKey Steps in Agile Software Delivery Roadmap
Key Steps in Agile Software Delivery RoadmapIshara Amarasekera
 
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...kalichargn70th171
 

Recently uploaded (20)

2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
 
What is Mendix and the concept of low-code development.docx
What is Mendix and the concept of low-code development.docxWhat is Mendix and the concept of low-code development.docx
What is Mendix and the concept of low-code development.docx
 
Santander Stream Processing with Apache Flink
Santander Stream Processing with Apache FlinkSantander Stream Processing with Apache Flink
Santander Stream Processing with Apache Flink
 
Effort Estimation Techniques used in Software Projects
Effort Estimation Techniques used in Software ProjectsEffort Estimation Techniques used in Software Projects
Effort Estimation Techniques used in Software Projects
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptx
 
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
OpenMetadata Community Meeting - 4th April, 2024
OpenMetadata Community Meeting - 4th April, 2024OpenMetadata Community Meeting - 4th April, 2024
OpenMetadata Community Meeting - 4th April, 2024
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptx
 
Understanding Plagiarism: Causes, Consequences and Prevention.pptx
Understanding Plagiarism: Causes, Consequences and Prevention.pptxUnderstanding Plagiarism: Causes, Consequences and Prevention.pptx
Understanding Plagiarism: Causes, Consequences and Prevention.pptx
 
oracle 23c new features for developer and dba
oracle 23c new features for developer and dbaoracle 23c new features for developer and dba
oracle 23c new features for developer and dba
 
Mastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxMastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptx
 
Revolutionize Your Video Editing with InVideo.io: A Comprehensive Review
Revolutionize Your Video Editing with InVideo.io: A Comprehensive ReviewRevolutionize Your Video Editing with InVideo.io: A Comprehensive Review
Revolutionize Your Video Editing with InVideo.io: A Comprehensive Review
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
logical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptxlogical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptx
 
Key Steps in Agile Software Delivery Roadmap
Key Steps in Agile Software Delivery RoadmapKey Steps in Agile Software Delivery Roadmap
Key Steps in Agile Software Delivery Roadmap
 
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
 

lesson 2.pptx

  • 1. Introduction to programming with C++ Day 2 (Conditions & Loops) By : eng / Khaled ahmed
  • 2. • Programming course : • (C++ , problem solving) : 1- Introduction 2-Conditions & Loops 3-Arrays 4-Strings 5-Functions 6- struct & review 7- OOP in C++ 8- INHERITANCE IN C++ 9-files ( input & output ) 10-General Review
  • 3. ARITHMETIC OPERATIONS 3 ▪For example, to multiply a times the quantity b + c we write a * ( b + c ). ▪There is no arithmetic operator for exponentiation in C++, so x2 is represented as x * x.
  • 4. PRECEDENCE OF ARITHMETIC OPERATIONS ? = 1 + 2 * (3 + 4)  Evaluated as 1 +(2 * (3+4))and the result is 15 ? = 5 * 2 + 9 % 4  Evaluated as (5*2) + (9 % 4) and the result is 11 4
  • 5. INCREMENT AND DECREMENT OPERATORS Increment operator: increment variable by 1  Pre-increment: ++variable  Post-increment: variable++ Decrement operator: decrement variable by 1  Pre-decrement: --variable  Post-decrement: variable --  Examples : ++K , K++ k= K+1 --K , K-- K= K-1 5
  • 6. SPECIAL ASSIGNMENT STATEMENTS C++ has special assignment statements called compound assignments += , -= , *= , /= , %= Example: X +=5 ; means x = x + 5; x *=y; means x = x * y; x /=y; means x = x / y; 6
  • 7. SELECTION STATEMENTS : IF STATEMENT Selection statements are used to choose among alternative courses of action. For example, suppose the passing mark on an exam is 60. The pseudocode statement  If student’s marks is greater than or equal to 60 Then Print “Passed” 7
  • 8. 8 if ( Expression) action statement ; if ( Expression) { action statement 1 ; action statement 2 ; . . action statement n ; } In C++ , The syntax for the If statement if ( grade >= 60 ) cout <<"Passedn“;
  • 9. RELATIONAL EXPRESSION AND RELATIONAL OPERATORS Relational expression is an expression which compares 2 operands and returns a TRUE or FALSE answer. Example : a >= b , a == c , a >= 99 , ‘A’ > ‘a’ Relational expressions are used to test the conditions in selection, and looping statements. 9 Operator Means == Equal To != Not Equal To < Less Than <= Less Than or Equal To > Greater Than >= Greater Than or Equal To
  • 10. 10 if ( Expression) action statement ; Else action statement ; if ( Expression) { action statements 1 ; . action statement n ; } Else { action statements 1 ; . action statement n ; } In C++ , The syntax for the If…Else statement if ( grade >= 60 ) cout <<"Passedn“; Else cout <<“Failedn”
  • 11. IF – ELSE IF STATEMENT 11 • For example, write a program that ask the user to Enter 2 numbers and print out whether they are equal or there is one which is greater than the other. Int main() { int num1, num2; cout <<"Enter Number 1 , Number2 n"; cin >>num1>>num2; if ( num1 == num2 ) cout << "Both Are Equal n"; else if (num1 > num2 ) cout <<"Number 1 is greater than number 2 n"; else cout <<"Number 2 is greater than number 1 n"; }
  • 12. COMBINING MORE THAN ONE CONDITION 12 Operator Means Description && And The Expression Value Is true If and Only IF both Conditions are true || OR The Expression Value Is true If one Condition Is True Example : check whether num1 is between 0 and 100 IF ( (num1 >= 0) && (num1 <=100) ) Cout <<“The Number Is between 1 and 100” ; Else Cout <<“ The Number Is Larger Than 100”; 
  • 13. 13 Loops ■ Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary.
  • 15. 15 The while() Loop while (<Condition>) <Loop body> ; Con ditio n Loop Body true false
  • 16. 16 #include <iostream> using namespace std; int main() { cout << ” * n”; cout << ” *** n”; cout << ”*****n”; cout << ” * n”; cout << ” * n”; cout << endl; } * *** ***** * * * *** ***** * * * *** ***** * * * *** ***** * * * *** ***** * *
  • 17. 17 i Variable #include <iostream> using namespace std; int main() { int i = 1; while (i <= 4) { cout << ” * n”; cout << ” *** n”; cout << ”*****n”; cout << ” * n”; cout << ” * n”; cout << endl; i++; } } * *** ***** * * * *** ***** * * * *** ***** * * * *** ***** * * true false 1 2 3 4 5 Screen
  • 18. 18 i Variable #include <iostream> using namespace std; int main() { int i = 1; while (i < 11) { i += 3; cout << i << endl; } } 4 7 10 13 1 4 7 10 13 Screen
  • 19. 19 Examples int main() { int i = 1; while (i<=5) { cout << i << ’ ’; i++; } } int main() { int i = 1; while (i<=5) { cout << ’i’ << ’ ’; ++i; } } 1 2 3 4 5 i i i i i
  • 20. 20 More Examples int main() { int i = 1; while (i<=5) cout << i << ’ ’; i++; } int main() { int i = 1; while (i<=5) cout << i++ << ’ ’; } 1 1 1 1 1 1 … 1 2 3 4 5
  • 21. 21 The do–while() Loop do <Loop body> ; while (<Condition>); Con ditio n Loop Body true false
  • 22. 22 The do – while() Loop do <Statement> ; while (<Condition>) ; do { <Statement 1> ; <Statement 2> ; . . . } while (<Condition>) ;
  • 23. 23 i Variable #include <iostream> using namespace std; int main() { int i = 1; do { cout << ” * n”; cout << ” *** n”; cout << ”*****n”; cout << ” * n”; cout << ” * n”; cout << endl; i++; } while(i <= 4); } * *** ***** * * * *** ***** * * * *** ***** * * * *** ***** * * 1 2 3 4 5 Screen
  • 24. 24 i Variable #include <iostream> using namespace std; int main() { int i = 1; do { i += 3; cout << i << endl; } while(i < 11); } 4 7 10 13 1 4 7 10 13 Screen
  • 25. 25 The for( ; ; ) Loop for (<init> ; <condition> ; <change>) <Loop body> ; init is usually an assignment to give a loop counter an initial value. Executed ONLY when entering the loop. Can also declare variables. condition is any statement returning an integral value and for as long as it is true, the statement will be executed. Executed at every pass. change is a statement normally to modify the loop counter, so that eventually it will make condition false and the loop will terminate. Executed at every pass after the execution of the loop body.
  • 26. 26 Example 1 1 2 3 4 5 6 int main() { int i; for(i=1; i<7; i++) cout << i << ” ”; }
  • 27. 27 Example 2 1 2 3 4 5 6 int main() { int i; for(i=1; i<7; ++i) cout << i << ” ”; }
  • 28. 28 Example 3 9 8 7 6 int main() { int i; for(i=9; i>5; --i) cout << i << ” ”; }