Iterative Methods:
The term
``iterative method'' refers to a wide range of techniques that use successive
approximations to obtain more accurate solutions to a linear system at each
step. In this method . In this types of methods, one or more steps are repeatedly
executed more than one times to obtain the require solution. This is the most
powerful technique to solve the linear problem which has the same operation to
be performed more than one times. In iterative technique we use looping construct to repeat the same
operations.
For example the
algorithm to add the natural number up to n without iterative method can be
1. Input n
2. Sum=1+2+3+4+5+6+ ……+n;
3. Display sum
If the
number of terms is large this straight forward method is not suitable for the
solution. Which is not appropriate algorithm for programming. We can write the
iterative algorithm to add n natural number as:
1. Input
n
2.
count = 1 , sum=0
while count <=1 do repeat step 3 to 4
3. sum = sum + count
4. count = count +1
5. print
the sum
In this case step 3
and 4 are repeatedly executed until the
condition is not true. If the condition is not true then rest of the step are
executed.
Hence we can see
that writing the algorithm using iterative method is much more easier and
practical to solve the problem.
Control Loops in C
In C iterative
algorithms are written using the Loops. Loops are used to repeat a block of
code. C gives us a choice of three types of loop, while loop, do while loop and for
loop
- The while loop keeps repeating an action until an associated test returns false. This is useful where the programmer does not know in advance how many times the loop will be traversed.
- The do while loops is similar, but the test occurs after the loop body is executed. This ensures that the loop body is run at least once.
- The for loop is frequently used, usually where the loop will be traversed a fixed number of times. It is very flexible.
However we can
write an iterative algorithm to solve a problem using any one of these three loops.
The while
statement :
The while statement, also called the
while loop, executes a block of statements
as long as a specified condition is true. The while
statement has the following form:
while
(condition)
statement;
condition is any C expression usually a relational or
logical expression and statement is a
single or compound C statement. When program execution reaches a while
statement, the following events occur:
1. The expression
condition is evaluated.
2. If condition
evaluates to false (0), the while statement
terminates, and execution
passes to the
first statement following statement.
3. If condition
evaluates to true (nonzero), the C statement(s) in statement are executed.
4. Execution returns
to step 1.
Example A simple while statement.
1: /* Demonstrates a
simple while statement */
2:
3: #include
<stdio.h>
4:
5: int count;
6:
7: void main()
8: {
9: /* Print the
numbers 1 through 20 */
10:
11: count = 1;
12:
13: while (count
<= 20)
14: {
15:
printf("%3d", count);
16: count++;
17: }
18: }
A small example example:
/* get numbers until you get one greater than 99 */
int number =0;
while (number <= 99)
scanf("%d", &number );
/* An example to
read the marks of n students and calculates the average */
#include<stdio.h>
void main()
{
float marks,total,avg;
int num_of_student,count;
printf(“\nEnter the no of students:”);
scanf(“%d”,&num_of_student);
total=0.0;
count=0;
while(count<num_of_student)
{
printf(“\nEnter the marks:”);
scanf(“%f”,&marks);
total+=marks;
count++;
}
avg=total/count;
printf(“\nThe average marks is %f “,avg);
}
Run the program and observe the output .
Nesting while Statements
Just like if else while statements can also be nested i.e.
the while statement itself contains other while statements as our requirements:The
general syntax of nested while is:
While(expression1)
{
while(expression2)
{
statements;
}
other statements;
}
Use of break
statement in while loop:
The break statement is used to
break the loop even if the expression of while loop evaluates true. If break statement
is encountered inside the while loop, the rest of the part of the loop is
skipped i. e. loop is broken out.
An example:
sum = 0;
i = 1;
while(i <= 10)
{
sum += i;
i++;
}
The above loops adds the number 1 to 10.
We can break from while loop using break statement writing
lines above using break as :
sum = 0;
i = 1;
while(1)
{
sum += i;
i++;
if(i>10)
break;
}
Use of continue
statement in loop
Using continue statements inside a
loop, we can skipped some statements and continue the update part(increment or
decrement ) without executing the other statements in loop. Example for
continue which is used to add the even number up to 10 is shown below.
i = 2;
sum = 0;
while(i<=10)
{
sum += i;
i +=2;
}
this can be equivalently written as
i = 1;
sum = 0;
while(i <= 10)
{
if(i%2!=0)
{ i++;
continue;
}
sum+= i++;
}
For statement:
The for
statement is a C programming construct that executes a block of one or more
statements a certain number of times. It is
called the for loop because program execution typically loops through
the statement more than onceA for statement has the following structure:
for ( initialization statement; condition
statement;
increment/decrement statement )
{
statement sequences;
}
e.g. for(i = 0;i<= 10; i++)
sum += i;
Here , the for loop has to execute only one statement:
sum+=i; , so , we have not used left
and right curly brackets.
In the above for loop,
- i = 1; initialization statement
- i <= 10 ; condition statement
- i++ , increment statement
Rules regarding for loop:
- A for loop generally contains three types of statement: initialization, condition and increment/decrement.
- All the three kind of statements in the for loop are optional. It is permitted to write:
for(;
;){
…..
….
}
But , it is absolutely necessary to
put two semicolons in for loop even if there are no initialization and
condition statements
- There can be more than one initialization statements and increment/decrement statements. In such cases each statements should be separated by comma ‘,’ . for example we can write :
for(sum
= 0,i = 1; i <= 10;sum += i, i++)
;
- There may not be single statement following for loop, even that case, a semicolon ‘;’ is must be put after the for loop indicating that the for loop executes a null statement.
How for loop works ?
Step 1:
Initialization statements are executed . these are executed only once.
Step 2: Condition is
tested
Step 3: If condition
is true, the statements associated with for loop are executed. If
condition
is false, exit from the for loop.
Step 4: Increments are executed
Step 5: Go to step 2
A simple example of for loop:
/* This program adds
numbers from 1 to 20 and display the sum in screen */
1: #include<stdio.h>
2:
3: void main()
4: {
5: int sum,i;
5: int sum,i;
6: sum = 0;
7: for(i=1;i<=20;i++)
8: sum += i;
9: printf(“The sum is : %d” , sum);
10:} /*end of main */
The Alternatives for the for statement in line no 7 of above
program can be written as:
1. for(sum = 0, i= 1; i<=20; sum+=i; i++)
;
2. sum = 0;
i=1;
for(;i
<= 20;sum += i,i++)
;
3. sum = 0;
i=1;
for(
; i <= 20; i++)
sum += i;
4. sum = 0;
i=1;
for(
; i <= 20;) {
sum += i;
i++;
}
5. sum = 0;
i
= 1;
for(;
;)
{
sum += i;
i++;
if(i>20)
break;
}
In fifth case the for(;;) loop is
infinite loop, as condition part is missing.Which is controlled by using break
statement.
Use of continue and
break
/* A simple program
that adds the odd numbers from 1 to 99 */
#include<stdio.h>
void
main()
{
int
sum = 0, i;
for(i=1;i++)
{
if(i%2
== 0)
continue;
sum += i;
if(i
>= 99)
break;
}
/*end of for loop */
printf(“The sum of 1,3,… ,99 is : %d”,sum);
} /*end
of main() */
break:
breaks the loop and control goes out from the loop .
continue:
goes to execute the increment or decrement part
of the for loop without executing rest of
statements in loop.
The do while statement:
do while is another type
of looping construct in C in which statements are executed at least once even
if the condition is not true. It executes the statements in the loop block at
once and checks the condition . Until
the condition is not true it executes the loop repeatedly and when
condition is false, the loop exits.
The general construct of
‘do while’ statement is :
do{
Statements sequences;
}while(condition );
condition is any C expression, and statement is a single or
compound C statement. When program execution reaches a do...while statement,
the following events occur:
1. The statements in
statements sequences are executed.
2. condition is
evaluated. If it's true, execution returns to step 1. If it's
false, the loop terminates.
The statement in
above program
for(sum = 0;i =1; i <= 20;i++)
Sum += i;
Can be written
using ‘do while’ loop as:
sum = 0;
i = 1;
do{
sum += i;
i++;
}while(i <= 20 );
In while
statement, condition is tested at the beginning and if the condition is false
the statements in loop are not executed .of the loop but in ‘do while’ the condition is tested at
the end of the loop after executing the statements at once.
Use of continue in while, do while, and for loop
- In case of ‘while’ and ‘do while’ loop when the continue encountered, it skips the remaining of the statements of loop and goes to check condition.
- In case of ‘for’ loop, when the continue is encountered, the remaining statement of the loop are skipped and increment statements of for loop executed and then the condition is tested.
- The ‘continue’ statement is applied to loop only not in ‘switch’
- The ‘break’ statement exits the loop skipping the remaining statements in loop in all types of loop.
e.g.
for(……; ……;…….)
{
……………
……………
if(condition)
break;
………….
…………
} /* end for */
while(condition)
{
…………
……………
if(condition)
break;
………..
………….
} /* end of while */
………..
……….
No comments:
Post a Comment