SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
.loops
Exercise:
Check Point
Question:7 | ISBN:978013274719 | Edition: 6

Question

Can you convert any for loop to a while loop? List the advantages of using for loops.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Yes,we can convert the for loop into while loop....

  •       To convert ,we need to add the initialization statement before the while loop.

Lets look into the syntax

For loop syntax:

  • for(initialization; condition ; increment/decrement)
  • ----our code----

While Loop Syntax:

  • initialization; 
  • while(condition){ 
  • ----our code----t; 

Examples:

  • Example-Forloop
  • for(int i=0; i<10; i++)
  • //Print i 
  •  
  • Example-whileloop
  • int i=0; 
  • while(i<10)
  • //Print i 
  • i++; 

 

 

Advantages of using Loops

These are the following advantages of loops:

  1. Loops provides code re-usability.
  2. With the help of loops, we do not need to write the same code again and again.
  3. Using loops, we can traverse over the elements of data structures (array or linked lists).
1 0

Discussions

Post the discussion to improve the above solution.