0% found this document useful (0 votes)
7 views49 pages

05 Instrctrl en

Uploaded by

aryalrosn0655
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views49 pages

05 Instrctrl en

Uploaded by

aryalrosn0655
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Imperative programming

School of Technology and Management

Control Structures

[email protected]

ESTiG

[email protected] (ESTiG) Imperative programming 1 / 14


Summary

1 Terminal Simple Output

2 Control Structures

3 Examples

[email protected] (ESTiG) Imperative programming 2 / 14


Terminal Simple Output

Summary

1 Terminal Simple Output

2 Control Structures

3 Examples

[email protected] (ESTiG) Imperative programming 3 / 14


Terminal Simple Output

Terminal Simple Output


C applications in Imperative programming → Terminal
Applications
User interaction terminal based Input/Output (I/O)

[email protected] (ESTiG) Imperative programming 4 / 14


Terminal Simple Output

Terminal Simple Output


C applications in Imperative programming → Terminal
Applications
User interaction terminal based Input/Output (I/O)

[email protected] (ESTiG) Imperative programming 4 / 14


Terminal Simple Output

Terminal Simple Output


Development Process desirable to be able to output the state of
variables/entities on the terminal
Although in an introductory manner, it's important to learn an output
function:
http://devdocs.io/c/io/fscanfprintf
Outputs formatted text to the terminal
The function's prototype (header) is:
printf("text format", var1 , var2 , ..., varn );
The text format can contain:
The output text we want to display on the terminal
Escape characters - \n, \t, \b, . . .
Format speciers (placeholders) → 1 for each variable var1 varn

[email protected] (ESTiG) Imperative programming 5 / 14


Terminal Simple Output

Terminal Simple Output


Development Process desirable to be able to output the state of
variables/entities on the terminal
Although in an introductory manner, it's important to learn an output
function:
http://devdocs.io/c/io/fscanfprintf
Outputs formatted text to the terminal
The function's prototype (header) is:
printf("text format", var1 , var2 , ..., varn );
The text format can contain:
The output text we want to display on the terminal
Escape characters - \n, \t, \b, . . .
Format speciers (placeholders) → 1 for each variable var1 varn

[email protected] (ESTiG) Imperative programming 5 / 14


Terminal Simple Output

Terminal Simple Output


Development Process desirable to be able to output the state of
variables/entities on the terminal
Although in an introductory manner, it's important to learn an output
function:
http://devdocs.io/c/io/fscanfprintf
Outputs formatted text to the terminal
The function's prototype (header) is:
printf("text format", var1 , var2 , ..., varn );
The text format can contain:
The output text we want to display on the terminal
Escape characters - \n, \t, \b, . . .
Format speciers (placeholders) → 1 for each variable var1 varn

[email protected] (ESTiG) Imperative programming 5 / 14


Terminal Simple Output

Terminal Simple Output


Development Process desirable to be able to output the state of
variables/entities on the terminal
Although in an introductory manner, it's important to learn an output
function:
http://devdocs.io/c/io/fscanfprintf
Outputs formatted text to the terminal
The function's prototype (header) is:
printf("text format", var1 , var2 , ..., varn );
The text format can contain:
The output text we want to display on the terminal
Escape characters - \n, \t, \b, . . .
Format speciers (placeholders) → 1 for each variable var1 varn

[email protected] (ESTiG) Imperative programming 5 / 14


Terminal Simple Output

Terminal Simple Output


Development Process desirable to be able to output the state of
variables/entities on the terminal
Although in an introductory manner, it's important to learn an output
function:
http://devdocs.io/c/io/fscanfprintf
Outputs formatted text to the terminal
The function's prototype (header) is:
printf("text format", var1 , var2 , ..., varn );
The text format can contain:
The output text we want to display on the terminal
Escape characters - \n, \t, \b, . . .
Format speciers (placeholders) → 1 for each variable var1 varn

[email protected] (ESTiG) Imperative programming 5 / 14


Terminal Simple Output

Terminal Simple Output


Development Process desirable to be able to output the state of
variables/entities on the terminal
Although in an introductory manner, it's important to learn an output
function:
http://devdocs.io/c/io/fscanfprintf
Outputs formatted text to the terminal
The function's prototype (header) is:
printf("text format", var1 , var2 , ..., varn );
The text format can contain:
The output text we want to display on the terminal
Escape characters - \n, \t, \b, . . .
Format speciers (placeholders) → 1 for each variable var1 varn

[email protected] (ESTiG) Imperative programming 5 / 14


Terminal Simple Output

Terminal Simple Output


Development Process desirable to be able to output the state of
variables/entities on the terminal
Although in an introductory manner, it's important to learn an output
function:
http://devdocs.io/c/io/fscanfprintf
Outputs formatted text to the terminal
The function's prototype (header) is:
printf("text format", var1 , var2 , ..., varn );
The text format can contain:
The output text we want to display on the terminal
Escape characters - \n, \t, \b, . . .
Format speciers (placeholders) → 1 for each variable var1 varn

[email protected] (ESTiG) Imperative programming 5 / 14


Terminal Simple Output

Terminal Simple Output


Development Process desirable to be able to output the state of
variables/entities on the terminal
Although in an introductory manner, it's important to learn an output
function:
http://devdocs.io/c/io/fscanfprintf
Outputs formatted text to the terminal
The function's prototype (header) is:
printf("text format", var1 , var2 , ..., varn );
The text format can contain:
The output text we want to display on the terminal
Escape characters - \n, \t, \b, . . .
Format speciers (placeholders) → 1 for each variable var1 varn

[email protected] (ESTiG) Imperative programming 5 / 14


Terminal Simple Output

Terminal Simple Output - printf


The format speciers are character sequences, started with %
followed by information about the type/format
They dene the location and format of each variable in the terminal
output
On the code snippet bellow we can identify:
%d - integer format (supports int)
%f - oating-point format (supports oat or double)
%c - character format (supports char)
\n - introduces a line break (new line)

This are just some of the format speciers provided by printf


The remaining will be addressed in more detail later on the course unit. . .

[email protected] (ESTiG) Imperative programming 6 / 14


Terminal Simple Output

Terminal Simple Output - printf


The format speciers are character sequences, started with %
followed by information about the type/format
They dene the location and format of each variable in the terminal
output
On the code snippet bellow we can identify:
%d - integer format (supports int)
%f - oating-point format (supports oat or double)
%c - character format (supports char)
\n - introduces a line break (new line)

This are just some of the format speciers provided by printf


The remaining will be addressed in more detail later on the course unit. . .

[email protected] (ESTiG) Imperative programming 6 / 14


Terminal Simple Output

Terminal Simple Output - printf


The format speciers are character sequences, started with %
followed by information about the type/format
They dene the location and format of each variable in the terminal
output
On the code snippet bellow we can identify:
%d - integer format (supports int)
%f - oating-point format (supports oat or double)
%c - character format (supports char)
\n - introduces a line break (new line)

This are just some of the format speciers provided by printf


The remaining will be addressed in more detail later on the course unit. . .

int age = 40 ;
double height = 1.72 ;
char gender = 'M ';
printf (" Profile : % d years , % f meters , gender = % c\n " , age , height , gender ) ;

Profile : 40 years , 1.720000 meters , gender = M

[email protected] (ESTiG) Imperative programming 6 / 14


Terminal Simple Output

Terminal Simple Output - printf


The format speciers are character sequences, started with %
followed by information about the type/format
They dene the location and format of each variable in the terminal
output
On the code snippet bellow we can identify:
%d - integer format (supports int)
%f - oating-point format (supports oat or double)
%c - character format (supports char)
\n - introduces a line break (new line)

This are just some of the format speciers provided by printf


The remaining will be addressed in more detail later on the course unit. . .

int age = 40 ;
double height = 1.72 ;
char gender = 'M ';
printf (" Profile : % d years , % f meters , gender = % c\n " , age , height , gender ) ;

Profile : 40 years , 1.720000 meters , gender = M

[email protected] (ESTiG) Imperative programming 6 / 14


Terminal Simple Output

Terminal Simple Output - printf


The format speciers are character sequences, started with %
followed by information about the type/format
They dene the location and format of each variable in the terminal
output
On the code snippet bellow we can identify:
%d - integer format (supports int)
%f - oating-point format (supports oat or double)
%c - character format (supports char)
\n - introduces a line break (new line)

This are just some of the format speciers provided by printf


The remaining will be addressed in more detail later on the course unit. . .

int age = 40 ;
double height = 1.72 ;
char gender = 'M ';
printf (" Profile : % d years , % f meters , gender = % c\n " , age , height , gender ) ;

Profile : 40 years , 1.720000 meters , gender = M

[email protected] (ESTiG) Imperative programming 6 / 14


Terminal Simple Output

Terminal Simple Output - printf


The format speciers are character sequences, started with %
followed by information about the type/format
They dene the location and format of each variable in the terminal
output
On the code snippet bellow we can identify:
%d - integer format (supports int)
%f - oating-point format (supports oat or double)
%c - character format (supports char)
\n - introduces a line break (new line)

This are just some of the format speciers provided by printf


The remaining will be addressed in more detail later on the course unit. . .

int age = 40 ;
double height = 1.72 ;
char gender = 'M ';
printf (" Profile : % d years , % f meters , gender = % c\n " , age , height , gender ) ;

Profile : 40 years , 1.720000 meters , gender = M

[email protected] (ESTiG) Imperative programming 6 / 14


Terminal Simple Output

Terminal Simple Output - printf


The format speciers are character sequences, started with %
followed by information about the type/format
They dene the location and format of each variable in the terminal
output
On the code snippet bellow we can identify:
%d - integer format (supports int)
%f - oating-point format (supports oat or double)
%c - character format (supports char)
\n - introduces a line break (new line)

This are just some of the format speciers provided by printf


The remaining will be addressed in more detail later on the course unit. . .

int age = 40 ;
double height = 1.72 ;
char gender = 'M ';
printf (" Profile : % d years , % f meters , gender = % c\n " , age , height , gender ) ;

Profile : 40 years , 1.720000 meters , gender = M

[email protected] (ESTiG) Imperative programming 6 / 14


Terminal Simple Output

Terminal Simple Output - printf


The format speciers are character sequences, started with %
followed by information about the type/format
They dene the location and format of each variable in the terminal
output
On the code snippet bellow we can identify:
%d - integer format (supports int)
%f - oating-point format (supports oat or double)
%c - character format (supports char)
\n - introduces a line break (new line)

This are just some of the format speciers provided by printf


The remaining will be addressed in more detail later on the course unit. . .

int age = 40 ;
double height = 1.72 ;
char gender = 'M ';
printf (" Profile : % d years , % f meters , gender = % c\n " , age , height , gender ) ;

Profile : 40 years , 1.720000 meters , gender = M

[email protected] (ESTiG) Imperative programming 6 / 14


Control Structures

Summary

1 Terminal Simple Output

2 Control Structures

3 Examples

[email protected] (ESTiG) Imperative programming 7 / 14


Control Structures

Control Structures
There are several phases on the implementation of a program, namely:
1 Finding the program's requirements Specication
2 Research and analysis of the possible solutions
3 Coding and debugging (development loop)
Phase 2 can consist of the program's representation in the form of a
ux diagram (≡ algorithm)
This ux diagram is later implemented (phase 3) by using C code
C language sequential programming paradigm
Provides a group of control structures, allowing the programmer to
control the ow of execution:
Selection structures - if, switch/case
Loop structures - while, do/while, for
Aiming to ease application design/algorithm, we introduce the basic
concepts behind select and loop structures
[email protected] (ESTiG) Imperative programming 8 / 14
Control Structures

Control Structures
There are several phases on the implementation of a program, namely:
1 Finding the program's requirements Specication
2 Research and analysis of the possible solutions
3 Coding and debugging (development loop)
Phase 2 can consist of the program's representation in the form of a
ux diagram (≡ algorithm)
This ux diagram is later implemented (phase 3) by using C code
C language sequential programming paradigm
Provides a group of control structures, allowing the programmer to
control the ow of execution:
Selection structures - if, switch/case
Loop structures - while, do/while, for
Aiming to ease application design/algorithm, we introduce the basic
concepts behind select and loop structures
[email protected] (ESTiG) Imperative programming 8 / 14
Control Structures

Control Structures
There are several phases on the implementation of a program, namely:
1 Finding the program's requirements Specication
2 Research and analysis of the possible solutions
3 Coding and debugging (development loop)
Phase 2 can consist of the program's representation in the form of a
ux diagram (≡ algorithm)
This ux diagram is later implemented (phase 3) by using C code
C language sequential programming paradigm
Provides a group of control structures, allowing the programmer to
control the ow of execution:
Selection structures - if, switch/case
Loop structures - while, do/while, for
Aiming to ease application design/algorithm, we introduce the basic
concepts behind select and loop structures
[email protected] (ESTiG) Imperative programming 8 / 14
Control Structures

Control Structures
There are several phases on the implementation of a program, namely:
1 Finding the program's requirements Specication
2 Research and analysis of the possible solutions
3 Coding and debugging (development loop)
Phase 2 can consist of the program's representation in the form of a
ux diagram (≡ algorithm)
This ux diagram is later implemented (phase 3) by using C code
C language sequential programming paradigm
Provides a group of control structures, allowing the programmer to
control the ow of execution:
Selection structures - if, switch/case
Loop structures - while, do/while, for
Aiming to ease application design/algorithm, we introduce the basic
concepts behind select and loop structures
[email protected] (ESTiG) Imperative programming 8 / 14
Control Structures

Control Structures
There are several phases on the implementation of a program, namely:
1 Finding the program's requirements Specication
2 Research and analysis of the possible solutions
3 Coding and debugging (development loop)
Phase 2 can consist of the program's representation in the form of a
ux diagram (≡ algorithm)
This ux diagram is later implemented (phase 3) by using C code
C language sequential programming paradigm
Provides a group of control structures, allowing the programmer to
control the ow of execution:
Selection structures - if, switch/case
Loop structures - while, do/while, for
Aiming to ease application design/algorithm, we introduce the basic
concepts behind select and loop structures
[email protected] (ESTiG) Imperative programming 8 / 14
Control Structures

Control Structures
There are several phases on the implementation of a program, namely:
1 Finding the program's requirements Specication
2 Research and analysis of the possible solutions
3 Coding and debugging (development loop)
Phase 2 can consist of the program's representation in the form of a
ux diagram (≡ algorithm)
This ux diagram is later implemented (phase 3) by using C code
C language sequential programming paradigm
Provides a group of control structures, allowing the programmer to
control the ow of execution:
Selection structures - if, switch/case
Loop structures - while, do/while, for
Aiming to ease application design/algorithm, we introduce the basic
concepts behind select and loop structures
[email protected] (ESTiG) Imperative programming 8 / 14
Control Structures

Control Structures
There are several phases on the implementation of a program, namely:
1 Finding the program's requirements Specication
2 Research and analysis of the possible solutions
3 Coding and debugging (development loop)
Phase 2 can consist of the program's representation in the form of a
ux diagram (≡ algorithm)
This ux diagram is later implemented (phase 3) by using C code
C language sequential programming paradigm
Provides a group of control structures, allowing the programmer to
control the ow of execution:
Selection structures - if, switch/case
Loop structures - while, do/while, for
Aiming to ease application design/algorithm, we introduce the basic
concepts behind select and loop structures
[email protected] (ESTiG) Imperative programming 8 / 14
Control Structures

Control Structures
There are several phases on the implementation of a program, namely:
1 Finding the program's requirements Specication
2 Research and analysis of the possible solutions
3 Coding and debugging (development loop)
Phase 2 can consist of the program's representation in the form of a
ux diagram (≡ algorithm)
This ux diagram is later implemented (phase 3) by using C code
C language sequential programming paradigm
Provides a group of control structures, allowing the programmer to
control the ow of execution:
Selection structures - if, switch/case
Loop structures - while, do/while, for
Aiming to ease application design/algorithm, we introduce the basic
concepts behind select and loop structures
[email protected] (ESTiG) Imperative programming 8 / 14
Control Structures

Control Structures
There are several phases on the implementation of a program, namely:
1 Finding the program's requirements Specication
2 Research and analysis of the possible solutions
3 Coding and debugging (development loop)
Phase 2 can consist of the program's representation in the form of a
ux diagram (≡ algorithm)
This ux diagram is later implemented (phase 3) by using C code
C language sequential programming paradigm
Provides a group of control structures, allowing the programmer to
control the ow of execution:
Selection structures - if, switch/case
Loop structures - while, do/while, for
Aiming to ease application design/algorithm, we introduce the basic
concepts behind select and loop structures
[email protected] (ESTiG) Imperative programming 8 / 14
Control Structures

Control Structures
There are several phases on the implementation of a program, namely:
1 Finding the program's requirements Specication
2 Research and analysis of the possible solutions
3 Coding and debugging (development loop)
Phase 2 can consist of the program's representation in the form of a
ux diagram (≡ algorithm)
This ux diagram is later implemented (phase 3) by using C code
C language sequential programming paradigm
Provides a group of control structures, allowing the programmer to
control the ow of execution:
Selection structures - if, switch/case
Loop structures - while, do/while, for
Aiming to ease application design/algorithm, we introduce the basic
concepts behind select and loop structures
[email protected] (ESTiG) Imperative programming 8 / 14
Control Structures

Control Structures
There are several phases on the implementation of a program, namely:
1 Finding the program's requirements Specication
2 Research and analysis of the possible solutions
3 Coding and debugging (development loop)
Phase 2 can consist of the program's representation in the form of a
ux diagram (≡ algorithm)
This ux diagram is later implemented (phase 3) by using C code
C language sequential programming paradigm
Provides a group of control structures, allowing the programmer to
control the ow of execution:
Selection structures - if, switch/case
Loop structures - while, do/while, for
Aiming to ease application design/algorithm, we introduce the basic
concepts behind select and loop structures
[email protected] (ESTiG) Imperative programming 8 / 14
Control Structures

Control Structures - simple/compound statements


In programming slang, statement can refer to:
simple statement - a single instruction (ends with ;):
double squareRoot = sqrt(25);
compound statement - block of related instructions (enclosed by {}):
{
int age ;
printf ( " What 's your age ? " ) ;
scanf ( " % d " , & age ) ;
printf ( " Your age is : % d \ n " , age ) ;
}

Compound statement scope concept (context)


More details later on the course. . .

Attention - variable declarations inside the scope are not available


outside

[email protected] (ESTiG) Imperative programming 9 / 14


Control Structures

Control Structures - simple/compound statements


In programming slang, statement can refer to:
simple statement - a single instruction (ends with ;):
double squareRoot = sqrt(25);
compound statement - block of related instructions (enclosed by {}):
{
int age ;
printf ( " What 's your age ? " ) ;
scanf ( " % d " , & age ) ;
printf ( " Your age is : % d \ n " , age ) ;
}

Compound statement scope concept (context)


More details later on the course. . .

Attention - variable declarations inside the scope are not available


outside

[email protected] (ESTiG) Imperative programming 9 / 14


Control Structures

Control Structures - simple/compound statements


In programming slang, statement can refer to:
simple statement - a single instruction (ends with ;):
double squareRoot = sqrt(25);
compound statement - block of related instructions (enclosed by {}):
{
int age ;
printf ( " What 's your age ? " ) ;
scanf ( " % d " , & age ) ;
printf ( " Your age is : % d \ n " , age ) ;
}

Compound statement scope concept (context)


More details later on the course. . .

Attention - variable declarations inside the scope are not available


outside

[email protected] (ESTiG) Imperative programming 9 / 14


Control Structures

Control Structures - simple/compound statements


In programming slang, statement can refer to:
simple statement - a single instruction (ends with ;):
double squareRoot = sqrt(25);
compound statement - block of related instructions (enclosed by {}):
{
int age ;
printf ( " What 's your age ? " ) ;
scanf ( " % d " , & age ) ;
printf ( " Your age is : % d \ n " , age ) ;
}

Compound statement scope concept (context)


More details later on the course. . .

Attention - variable declarations inside the scope are not available


outside

[email protected] (ESTiG) Imperative programming 9 / 14


Control Structures

Control Structures - simple/compound statements


In programming slang, statement can refer to:
simple statement - a single instruction (ends with ;):
double squareRoot = sqrt(25);
compound statement - block of related instructions (enclosed by {}):
{
int age ;
printf ( " What 's your age ? " ) ;
scanf ( " % d " , & age ) ;
printf ( " Your age is : % d \ n " , age ) ;
}
printf ( " Age : % d \ n " , age ) ; ← ERROR ! ! !
Compound statement scope concept (context)
More details later on the course. . .

Attention - variable declarations inside the scope are not available


outside

[email protected] (ESTiG) Imperative programming 9 / 14


Control Structures

Control Structures - selection structures


Allow the inclusion of an instruction block in the execution, only
when a condition is met if
A variation allows the inclusion of another instruction block when
the condition is not met if/else
Code C Algorithm
if ( condition ) {
IF condition THEN
instructions
execute instructions
}

[email protected] (ESTiG) Imperative programming 10 / 14


Control Structures

Control Structures - selection structures


Allow the inclusion of an instruction block in the execution, only
when a condition is met if
A variation allows the inclusion of another instruction block when
the condition is not met if/else
Code C Algorithm
if ( condition ) {
IF condition THEN
instructions
execute instructions
}
if ( condition ) {
IF condition THEN
instructions
execute instructions
} else {
ELSE
other instructions
execute other instructions
}

[email protected] (ESTiG) Imperative programming 10 / 14


Control Structures

Control Structures - loop structures

Allow looping an instruction block iteration


Repeat an instruction block (scope) while a condition is met →
while
A variation allows postponing the condition verication to the end of
each iteration do/while

[email protected] (ESTiG) Imperative programming 11 / 14


Control Structures

Control Structures - loop structures

Allow looping an instruction block iteration


Repeat an instruction block (scope) while a condition is met →
while
A variation allows postponing the condition verication to the end of
each iteration do/while
Code C Algorithm
while ( condition ) {
WHILE condition DO
instructions
execute instructions
}

[email protected] (ESTiG) Imperative programming 11 / 14


Control Structures

Control Structures - loop structures

Allow looping an instruction block iteration


Repeat an instruction block (scope) while a condition is met →
while
A variation allows postponing the condition verication to the end of
each iteration do/while
Code C Algorithm
while ( condition ) {
WHILE condition DO
instructions
execute instructions
}
do { DO
instructions execute instructions
} while ( condition ) ; WHILE condition

[email protected] (ESTiG) Imperative programming 11 / 14


Examples

Summary

1 Terminal Simple Output

2 Control Structures

3 Examples

[email protected] (ESTiG) Imperative programming 12 / 14


Examples

Examples
Age verication:
# include < stdio .h >

int main () {
int age = 40 ;
if ( age >= 18) {
printf (" You can vote !\ n") ;
}
}

Sum all the even numbers between 0 and 100:


# include < stdio .h >

int main () {
int number = 0, sum = 0 ;
while ( number < 100) {
number = number + 2;
sum = sum + number ;
}
printf (" Even sum = %d\ n" , sum );
}

[email protected] (ESTiG) Imperative programming 13 / 14


Examples

Examples
Age verication:
# include < stdio .h >

int main () {
int age = 40 ;
if ( age >= 18) {
printf (" You can vote !\ n") ;
}
}

Sum all the even numbers between 0 and 100:


# include < stdio .h >

int main () {
int number = 0, sum = 0 ;
while ( number < 100) {
number = number + 2;
sum = sum + number ;
}
printf (" Even sum = %d\ n" , sum );
}

[email protected] (ESTiG) Imperative programming 13 / 14


Examples

Examples

The previous example, using the do/while control structure:


# include < stdio .h >

int main () {
int number = 0, sum = 0 ;
do {
number = number + 2;
sum = sum + number ;
} while ( number < 100) ;
printf (" Even sum = %d\ n" , sum );
}

Looking at this simple example, while and do/while have the same
exact eect
Along the way, we will learn that more complex scenarios can be more
appropriate to while or do/while

[email protected] (ESTiG) Imperative programming 14 / 14


Examples

Examples

The previous example, using the do/while control structure:


# include < stdio .h >

int main () {
int number = 0, sum = 0 ;
do {
number = number + 2;
sum = sum + number ;
} while ( number < 100) ;
printf (" Even sum = %d\ n" , sum );
}

Looking at this simple example, while and do/while have the same
exact eect
Along the way, we will learn that more complex scenarios can be more
appropriate to while or do/while

[email protected] (ESTiG) Imperative programming 14 / 14


Examples

Examples

The previous example, using the do/while control structure:


# include < stdio .h >

int main () {
int number = 0, sum = 0 ;
do {
number = number + 2;
sum = sum + number ;
} while ( number < 100) ;
printf (" Even sum = %d\ n" , sum );
}

Looking at this simple example, while and do/while have the same
exact eect
Along the way, we will learn that more complex scenarios can be more
appropriate to while or do/while

[email protected] (ESTiG) Imperative programming 14 / 14

You might also like