C Programming Part 9
C Programming Part 9
If you don't initialize any variable, check condition and increment or decrement variable in for
loop, it is known as infinitive for loop. In other words, if you place 2 semicolons in for loop, it is
known as infinitive for loop.
for(; ;){
C PROGRAMMING Page 81
printf("infinitive for loop example by javatpoint");
In case if the
Where to
test condition In case if the test
Use for Loop, while Loop
fails at the condition fails at the
and do while Loop
beginning, and beginning, and you
you may not may want to execute
want to execute the body of the loop
the body of the atleast once even in
loop even once the failed condition,
if it fails, then then the do while
the while loop loop should be
should be preferred.
preferred.
C PROGRAMMING Page 82
the update-expression expression
is executed which should be
updates the value of updated inside
counter variable. the body of the
while. However,
the counter
variable is
initialized
outside the body
of the loop.
update-expression
for (
initialization-
exp.(s);
while(test- do {
test-expression(s); expression)
body-of-the-
update- { loop;
expression(s)
body-of-the- update-
Syntax of Loops
) loop; expression(s);
{ update- }
expression(s);
body-of-the-loop while (test-
; } expression);
C PROGRAMMING Page 83
do while loop is an
exit controlled loop,
Which one is Entry
Both loops i.e. for loop and while loop are means means that
Controlled Loop
entry controlled loop, means condition is condition is placed
and
checked first and if the condition is true after the body of the
Which one is Exit
then the body of the loop will executes. loop and is evaluated
Controlled Loop ?
before exiting from
the loop.
int i = 1; int i = 1;
: :
:
: :
Conversion of one Loop :
to another Loop do
while (i<=10)
or for (int i=1; i<=10;
Example : Print numbers i++) { {
from 1 to 10 using all the
{ Printf(“%d”,i);
three loops.
Printf(“%d”,i); ++i;
Printf(“%d”,i); }
++i }
} while (i<=10)
Syntax:
C PROGRAMMING Page 84
{
statement ;
Example:
main()
#include<stdio.h>
#include<conio.h>
void main( )
int i,j;
for(i=1;i<5;i++)
printf("\n");
C PROGRAMMING Page 85
for(j=i;j>0;j--)
printf("%d",j);
getch();
Output
21
321
4321
54321
Jump Statements
Jumping statements are used to transfer the program‟s control from one location to another, these
are set of keywords which are responsible to transfer program‟s control within the same block or
from one function to another.
return statement
break statement
continue statement
goto statement : goto statement doesnot require any condition. This statement passes control
anywhere in the program i.e, control is transferred to another part of the program without testing
any condition.
C PROGRAMMING Page 86
Syntax : goto label;
.....
.....
label:
statements;
Or
The goto statement requires a label to identify the place to move the execution. A label is a valid
variable/identifier name and must be ended with colon ( : )
Flowchart
C PROGRAMMING Page 87
Example
int main()
int age;
Vote:
NoVote:
scanf("%d", &age);
if(age>=18)
goto Vote;
else
goto NoVote;
return 0;
C PROGRAMMING Page 88
}
Output
Break Statement
Break is a keyword. The break statement terminates the loop (for, while and do...while loop)
immediately when it is encountered. The break statement is used/ associated with decision
making statement such as if ,if-else.
break;
Flowchart
C PROGRAMMING Page 89
How break statement works?
C PROGRAMMING Page 90