0% found this document useful (0 votes)
97 views2 pages

Iteration Theory Solutions

This document contains answers to 10 theory questions on iteration in programming. 1) It defines the basic parts of a for loop as initialization, condition, and body/update. 2) It provides examples of for, while, and do-while loops and compares their syntax and flow. 3) It discusses various examples of valid and invalid for loop syntax and behavior of loop variables inside and outside loops. 4) It briefly explains time delay loops and their purpose to pause program execution.

Uploaded by

Aakash Kapoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views2 pages

Iteration Theory Solutions

This document contains answers to 10 theory questions on iteration in programming. 1) It defines the basic parts of a for loop as initialization, condition, and body/update. 2) It provides examples of for, while, and do-while loops and compares their syntax and flow. 3) It discusses various examples of valid and invalid for loop syntax and behavior of loop variables inside and outside loops. 4) It briefly explains time delay loops and their purpose to pause program execution.

Uploaded by

Aakash Kapoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

KP/Iteration (theory) solutions/Page 1 of 2 www.kishorepandit.

com[ kp]
KP/IterationAnswers/Page 1 of 2.
Answers to the 10 theory question on iteration

Q1. Initialization Condition Body Update.

Q2. Syntax Example(to print 1 to 5 ) - Flowchart
For While Do-While
for(init; cond; updt)
{ body;
}


init;
while(cond)
{ body;
update;
}
init;
do
{ body;
update;
} while(cond);
for(int i=1; i<=5; i++)
{ System.out.println(i);
}
int i=1;
while(i<=5)
{ System.out.println(i);
i++;
}
int i=1;
do
{ System.out.println(i);
i++;
} while(i<=5);

for Flowchart


Q3. (i) for(int i=1, j=10; i<=10; i++, j--)
(ii) for( ; i<=10 ; )
(iii) for( ; ; )
(iv) for(int i=1; i<=10; i++);
(v) for(int i=1; i<=10; i++) { Sop(i) } ; // i cannot be accessed outside the loop
(vi) for(int i=1; i<=10; i++); Sop(i); Output=11, because the value of loop variable on exit is always
next in the series.

Q4. Time delay loop purpose is to delay program execution for some time.
N=1
while( N++ < 1000000) ;

Q5. One infinite loop.

Q6. Entry controlled / top/pre tested condition checked before body. Exit... after body.
KP/Iteration (theory) solutions/Page 2 of 2 www.kishorepandit.com[ kp]
KP/IterationAnswers/Page 2 of 2.
Q7. Nested Loop - Loop inside a loop.
Classic Right Aligned
1
12
123
1234
12345
for(int i=1; i<=5; i++)
{ for(int j=1; j<=i;
{ System.out.print(j);
}
System.out.println();
}
1
12
123
1234
12345
int b=5;
for(int i=1; i<=5; i++)
{ for(int j=1; j<=b; j++)
{ System.out.print(" ");
}
for(int j=1; j<=i; j++)
{ System.out.print(j);
}
System.out.println();
b--;
}
Side Mirror Bottom Mirror
1 1
12 21
123 321
1234 4321
1234554321
int b=8;
for(int i=1; i<=5; i++)
{ for(int j=1; j<=i; j++)
{ System.out.print(j);
}
for(int j=1; j<=b; j++)
{ System.out.print(" ");
}
for(int j=i; j>=1; j--)
{ System.out.print(j);
}
System.out.println();
b-=2;
}
*
***
*****
*******
*********
*******
*****
***
*
int b=5;
for(int i=1; i<=9; i+=2)
{ for(int j=1; j<=b; j++) Sop(" ");
for(int j=i; j>=1; j--) Sop("*");
System.out.println();
b--;
}
b=2;
for(int i=7; i>=1; i-=2)
{ for(int j=1; j<=b; j++) Sop(" ");
for(int j=i; j>=1; j--) Sop("*");
System.out.println();
b++;
}


Q8. FOR When number of times are known.
WHILE When number of times are not known.
DO-WHILE When the loop is to be executed at least once.
Q9. Similarity- both are jump statements., both ignore statements after them.
Difference- Break terminates a loop. Continue Starts the next iteration.
Q10. Label Name given to a statement.
Way of giving- label: statement
When we wish to exit the outer loop in a nested loop structure.
E.g. the following will exit the outer loop.
outer: for(int i=1; i<=5; i++)
{ for(int j=1; i<=3; j++)
{ if(i==2) break outer;
}//j
}//i

END

You might also like