Differences between if() and for()

This is a discussion on Differences between if() and for() within the C++ Programming forums, part of the General Programming Boards category; Hi there, I'm just new into learning C++, enjoying it alot too! But I want to learn too quickly and ...

  1. #1
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Question Differences between if() and for()

    Hi there, I'm just new into learning C++, enjoying it alot too! But I want to learn too quickly and find myself having to backtrack alot.

    My question is this, I'm up to the loop chapters and have been learning everything about the "if" statement and now it goes to the "for" statement, far as I can see "for" is wayyy better but since I'm new at all this, is it!?

    The book will probably tell me later but as I said, I want to know NOW

    Can I do everything I would do with if with for?
    With the for statement I don't need a "goto" statement and as far as I've heard you don't want to use the "goto" anyways.
    The for|do|while statements act completely nicer than if, sometimes cutting down 90% of the lines.

    Hate to bother you people with this newbie question but that's what a forums for right!

    Thanks alot,

    Rob

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The for statement repeats a block of code a specified number of times. In the code below, i is a loop counter and causes the block of code to be repeated 10 times because i is incremented at the end of each loop iteration.
    Code:
    for(int i = 0; i < 10; ++i)
    {
       // do something
    }

    The if statement just tests a statement for true or false. If true then the block of code is executed only once. If the expression is false, the block of code is not executed;
    Code:
    int i = 5;
    if( i == 0)
    {
       // do something
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If you are confused because you replaced an if and a goto with a for loop, then what you are really saying is that a for loop is better than a goto, which is correct. Since if by itself does something different than for, you cannot say one is better in general.

    If your block of code will always only run once, use an if statement, it is clearer, and can be used with else for more complicated code. If your block of code must be run in a loop n number of times (where n can be 0 or more), then use a loop- either for, while, or do-while.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Let's say you want the user to enter an integer number. After that, you want to accomplish two tasks:

    1) If the number the user entered is equal to 10 display it.

    2) Display the number ten times each time adding one to it.

    How would you accomplish each of those tasks? Write a program to accomplish task 1, and write another program to accomplish task2.
    Last edited by 7stud; 10-26-2005 at 01:00 AM.

  5. #5
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Makes sense now thanks

    Makes sense, thanks "for " the replies!
    Going to save this thread for reference while I lean c++.

    Sure is funny that c++ (so far) seems like an advanced version of a ms-dos batch file where you make your own "choice" and errorlevels.

    Very thankful for the super quick response to a "newbie" question as well, posted a similar question in the newsgroups only to get flamed!
    Obviously cprogramming.com is no-nonsense,

    Rob Sitter

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Robert_Sitter
    Sure is funny that c++ (so far) seems like an advanced version of a ms-dos batch file where you make your own "choice" and errorlevels.
    don't even think that way or you most certainly will get flamed All programming languages have some similarities but in most cases that doesn't mean that one language is an advanced version of another. c++ is one of the vew exceptions -- it was derived from C.

  7. #7
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Last post to close this thread

    I completely understand now and have tested it out, here's my example to what you've showed me:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    
    	int i = 1, first = 0, max = 0;
    
    	cout << "\nEnter the starting number: ";
    	cin >> first;
    	cout << "\nEnter the ending number: ";
    	cin >> max;
    	cout << "\n";
    
    	if(first >= max) //  :D 
    	{
    	cout << "There was an error, you cannot count in a incrementing fassion\n"
    	<< "from " << first << " to " << max << ".\n"
    	<< "Please re-start this program and try again.\n";
    		system("pause");
    		return 0;
    	}
    	for(; first <= max; first++) //  :D 
    	cout << "First number: " << first << " incrementing to: "  << max << "\n";
    
    	system("pause");
    	return 0;
    }
    return 0;
    Bye Until the next question!

    Rob Sitter

  8. #8
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    326
    if and for, what do they have in common?

    An if / else statement is a decision structure whereas for/do-while/while structures are repition structures.

    "If this, then that." is much different than:

    "Do this while this condition is true."

    Used in conjuction, any algorithm can be achieved. Be sure to keep the two logics seperate, however.

    Neither IF or FOR is better than one another, they both achieve totally different ends. Study your flow-charts and you will see the fundamental difference and why you cannoy say FOR is "wayyy better" than IF.

    and LOL @ C++ being a MS-DOS batch file. I can't even explain what a silly comparison that is but suffice it to say that C++ is supremely more sophisticated than a dumb MS-DOS batch file. Sure, at some level they both use the same logic, such as decision and looping structures, but I assure you that any complied programming language (even BASIC) is much more advanced than a MS-DOS .BAT file.
    Last edited by MacNilly; 10-31-2005 at 10:48 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21