Any suggestions or improvements?

Can I use the for loop to replace the do...while loop in this program? Any suggestion to make this program more simple? This program ask the user to enter their option from 1 to 5 (if choice is another number the program will terminate) My code:

int choice;
    char option;
    printf("You want to know what I'm thinking about you?\n");
    printf("1. Add New Contact\n");
    printf("2. Edit Existing Contact\n");
    printf("3. Call Contact\n");
    printf("4. Text Contact\n");
    printf("5. Exit\n");

for (int i = 0; i < 5; i++)
    printf("Please choose the above option:\n");
    scanf(" %d", &choice);
    switch (choice)
    case(1) : printf("To add new contact, you need the contact's: First name, Last name and Number");
    case(2) : printf("To edit, enter new contact's number");
    case(3) : printf("Who do you wish to call");
    case(4) : printf("Who do you want to text");
    case(5) : exit(1);
    default: printf("\n%d is not a valid choice!\n", choice);

Source code:

printf("Enter your choice: ");
scanf(" %d", &choice);
switch (choice)
case (1): printf("\nTo add you will need the ");
printf("First name, last name, and number.\n");

case (2): printf("\nGet ready to enter the name of ");
printf("name of the\n");
printf("contact you wish to change.\n");
case (3): printf("\nWhich contact do you ");
printf("wish to call?\n");
case (4): printf("\nWhich contact do you ");
printf("wish to text?\n");
case (5): exit(1); //Exits the program early
default: printf("\n%d is not a valid choice.\n", choice);
printf("Try again.\n");
} while ((choice < 1) || (choice > 5));
return 0;


I don't think for this problem replace the "do...while" loop with the "for" loop is well. In the first code (for loop) if the user choose a correct answer the program make 5 times the same operation. Insted in the "do while" the loop stop just when the answer is correct (chose is = 1...2....3...4..or 5). I think that the second code is right regards

Well, in theory you could, but it wouldn't make any sense. As a rule of thumb:

  • Use for when the number of iterations are known. (Usually considered easiest to read and usually produces the most effective code.)
  • Use while when the number of iterations are unknown.
  • Use do while when the number of iterations are unknown, but you must put the loop condition after the first lap in the loop (for example because you need to evaluate user output).

I'm afraid, no. As the code is currently written, the major behavioral difference, unless the input is 5,

  • The for loop version is guranteed to execute 5 times,
  • The do...while version depends on the input value for each iteration. for any value less than 1 or greater than 5 will terminate the loop. Otherwise, it will continue looping.

Need Your Help

Deploying VBA project that uses ADO objects

excel vba deployment ado

In an Excel VBA project, I need to read and write UTF-8-encoded text files. After reading this and this SO topic, I am considering using ADO stream objects to accomplish this.

displaying html in div or iframe

javascript jquery html iframe

I want to display my html string in this iFrame.