Control Structures in Programming: If, While, For, and Do-While Loops, Slides of Computer Science

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

2012/2013

Uploaded on 03/21/2013

dheeraj
dheeraj 🇮🇳

5

(4)

101 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Control Structures
Control Structures
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Control Structures in Programming: If, While, For, and Do-While Loops and more Slides Computer Science in PDF only on Docsity!

Control Structures

Control Structures

5/17/ 5/17/

if

if

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

if

else

else

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/

if

if

else

else

if

if

else etc.

else etc.

Example Example

if(G if(G

ch ch

A

A

else else

if(G if(G

ch ch

B

B

else else

ch ch

C

C

5/17/ 5/17/

while

while

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;

i = i + 1; i = i + 1;

10 10

5/17/ 5/17/

There is a post

There is a post

test version of

test version of

while.

while.

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/

Problem:

Problem:

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/

for

for

syntax: syntax:

for(initializer for(initializer

bool bool

expression; updater) expression; updater)

body of loop body of loop

16 16

5/17/ 5/17/

for flow of control

for flow of control

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/

Primary Use of a for

Primary Use of a for

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/

Question

Question

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?