Repetition and Iteration Structure Module 4 Repetition and

  • Slides: 33
Download presentation
Repetition and Iteration Structure • Module 4 • Repetition and Iteration Structure: Repeat Until

Repetition and Iteration Structure • Module 4 • Repetition and Iteration Structure: Repeat Until loop, The While loop, the for loop, and Nested loops Illustrating examples - Sum and average of N numbers, Sum of squares of first N positive integers • Number series: Different Number series, solving problems using Number sequence and Iterations – Print first N positive integers, Sum of squares of first N positive integers without any loop structure, Fibonacci sequence, Check the number is prime or not, Reverse a no, pattern printing Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • Loop (Repetition) • The loop allows a statement or

Repetition and Iteration Structure • Loop (Repetition) • The loop allows a statement or a sequence of statements to be repeatedly executed based on some condition • A trip around the loop is known as iteration • The Repetition structure can be implemented using • Repeat Until Loop • The While Loop • The For Loop Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • • The Repeat Until loop. The pseudo-code syntax

Repetition and Iteration Structure • • • The Repeat Until loop. The pseudo-code syntax REPEAT A statement or block of statements UNTIL a true condition Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • • • The Repeat Until loop. Example :

Repetition and Iteration Structure • • • • The Repeat Until loop. Example : A program print first N numbers. N: The limit I: loop Counter variable First read the Limit , N using parallelogram Initialized I=1, I=1 using Rectangular Box Display “Enter the Limit” Limit , using Parallelogram Read N, parallelogram Display I , with parallelogram Compute I=I+1, I=I+1 using rectangular box Check condition I<=N, I<=N if true, go to the Display I statement Otherwise end processing Condition can check using diagonal box Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • The Repeat Until loop. • Example : A program

Repetition and Iteration Structure • The Repeat Until loop. • Example : A program print first N numbers. • The pseudo-code Begin Use variables: N, I of the type integer Display ”Enter the limit” Read N Set I =1 REPEAT DISPLAY I I=I+1 UNTIL I<=N End Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • The WHILE loop • While loop tests for condition

Repetition and Iteration Structure • The WHILE loop • While loop tests for condition at the beginning of the loop. • In this case no action is performed at all if the condition false. • The syntax is • WHILE (a condition is true) • A statement or block of statements • ENDWHILE Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • • The WHILE loop Example : A program

Repetition and Iteration Structure • • • The WHILE loop Example : A program print first N numbers. N: The limit I: loop Counter variable First read the Limit , N using parallelogram Initialized I=1, I=1 using Rectangular Box Display “Enter the Limit” Limit , using Parallelogram Read N, N parallelogram Check condition I<=N, I<=N using diagonal box if true Display I , with parallelogram and Compute I=I+1, I=I+1 using rectangular box If condition false, end processing Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • • • The WHILE loop Example : A

Repetition and Iteration Structure • • • • The WHILE loop Example : A program print first N numbers. The pseudo-code Begin Use variables: N, I of the type integer Display ”Enter the limit” Read N Set I =1 WHILE ( I<=N) DISPLAY I I=I+1 END WHILE End Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • The FOR Loop The third type of iteration

Repetition and Iteration Structure • • The FOR Loop The third type of iteration is a for loop. It uses an initialization of the variable as a starting point, a stop condition depending on the value of the variable. The variable is incremented on each iteration until it reaches the required value. The pseudo-code syntax : FOR (starting state, stopping condition, increment) Statements ENDFOR Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • • The FOR Loop Example : Sum of

Repetition and Iteration Structure • • • The FOR Loop Example : Sum of first N positive integers N: The limit I: loop Counter variable SUM : Sum of given numbers Initialized I=1, I=1 SUM=0 using Rectangular Box Read the Limit , N, Display “Enter the Limit” Limit , using Parallelogram Read N, N parallelogram Check condition I<=N, I<=N using diagonal box If true, compute SUM=SUM+NUM and I=I+1, I=I+1 using rectangular box If condition false, End Loop Pint SUM, using parallelogram and end processing Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • • The FOR Loop Example : Sum of

Repetition and Iteration Structure • • • The FOR Loop Example : Sum of first N positive integers of numbers. pseudo-code Begin Use variables: N, I , SUM=0, of the type integer DISPLAY “Enter limit” ACCEPT N FOR (I = 1, I <= N, I + 1) SUM = SUM+ I ENDFOR DISPLAY “The sum is “, SUM End Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • The Repeat Until loop. Sum of first N

Repetition and Iteration Structure • • The Repeat Until loop. Sum of first N positive integers of numbers. Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • • The FOR Loop Example : Sum of

Repetition and Iteration Structure • • • The FOR Loop Example : Sum of squares of first N positive integers N: The limit I: loop Counter variable SUM : Sum of given numbers Initialized I=1, I=1 SUM=0 using Rectangular Box Read the Limit , N, Display “Enter the Limit” Limit , using Parallelogram Read N, N parallelogram Check condition I<=N, I<=N using diagonal box If true, compute SUM=SUM+I*Iand I=I+1, SUM=SUM+I*I I=I+1 using rectangular box If condition false, End Loop Pint SUM, using parallelogram and end processing Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • • The FOR Loop Example : Sum of

Repetition and Iteration Structure • • • The FOR Loop Example : Sum of squares of first N positive integers of numbers. pseudo-code Begin Use variables: N, I , SUM=0, of the type integer DISPLAY “Enter limit” ACCEPT N FOR (I = 1, I <= N, I + 1) SUM = SUM+ I*I ENDFOR DISPLAY “The sum is “, SUM End Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • The Repeat Until loop. Sum of squares of

Repetition and Iteration Structure • • The Repeat Until loop. Sum of squares of first N positive integers of numbers. Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • • • The FOR Loop Example : Program

Repetition and Iteration Structure • • • • The FOR Loop Example : Program to calculate the sum and average of a series of numbers N: The limit I: loop Counter variable NUM : The numbers to enter each time SUM : Sum of given numbers AVG : Average of given numbers First read the Limit , N using parallelogram Initialized I=1, I=1 SUM=0 using Rectangular Box Display “Enter the Limit” Limit , using Parallelogram Read N, N parallelogram Check condition I<=N, I<=N using diagonal box If true Display “Enter Number” , using Parallelogram & read NUM, with Parallelogram Compute SUM=SUM+NUM and I=I+1, I=I+1 using rectangular box If condition false, End forloop Compute AVG=SUM/N using Parallelogram, Pint SUM , AVG and end processing Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • • The FOR Loop Example : Program to

Repetition and Iteration Structure • • • The FOR Loop Example : Program to calculate the sum and average of a series of numbers. pseudo-code Begin • ACCEPT NUM Use variables: N, I of the type integer SUM=0, NUM, AVG of the type real • SUM = SUM+ NUM • ENDFOR DISPLAY “Enter limit” • AVG = SUM/ N ACCEPT N • DISPLAY “The sum is “, SUM FOR (I = 1, I <= N, I + 1) • DISPLAY “Average is “, AVG DISPLAY “Input the number ” • End Ajith G. S: poposir. orgfree. com

Repetition and Iteration Structure • • The Repeat Until loop. Program to calculate the

Repetition and Iteration Structure • • The Repeat Until loop. Program to calculate the sum and average of a series of numbers Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find Even number between 1 to N • • •

Algorithm & Flowchart to find Even number between 1 to N • • • Without loop structure N: The limit I: loop Counter variable Initialized I=1 using Rectangular Box First read the Limit , N using parallelogram Display “Enter the Limit” Limit , using Parallelogram Read N, N parallelogram Check condition I>N, I>N using diagonal box If true then end processing If false, Check I % 2 =0 , using diagonal box if true, then print I using parallelogram Compute I=I+1, I=I+1 using rectangular box GOTO the step check condition I>N Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find Even number between 1 to N • • •

Algorithm & Flowchart to find Even number between 1 to N • • • • Without loop structure Algorithm Step-1 : Start Step-2 : I = 1 Step-3 : Read N Step-4 : IF (I > N ) THEN GO TO Step-8 ENDIF Step-5 : IF ( (I % 2) =0) THEN Display I ENDIF Step-6 : I = I + 1 Step-7 : GO TO Step-4 Step-8 : Stop Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find Even number between 1 to N • Without loop

Algorithm & Flowchart to find Even number between 1 to N • Without loop structure Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find odd number between 1 to N • • •

Algorithm & Flowchart to find odd number between 1 to N • • • Without loop structure N: The limit I: loop Counter variable Initialized I=1 using Rectangular Box First read the Limit , N using parallelogram Display “Enter the Limit” Limit , using Parallelogram Read N, N parallelogram Check condition I>N, I>N using diagonal box If true then end processing If false, Check I % 2 =1 , using diagonal box if true, then print I using parallelogram Compute I=I+1, I=I+1 using rectangular box GOTO the step check condition I>N Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find odd number between 1 to N • • •

Algorithm & Flowchart to find odd number between 1 to N • • • • Without loop structure Algorithm Step-1 : Start Step-2 : I = 1 Step-3 : Read N Step-4 : IF (I > N ) THEN GO TO Step-8 ENDIF Step-5 : IF ( (I % 2) =1) THEN Display I ENDIF Step-6 : I = I + 1 Step-7 : GO TO Step 4 Step-8 : Stop Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find odd number between 1 to N • Without loop

Algorithm & Flowchart to find odd number between 1 to N • Without loop structure Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find sum of series 1+3+5+…. . +N • • •

Algorithm & Flowchart to find sum of series 1+3+5+…. . +N • • • • Without loop structure N: The limit I: loop Counter variable SUM : Sum of given numbers Initialized I=1 & SUM=0 using Rectangular Box First read the Limit , N using parallelogram Display “Enter the Limit” Limit , using Parallelogram Read N, N parallelogram Check condition I>N, I>N using diagonal box If true then print SUM & end processing If false, Check I % 2 =1 , using diagonal box if true, then Compute SUM=SUM+I using rectangular box Compute I=I+1, I=I+1 using rectangular box GOTO the step check condition I>N Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find sum of series 1+3+5+…. . +N • • •

Algorithm & Flowchart to find sum of series 1+3+5+…. . +N • • • • Without loop structure Algorithm Step-1 : Start Step-2 : I = 1 & SUM=0 Step-3 : Read N Step-4 : IF (I > N ) THEN GO TO Step-8 ENDIF Step-5 : IF ( (I % 2) =1) THEN SUM=SUM+I ENDIF Step-6 : I = I + 1 Step-7 : GO TO Step-4 Step-8 : Display SUM Step-9 : Stop Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find sum of series 1+3+5+…. . +N • Without loop

Algorithm & Flowchart to find sum of series 1+3+5+…. . +N • Without loop structure Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find sum of series 1 + X 2 +X 3

Algorithm & Flowchart to find sum of series 1 + X 2 +X 3 …. XN • • • Without loop structure N: The limit First find sum=X + X 2 +X 3 …. XN X: x value Then add 1 to sum I: loop Counter variable SUM : Sum of given numbers Initialized I=1 & SUM=1 using Rectangular Box First read the Limit , N & X using parallelogram Display “Enter the Limit & Value of X” X , using Parallelogram Read N, N X parallelogram Check condition I>N, I>N using diagonal box If true then print SUM & end processing If false, Compute SUM=SUM+X^I & I=I+1, I=I+1 using rectangular box GOTO the step check condition I>N Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find sum of series 1 + X 2 +X 3

Algorithm & Flowchart to find sum of series 1 + X 2 +X 3 …. XN • • • • Without loop structure Algorithm Step-1 : Start Step-2 : I = 1 & SUM=1 Step-3 : Read N, X Step-4 : IF (I > N ) THEN GO TO Step-8 ENDIF Step-5 : SUM=SUM+ X ^ I Step-6 : I = I + 1 Step-7 : GO TO Step-4 Step-8 : Display SUM Step-9 : Stop Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find sum of series 1 + X 2 +X 3

Algorithm & Flowchart to find sum of series 1 + X 2 +X 3 …. XN • Without loop structure Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find sum of series 1 - X + X 2

Algorithm & Flowchart to find sum of series 1 - X + X 2 -X 3 + X 4 …. XN • • • • Without loop structure N: The limit First find sum=-X + X 2 -X 3 …. XN Then add 1 to sum X: x value First term –ve, (SIGN=1) I: loop Counter variable SUM : Sum of given numbers SIGN: for alternate + Initialized I=1 , SUM=1 & SIGN=1 using Rectangular Box First read the Limit , N & X using parallelogram Display “Enter the Limit & Value of X” X , using Parallelogram Read N, N X parallelogram Check condition I>N, I>N using diagonal box If true then print SUM & end processing If false, Compute SUM=SUM+X^I * (-1 ^ SIGN) , SIGN=SIGN +1 & I=I+1, I=I+1 using rectangular box GOTO the step check condition I>N Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find sum of series 1 - X + X 2

Algorithm & Flowchart to find sum of series 1 - X + X 2 -X 3 + X 4 …. XN • • • • Without loop structure Algorithm Step-1 : Start Step-2 : I = 1 , SIGN=1, & SUM=1 Step-3 : Read N, X Step-4 : IF (I > N ) THEN GO TO Step-8 ENDIF Step-5 : SUM=SUM+ X ^ I *(-1 ^ SIGN) Step-6 : I = I + 1 , SIGN=SIGN +1 Step-7 : GO TO Step-4 Step-8 : Display SUM Step-9 : Stop Ajith G. S: poposir. orgfree. com

Algorithm & Flowchart to find sum of series 1 - X + X 2

Algorithm & Flowchart to find sum of series 1 - X + X 2 -X 3 + X 4 …. XN • Without loop structure Ajith G. S: poposir. orgfree. com