












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The syntax and usage of different control structures in programming, including if, if-else, while, for, and do-while loops. It also provides examples and exercises to illustrate their functionality.
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!













5/17/ 5/17/
syntax: syntax:
if(boolean if(boolean
expression) expression)
{ {
body of the if statement body of the if statement
} }
When the statement is executed, the When the statement is executed, the
boolean boolean
expression is evaluated. If it is true the body is expression is evaluated. If it is true the body is executed else it is skipped over. If there are no executed else it is skipped over. If there are no { }, the if applies to the next single statement. { }, the if applies to the next single statement.
5/17/ 5/17/
if(boolean if(boolean
expression) expression)
{ {
body to do if the expression is true body to do if the expression is true
} } else else { {
body to do if the expression is false body to do if the expression is false
} } One and only one of these two blocks will be One and only one of these two blocks will be
executed. executed.
5/17/ 5/17/
Example Example
if(G if(G
ch ch
else else
if(G if(G
ch ch
else else
ch ch
5/17/ 5/17/
syntax: syntax: while(boolean while(boolean
expression) expression)
{ {
body of loop to be iterated body of loop to be iterated
} } This is a pretest loop. The This is a pretest loop. The
boolean boolean
expression is expression is
evaluated. If true execute the loop. Check the evaluated. If true execute the loop. Check the expression after each iteration of the loop and expression after each iteration of the loop and continue to iterate as long as the expression is continue to iterate as long as the expression is true. true.
5/17/ 5/17/
What does the following do? What does the following do?
sum = 0; sum = 0;
i = 1; i = 1;
while(i while(i
sum = sum =
sum sum
i = i + 1; i = i + 1;
10 10
5/17/ 5/17/
syntax: syntax: do do { {
body of loop to be iterated body of loop to be iterated
} }
while(boolean while(boolean
expression); expression);
The condition is tested at the end of the loop rather The condition is tested at the end of the loop rather
than at the beginning. What is the one thing you than at the beginning. What is the one thing you can say about a do can say about a do
while that you may not while that you may not
always be able to say about the while? always be able to say about the while?
11 11
5/17/ 5/17/
Write the example on slide 8 as an Write the example on slide 8 as an
equivalent do equivalent do
while structure. while structure.
Can this always be done? Can this always be done?
Is it always a matter of just rearranging the Is it always a matter of just rearranging the
structure and moving the conditional to the structure and moving the conditional to the
end? If not, give an example showing that end? If not, give an example showing that
other changes must be made. other changes must be made.
13 13
5/17/ 5/17/
We We
ll draw a picture on how to do this. ll draw a picture on how to do this.
Then we will look at an example: Then we will look at an example:
ExampleRepeat.cs ExampleRepeat.cs
14 14
5/17/ 5/17/
syntax: syntax:
for(initializer for(initializer
bool bool
expression; updater) expression; updater)
body of loop body of loop
16 16
5/17/ 5/17/
The initializing statement is done. Once it is The initializing statement is done. Once it is
done, it is not visited done, it is not visited
again.Then again.Then
it it
becomes similar to a while as shown by becomes similar to a while as shown by
the following pseudo code. the following pseudo code.
while(bool while(bool
expression is true){ expression is true){
do the body of the loop do the body of the loop
do the update do the update
17 17
5/17/ 5/17/
The for structure is normally used when you The for structure is normally used when you
want to execute a loop a predetermined want to execute a loop a predetermined
number of times, often indexed with some number of times, often indexed with some
integer counter. integer counter.
We will look at a number of examples. We will look at a number of examples.
19 19
5/17/ 5/17/
Consider the following C# statement code: Consider the following C# statement code:
int int
i = 1; i = 1;
for( ; ; ) for( ; ; )
Console.Write(i Console.Write(i
i = i+1; i = i+1;
Will it compile? What happens? Try it. Will it compile? What happens? Try it.
20 20
5/17/ 5/17/
After the i = i+1; statement, add the After the i = i+1; statement, add the
following: following:
if(i if(i
break; break;
Recompile and run it. Can you guess what Recompile and run it. Can you guess what
the break command does? In what other the break command does? In what other
structures might you use it? structures might you use it?