HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
C Programming Language Part 6
1.
2. Loops
A loop statement allows us to execute a statement or group of statements multiple
times and following is the general form of a loop statement in most of the programming
languages:
3. Loop Type Description
while loop Repeats a statement or group of statements while a given
condition is true. It tests the condition before executing
the loop body.
for loop Execute a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
do...while loop Like a while statement, except that it tests the condition at
the end of the loop body
nested loops You can use one or more loop inside any another while, for
or do..while loop.
Type of Loops
4. Loop Control Statements:
Control Statement Description
break statement Terminates the loop or switch statement and transfers
execution to the statement immediately following the
loop or switch.
continue statement Causes the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.
goto statement Transfers control to the labeled statement. Though it is
not advised to use goto statement in your program.
5. while loop in C1
Syntax: while (condition)
{
statement(s);
}
Flow Diagram:
A while loop statement in C programming language repeatedly executes a target statement
as long as a given condition is true.
6. Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %dn", a);
a++;
}
return 0;
}
7. do...while loop in C
2
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed
to execute at least one time.
Syntax: do
{
statement(s);
}while( condition );
Flow Diagram:
8. Example
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do
{
printf("value of a: %dn", a);
a = a + 1;
}while( a < 20 );
return 0;
}
9. A for loop is a repetition control structure that allows you to efficiently write a loop
that needs to execute a specific number of times.
Syntax:
3
for ( init; condition; increment )
{
statement(s);
}
Flow Diagram:
10. Example:
#include <stdio.h>
int main ()
{
/* for loop execution */
for( int a = 10; a < 20; a = a + 1 )
{
printf("value of a: %dn", a);
}
return 0;
}
11. Nested loops in C
4
Syntax
Nested for loop
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
14. Example of for Loops and while loops
Write a program uses a nested for loop to find the prime numbers from 2 to 100:
#include<stdio.h>
main()
{
int a,n,f;
for(a=2;a<=200;a++)
{
n=2; f=0;
while(n<=a/2)
{
if(a%n==0)
{
f=1;
break;
}
n++;
}
if(f==0)
{
printf("prime %d n",a);
}
else
{
printf(" .....non prime %d n",a);
}
}
}
16. Break statement in C1
The break statement in C programming language has the following two usages:
1. When the break statement is encountered inside a loop, the loop is immediately
terminated and program control resumes at the next statement following the loop.
2. It can be used to terminate a case in the switch statement (covered in the next chapter).
18. Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %dn", a);
a++;
if( a > 15)
{
/* terminate the loop using break statement
*/
break;
}
}
return 0;
}
19. Continue statement in C
2
The continue statement in C programming language works somewhat like
the break statement. Instead of forcing termination, however, continue forces
the next iteration of the loop to take place, skipping any code in between.
For the for loop, continue statement causes the conditional test and
increment portions of the loop to execute. For the while and do...while loops,
continue statement causes the program control passes to the conditional tests.
21. #include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do
{
if( a == 15)
{
/* skip the iteration */
a = a + 1;
continue;
}
printf("value of a: %dn", a);
a++;
}while( a < 20 );
return 0;
}
Example
22. #include<stdio.h>
main( )
{
int i, j ;
for ( i = 1 ; i <= 2 ; i++ )
{
for ( j = 1 ; j <= 2 ; j++ )
{
if ( i == j )
continue ;
printf ( "n%d %dn", i, j ) ;
}
}
}
Example
23. goto statement in C
A goto statement in C programming language provides an unconditional jump from
the goto to a labeled statement in the same function.
NOTE: Use of goto statement is highly discouraged in any programming language
because it makes difficult to trace the control flow of a program, making the program
hard to understand and hard to modify. Any program that uses a goto can be rewritten
so that it doesn't need the goto.
Syntax
goto label;
..
.
label: statement;
25. #include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
LOOP:do
{
if( a == 15)
{
/* skip the iteration */
a = a + 1;
goto LOOP;
}
printf("value of a: %dn", a);
a++;
}while( a < 20 );
return 0;
}
Example
26. #include <stdio.h>
main( )
{
int goals ;
printf ( "Enter the number of goals scored against India" ) ;
scanf ( "%d", &goals ) ;
if ( goals <= 5 )
goto sos ;
else
{
printf ( "About time soccer players learnt Cn" ) ;
printf ( "and said goodbye! adieu! to soccer" ) ;
exit( ) ; /* terminates program execution */
}
sos :
printf ( "To err is human!" ) ;
}
Example