Hello folks...!!!
Today we are going to look at Nested for loops and for loops with 'else'
condition.
Nested for-loops
Nested loops are nothing but for loops within for loops. The inner for loop gets executed n times for each iteration of the outer loop. Let us look at a simple example :Now look at this nested loop. The outermost for-loop is executed 5 times. For each iteration of the outer loop, the inner loop is executed five times. Unlike while loop, the incrementation is taken care by the loop itself. We need not specify it unless and until we need to change the interval.
We saw many pattern printing problems in while loop. The same problems can be solved with nested for-loops also.
Calling the print() method, simply prints a new line.
Let us look at code for finding the sum of a series . Consider the series below :
y=1+(1+2)+(1+2+3)+(1+2+3+4)+.......................+(1+2+3+.......+n)
- In the code above, the inner loop calculates the sum of numbers within the brackets.
- In the outer loop, it is added to the 'ans' variable.
- Here I have taken n=3.
- So the series is : 1+(1+2)+(1+2+3)
- If it is evaluated, the first term is '1' , the sum of second term is (1+2)=3, the sum of third term is (1+2+3)=6. So the final answer is 1+3+6=10.
- The evaluation is done in the order given below:
ans=0
outer-iteration 1:
sum=0,ans=0
inner loop 1: 1<2
sum+= j
sum=1
ans+=sum
thus ans=1
outer-iteration 2:
sum=0,ans=1
inner
loop 1: 1<3
sum=1
inner
loop 2: 2<3
sum=1+2=3
inner
loop 3: 3<3
ans=1+3=4
outer-iteration 3:
sum=0,ans=4
inner
loop 1: 1<4
sum=1
inner
loop 2: 2<4
sum=1+2=3
inner
loop 3: 3<4
sum=3+3=6
inner
loop 4: 4<4
ans+=sum:
4+6=10
final ans: 10
The else condition can be used to execute some instructions on termination of the loop. If the loop is terminated by a 'break' statement, then the else-condition wont be executed.
Examples:
Let us look at the code for checking whether a number is prime or not.
An input number is not prime if it is divisible by any number which is less than or equal to half the input number. We use '//' in the condition because, it yields an integer output on division. The flag is just a variable used for keeping track of divisibility of a number.
Thats all about the for-loops. Do try the exercises given for while loop using for-loops. Try especially the pattern printing problems.
The else condition can be used to execute some instructions on termination of the loop. If the loop is terminated by a 'break' statement, then the else-condition wont be executed.
Examples:
Let us look at the code for checking whether a number is prime or not.
An input number is not prime if it is divisible by any number which is less than or equal to half the input number. We use '//' in the condition because, it yields an integer output on division. The flag is just a variable used for keeping track of divisibility of a number.
Thats all about the for-loops. Do try the exercises given for while loop using for-loops. Try especially the pattern printing problems.
Next Topic👉
Comments
Post a Comment