1 / 16

COMPUTER SCIENCE

COMPUTER SCIENCE. PROJECT WORK. PREPARED BY-. ANUJ AND ANKIT CLASS XI-SCIENCE ROLL NO-02 AND 38. FLOW OF CONTROL. LOOP PROGRAMMING. TOPIC. FOR LOOP WHILE LOOP DO-WHILE LOOP. THE FOR LOOP.

claire
Download Presentation

COMPUTER SCIENCE

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. COMPUTER SCIENCE PROJECT WORK

  2. PREPARED BY- ANUJ AND ANKIT CLASS XI-SCIENCE ROLL NO-02 AND 38

  3. FLOW OF CONTROL LOOP PROGRAMMING

  4. TOPIC FOR LOOP WHILE LOOP DO-WHILE LOOP

  5. THE FOR LOOP The for loop is the easiest to understand of the C++ loops. All its loop control elements are gathered in one place (on the top of the loop), while in the other loop construction of C++,they (top control elements) are scattered about the program. The general form (syntax) of the for loop statement is

  6. for (initialization expression(s); test expression;update expression(s) )body- of –the- loop; The following examples illustrates the use of for statement

  7. Program using loop to print numbers from 1 to10 #include<iostream.h> int main() { int i; //declaring variable i for (i=1;i<=10;i++) cout<<i; return 0; } Output 12345678910

  8. The following lines explain the working of the above given for loop cout<<i; Body of loop

  9. Firstly initialization expression is executed .i.e i=1 which gives the first value 1 to variable i. • Then the test expression is evaluated. i.e. , i<=10 which results into true i.e.1 . • Since the test expression is true , the body of loop i.e. cout<<i is executed which prints the current value of i . • After executing the loop body the update expression i.e. i++ is executed which increments the value of i. • After the update expression is executed the test expression is again evaluated. If it is true the sequence is repeated from step no. 3,otherwise the loop terminates Following figure outlines the working of a For loop:

  10. Initialization expression Test Expression false exit true Body of loop Update expression

  11. The while Loop • The second loop available in C++ is the While loop. The while loop is an entry-controlled loop . The syntax of a while loop is While (expression) loop-body

  12. Program to calculate the factorial of an integer #include<iostream.h> int main() { Unsigned long i, num, fact=1; cout<<“Enter Integer:”; cin>>num; //num gets its 1st value. while(num) { fact=fact*num ; --num; } Cout” the factorial of”<<i<<“is”<<fact<<“\n”; getch() }

  13. Output of the above program. Enter Integer :10 The factorial of 10 is 3628800

  14. The do-while loop Unlike the for and while loops, the do-while is an exit –controlled loop i.e., it evaluates its test expression at the bottom of the loop after executing its loop-body statements. This means that a do-while loop always executes at least once. In the other two loops for and while, the test -expression is evaluated at the beginning of the loop i.e., before the executing the loop –body. If the test -expressions evaluates to false the first time itself,the loop is never executed .But in some situation , it is wanted that the loop-body is executed at least once, no matter what the intial state of the test-expression is .In such cases, the do-while loop is the obvious choice . The syntax of the do-while loop is

  15. Do { Statement; }while (test-expression); The braces { } are not necessary when the loop body contains single statement.The following do-while loop prints all upper-case letters:

  16. char ch = ‘A’; do { cout<<“\n”<<ch; ch++ }while (ch<= ‘Z’ ); The above code prints characters from ‘A’ onwards until the condition ch<=‘Z’ becomes false.

More Related