Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
0 views

C Programming Part 9

VARDHAMAN COLLEGE OF ENGINEERING - C programming

Uploaded by

molocof324
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

C Programming Part 9

VARDHAMAN COLLEGE OF ENGINEERING - C programming

Uploaded by

molocof324
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Infinitive for loop in C

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");

Basis of Difference For Loop While Loop Do While Loop

The for loop is


appropriate
The other two loops i.e. while and do
when we know in
while loops are more suitable in the
advance
situations where it is not known before
how many times the
hand when the loop will terminate.
loop
will be executed.

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.

A for loop initially A while loop A do while loop will


initiates a counter will always always executed the
variable (initialization- evaluate the code in the do {} i.e.
expression), then it test-expression body of the loop
checks the initially. It the block first and then
How all the three loops
test-expression, and test-expression evaluates the
works?
executes the body of becomes true, condition. In this
the loop if the test then the body of case also, the counter
expression is true. the loop will be variable is initialized
After executing the executed. The outside the body of
body of the loop, update the loop.

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.

Position of the statements


:
In for loop, all the
 Initialization In while and do while loop, they are
three statements are
placed in different position.
 test-expression placed in one position

 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)

Nested for loop


We can also have nested for loops, i.e one for loop inside another for loop. nesting is often used
for handling multidimensional arrays.

Syntax:

for(initialization; condition; increment/decrement)

for(initialization; condition; increment/decrement)

C PROGRAMMING Page 84
{

statement ;

Example:

main()

for (int i=0; i<=5; i++)

for (int j=0; j<=5; j++)

printf("%d, %d",i ,j);

Example : Program to print half Pyramid of numbers

#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.

There are four jumping statements in C language:


 goto statement

 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;

Inthissyntax, label isan identifier.


When, the control of program reaches to goto statement, the control of the program will jump to
the label: and executes the code below it.

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:

printf("you are eligible for voting");

NoVote:

printf("you are not eligible to vote");

printf("Enter you age:");

scanf("%d", &age);

if(age>=18)

goto Vote;

else

goto NoVote;

return 0;

C PROGRAMMING Page 88
}

Output

Enter you age:19

you are eligible for voting

Enter you age:15

you are not eligible to vote

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.

Syntax of break statement

break;

Flowchart

C PROGRAMMING Page 89
How break statement works?

C PROGRAMMING Page 90

You might also like