Sie sind auf Seite 1von 9

Decision Making & Looping

Decision Making & Looping


Looping:
A sequence of statements are executed until some condition for
termination of the loop satisfied.

Program Loop Control Statement: Test the condition & directs the execution
consists of Body of the loop: Contents to execute

• Entry controlled loop


Loop control structure
• Exit controlled loop
Steps in Looping

• Initialization of control variable


• Execution of statement
• Test for condition
• Incrementing / Updating condition variable

Constructs for Loop


1. While
2. Do
3. For
Syntax
While Statement while (test-condition)
• Simplest Looping structure {
• ‘while’ is Entry controlled Loop Body of the loop
}
• Test condition, before the body of loop executed

Program to find the sum of first 10 numbers


n
? Program to evaluate the equation Y= X with while loop
do Statement (do …while)

• Do…while is an exit controlled loop


• Execute the body of loop before test the condition
• If the test condition is true, program continues to evaluate loop
• If the test condition is false, loop will terminated.

Syntax

do
{
Body of the loop
}
while (test-condition);
? Program to find the sum of numbers from 40 to 50 With do… while loop
Syntax
FOR Statement for (initialization ; test-condition ; increment)

• for is an entry-controlled loop {


Body of the loop
}

? Program to print numbers from 1 to 10 using for statement

Das könnte Ihnen auch gefallen