5 loops (1)

Download 5 Loops (1)

Post on 11-Dec-2015

217 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

not mine

TRANSCRIPT

  • Programming Fundamentals BSCS Semester1Course Instructor: M. NadeemContact: Muhammad.nadeem@cs.uol.edu.pk

  • **Loops & Its LogicWhat is a Loop?An iterative statement is called loop. A type of control structure that repeats a statement or set of statements is known as looping structures. It is also known as iterative or repetitive structure. There are two elements of the loop: the body of loop which is to be executed number of times and a loop condition which terminates the loop when a particular condition is met.

    Repetition

    A single statementA set of statements

  • **Types of LoopTypes of LoopCounter loop (for loop)Conditional loops (while, do while)

  • **For Loop OperationInitialization ExpressionBody of LoopIncrement ExpressionExitTest ExpressionTrueFalse

  • **For Loop Structurefor ( j=0 ; j < 5 ; j++ )statement;Single Statement Loop Bodykeyword

  • **For Loop Structurefor ( j=0 ; j < 5 ; j++ ){ statement; statement; statement;}Initialization ExpressionTest ExpressionIncrement ExpressionMultiple Statement Loop Bodykeyword

  • **For Loop// demonstrate simple for loop# include #includevoid main() {int j;for (j=0 ; j < 5 ; j++)printf(%d , j * j;getch();}Output:014916

  • **For Loop VariationsMultiple initialization and increment Expressions

    for (x=0 , j=0 ; j < 5 ; j++ , x++)

    Increment or Decrement

    for ( j=5 ; j > 0 ; j-- )for ( j=0 ; j

  • The Infinite loop

    A loop that does not terminate is called Infinite loop.A loop who's condition always remain true is called infinite loop.e.g.for(int i=0;i>0;i++)printf(%d,i);**

  • **While Loopwhile ( test expression ){Statement;Statement;Statement;}

    while (test expression )statement;

    Single Statement Loop BodyMultiple Statement Loop BodyWhile Operation

  • **The body of the loop is executed until the condition becomes false. Here is a program which illustrates the working of the while loop. void main () { int c=0; int i=0; char str=y; while(str==y) { cout
  • // to find the average of as many numbers as user wants with while loop. void main(){int n,sum,count;while(scanf(%d,&n)){If(n==0)continue;sum=sum+n //sum+=n;count++;}Printf(%d,(sum/count));}**

  • **Do While LoopDo{Statement;Statement;Statement;}while ( test expression );

    Dostatement;while (test expression );

    Single Statement Loop BodyMultiple Statement Loop BodyMultiple Statement Loop BodyDo While OperationMultiple Statement Loop Body

  • The body of the loop is executed until the condition becomes false. Here is a program which illustrates the working of the do while loop. void main () { int c=0; int i=0; char str=y; do { cout
  • **// to find the average of as many numbers as user wants with do while loop. void main(){int n,sum,count;do{If(n==0)continue;sum=sum+n //sum+=n;count++;} while(scanf(%d,&n));printf(%d,(sum/count));}

  • **When to use Which Loop?Advance knowledgeFor LoopNo Prior Knowledge While Loop Do While(executes at least once)

  • **Break & Continue StatementTo exit a loop you can use the break statement at any time. This can be very useful if you want to stop running a loop because a condition has been met other than the loop end condition. With continue; it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented).EXAMPLE

    int main() { int j, total;for (int i=0; i>j;if ( j == 10) continue; total = i + j; cout

  • **Continue statementWith continue; it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented). EXAMPLE

    int main() { int i;i = 0; while ( i < 20 ) { i++; continue;printf(hey wait! you are skipping me\n); } return 0; }

  • Nested LoopA loop within an other loop is called nested loop. e.g.for(int i=1;i
  • **Nested Loopfor(int i=5;i>=1;i--){ for(int j=i;j>=1;j--){printf(%d,j);}printf(\n);}Output543214321321211

  • **Nested Loopfor(int i=5;i>=1;i--){ for(int j=1;j
  • **Nested Loopfor(int i=5;i>=1;i--){ for(int j=1;j
  • **Nested Loopint i=5;while(i>=1){ int j=1;while(j
  • What makes a bad program?Repeating trial and error without understanding the problemWriting Code without detailed analysis and design (Abstraction, Algo)Writing tricky and dirty programs

  • Programms:Write a program using for, while and do while loops to:Display numbers from 0 to 100Display numbers from 100 to 0Display even numbers from 0 to 100Display odd numbers from 0 to 100Display even numbers from 100 to 0Display odd numbers from 100 to 0Display Square of numbers from 0 to 100Display Cube of numbers from 0 to 100Display Square of numbers from 100 to 0Display Cube of numbers from 100 to 0Display Square of numbers from 40 to 100Display Cube of numbers from 50 to 100Display Square of numbers from 500 to 1000Display Cube of numbers from 1000 to 1500Display Average of even numbers from 1000 to 1200Display Average of odd numbers from 1200 to 1000

    **

  • Programs:Write a program using for, while and do while and nested loops to display the following outputs. You are also required to submit the dry runs of all these programs on paper.

    **12345678910123456789123456781234567123456123451234123121===========**************** ******** **** ** *===========********************************11212312341234512345612345671234567812345678912345678910

  • **135791357913571357135135131 * *** ***** ******* ***** *** *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&002024024602468024681002468101202468101214Programs:Write a program using for, while and do while and nested loops to display the following outputs. You are also required to submit the dry runs of all these programs on paper.

    *

Recommended

View more >