1 / 14

In the Name of Allah The Most Merciful The Most Compassionate LOOPS vustudents.ning

In the Name of Allah The Most Merciful The Most Compassionate LOOPS http://vustudents.ning.com. LOOP. A statement or a set of statement that is executed repeatedly is called a loop.There are three kinds of Loops in C++ for loop while loop do-while loop. for Loop.

deacon-bean
Télécharger la présentation

In the Name of Allah The Most Merciful The Most Compassionate LOOPS vustudents.ning

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. In the Name of Allah The Most Merciful The Most CompassionateLOOPShttp://vustudents.ning.com

  2. LOOP • A statement or a set of statement that is executed repeatedly is called a loop.There are three kinds of Loops in C++ for loop while loop do-while loop

  3. for Loop • The for loop statement is used to execute a set of statements repeatedly for a fixed number of times. It is also know as “counter loop”. It has the following parts • Initialization • Condition/Test expression • Increment /Decrement • Body of the Loop The syntax is: 1) for ( initialization; condition; inc/dec ) { Body of the loop }

  4. Initialization: The value of variable(s) of assigned when control enters into the loop first time.It is optional.if it is to be omitted then a semicolon is used in its place. • Condition: The body of the loop executes as long as this condition remains true. • Increment/Decrement: the value of the variable(s) is incremented or decremented. For more than one variable they are separated by commas. This part is executed after executing the body of the loop..Its use is also optional. If it is not used then the control variable must be inc/dec inside the body of the loop. • Body of the loop: If more than 1 statement are to be executed these are enclosed in braces.

  5. No Semi colon • for (int a=2;a<=16; a=a+2) cout<<“\t”<<a; Output: 2 4 6 8 10 12 14 16 • int x=2; for (;x<=16; ) { cout<<“\t”<<x; x=x+2; } Output: 2 4 6 8 10 12 14 16

  6. FLOWCHART Initialization FALSE condition Exit TRUE Body of the Loop Inc/Dec

  7. while Loop • It is a conditional loop. It is used to execute a statement or a set of statements as long as the given condition remains true. • The Syntax is while(condition) { statement(s); } If the loop never ends (the condition is never false) ,it is said to be an infinite loop

  8. FLOWCHART FALSE condition Exit TRUE Body of the Loop

  9. int n=2; while(n<10) { cout<<“\t”<<n; n=n+2; } Output: 2 4 6 8 • NOTE: while(0) is always a false loop while(2) is an infinite loop

  10. do-while Loop • It is also conditional loop. It is similar to the while loop but the condition is tested after executing the statements of the loop • The Syntax is do { statement(s); } while (condition); It is useful when the body of the loop is to be executed at least once.

  11. FLOWCHART Body of the Loop Exit condition TRUE FALSE

  12. # include<iostream.h> void main() { int x=2; do { cout<<“\t”<<x; x=x+2; }while(x<10); } • Output?? 2 4 6 8 Semicolon

  13. The continue statement • It shifts the control back to the beginning of the loop.It skips the remaining statements within the loop body. Start of loop continue condition

  14. //Factorial Program # include<iostream.h> void main() { long int fact,n,c; cout<<“\nEnter a number: ”; cin>>n; fact =1; while(n>=1) { fact = fact*n ; n-- ; } cout<<“\nFactorial= ”<<fact; } http://vustudents.ning.com

More Related