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

INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Chandrakant Divate
 
L05if
L05ifL05if
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3 rohassanie
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
Zohaib Ahmed
 
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
Hemantha Kulathilake
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
Mohammed Saleh
 
Looping
LoopingLooping
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
ShifatiRabbi
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
Ayman Hassan
 
C++ loop
C++ loop C++ loop
C++ loop
Khelan Ameen
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
Vikash Dhal
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
Farhan Ab Rahman
 
Loops
LoopsLoops
Loops
Kamran
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
SumitSingh813090
 
C.pdf
C.pdfC.pdf

Similar to lesson 2.pptx (20)

Oop1
Oop1Oop1
Oop1
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
 
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
 

Recently uploaded

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 

Recently uploaded (20)

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 

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 << ” ”; }