Thread: Convert for loop to while loop

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    1

    Convert for loop to while loop

    Hi, could someone help me convert this for loop to while loop
    And how to detect how many times each one is executed or evaluated please?
    Thank you

    Code:
    int x=1, a=33;
    for(a=17; a< 20; a++){
    	x++;
    }
    
    ******************************
    
    int x=1, a=33;
    
    while(          ){
    
    	x++;
    
    }
    
    Final value of a:______ 
    Final value of x:______ 
    How many times a=17 is executed :______
    How many times a< 20 is evaluated:_______
    How many times x++   is executed:________
    How many times a++ is executed:_________

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    for ( a ; b ; c ) {
      d;
    }
    is the same as
    Code:
    a;
    while ( b ) {
      d;
      c;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  2. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  3. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  4. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  5. Help with loop to convert string to int
    By m712 in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2002, 03:11 AM

Tags for this Thread