Classof1 logo
Fax: 1- 425- 458- 9358 | Toll free: 1- 877- 252 - 7763
Bookmark and Share
Forgot Password? Click Here
Register  |  Account

Need help with Computer Science assignment?

Get customized homework help now!

Looping Statements

Looping statements are used to execute a group of statements repeatedly until some condition is satisfied. The looping statements are

  • while statement
  • do…… while statement
  • for statement

a)while statement – Entry control statement:

This is a simple looping statement. The general form is

While (test condition)

{

body of the loop;

 }

When this statement is executed, the computer first evaluates the test condition. If the value is false, the control is transferred to next statement. If the value is true then the body of the loop is executed repeatedly until the test condition becomes false. When the test condition becomes false the control is transferred to next statement.

Rules:

If the body of the loop contains more than one statement the {} brackets are must.

The test condition should be any relational or logical expression enclosed within brackets.

b) do……while statement:

Syntax:

Do

{

body of the loop;

}

while(test condition);

next statement;

When this statement is executed the body of the loop is executed first. Then the test condition is evaluated. If the value is false, the control is transferred to the next statement. If the value is true the body of the loop is executed repeatedly until the test condition becomes false. When the test condition becomes false the control is transferred to the next statement.

Rules:

The test  condition should be any relational or logical expression enclosed within parentheses.

Brackets are must if the body of the loop contains more than one statement.

The while statement should be immediately after the body of the loop.

Difference between while and do….while:

while is an entry control statement. So if the test condition is false the body of the loop will not be executed.

Do…while is an exit control statement, the body of the loop will be executed atleast once irrespective of the test condition value

c) for statement:

for statement is used to execute a statement or a group of statements repeatedly for a known number of times.

General form:

for (i = initial condition; test condition; increment or decrement)

{

body of the loop;

}

next statement;

where i – valid variable called control variable.

Initial condition is used to set initial values to control variable.

Test condition is used to check the control variable. According to this the  loop is executed or not.

Increment or decrement is used to change the value of the control variable.

When the FOR statement is executes the value of the control variable is initialized and tested with the test condition. If the value of the test condition is true the body of the loop will be executed and the control is transferred to the FOR statement. Then the value of the control variable is incremented or decremented .When the test condition becomes false the control is transferred to the next statement. The body of the loop is executed repeatedly as long as the test value is true.

Nested for loop:

If one for loop is enclosed within another for loop then such loops are known as nested for loops. There is no limit on the number of for loops that can be nested.

Computer Science Homework Help
Name* :
Email* :
Country* :
Phone* :
Subject* :
Upload Homework :
Upload another homework (upto 5 uploads max.)
Due Date
Time
AM/PM
Timezone
Instructions
(Type Security Code - case sensitive)