hi, i'm new to c++(just 3 days) and new to this forum too (so pls go easy on me). my problem is i can't remember how to use the for, while and do loops. but when i see a program with these loops in them i completely understand the program but can't write one cuz i keep forgeting how it's done. is there any "method" or "trick" you guys use to remember them? and if it's just that i need more practice can you link me to a site that has a bunch of "worked" examples and exercises?
thanks!

Recommended Answers

All 6 Replies

A for() loop is best used when you know (either based on a variable's value, or a set number) how many times a loop should run. For example, if I wanted to have a message printed exactly 5 times, I would do this:

for(int x = 0; x < 5; x++)
    std::cout << "Hello, world!" << std::endl;

A [I]while()[/I] loop is often used when you don't know how many times a loop should run, or are dependant on a changing condition to determined when you should exit the loop. For example:

#include <iostream>

int main() {
    std::string input("");
    
    while(input != "q") {
        std::cout << "\nEnter a message (\'q\' to quit):\n>>> ";
        std::getline(std::cin, input);
    }
}

A

do {
} while()

loop is used when you always want something to happen once, and then check a condition to see if it should happen again.

Hope what helped somewhat. :)

Well within three days you can not understand all of the three loops :P.

It will take some time, all you can do is practice more and more and try to understand the code of others. This will help you alot while understanding the programming.

thanks for the info..i havent checked my mail in a while thats why i'm just replying. and i have one more loop question...its a program to make a simple calculator. with this calculator presing "C" will clear the number and "Q" will exit the program. i've seen the way the book says it but i have changed a few things in it just for practice but it keeps saying "switch quantity not". i dont know what that means...any help?
anyway, here is the program pls show me where i have gone wrong. thanks

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
    double on_value = 0.0;
    double initial_value;
    double new_value;
    double second_value;
    double entry;
    double operand;
    double answer;
    cout<<on_value<<endl;
    
    cout<<"Input first entry: ";
    cin>>entry;
    new_value = on_value + entry;
    cout<<new_value<<endl;
    
    cout<<"Input operand: ";
    cin>>operand;
    
    switch (operand)
    {    
    case "C":
    cout<<on_value;
    break;
    
    case "+":
    cout<<"Input next number: ";
    cin>>entry;
    answer = new_value + entry;
    break;
    case "-":
    cout<<"Input next number: ";
    cin>>entry;
    answer = new_value - entry;
    break;
    case "/" :
    cout<<"Input next number: ";
    cin>>entry;
    answer = new_value / entry;
    break;
    case "*":
    cout<<"Input next number: ";
    cin>>entry;
    answer = new_value * entry;
    break;
    default:
    cout<<"Not understood. Inout proper operand"<<endl;
    }
    system("pause");
    return 0;
    }

p.s.
i have erased and put back a few things so i think it's a bit messy

but i have changed a few things in it just for practice but it keeps saying "switch quantity not". i dont know what that means...any help?

double operand;
// ...
switch (operand)
{
   case "C":
   // ...

"switch quantity not an integer"? You can't use a switch with strings, either.

thanks for the reply ...that was FAST!!! i changed the double operand to char operand and it worked fine thanks. oh yea i also changed the quotation (" ")marks found in the switch loop to apostrophe (' '). is there a way of knowing which is to be used?

oh yea i also changed the quotation (" ")marks found in the switch loop to apostrophe (' '). is there a way of knowing which is to be used?

A single character enclosed within single-quotes is a character constant '@' .
A string of zero or more characters enclosed in double-quotes is a string literal: "string literal" .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.