Search
Left Quote    Wise men speak because they have something to say; Fools because they have to say something.
- Plato (427-347 B.C.)    
Right Quote
 
[login] | [Register]
 

Beginning Visual C++ 6.0

by: bs0d
Page: 6 of 10
(View All)

While loop


The while loop is pretty easy to get use to and can be quite effective for repeating code for you. Here is the syntax of a while loop:


while(condition)
{

statements to be executed
will go inside the braces

} //ends the while loop.

Just like for/next, its not required to have brackets ({ and }) if you only have one statement within the loop, but again its good pratice to include them and it wont hurt anything.


Now check out the code sample and see a while loop in action:



Simple enough right? You can nest while loops with other loops and all that neat stuff, so play around with that if you like, were going to move right along to the the for loop.


Do While loop


This loop is similar to the while loop, but this one is a posttest loop where the condition is set after the statements. Here is the syntax for a do while loop:


do
{

statements to
be executed

} while (<condition>);

I haven't found myself using a do while loop much often, but it does have its purposes. Here is a smiple code sample that will how you how to use a do while loop in your Visual C++ program(s):





There you have it. Watch out for infinite loops. These are loops that never end, so your program keeps running and running, draining your resources. This may occur in the code above if we incremented count, but set the condition to be a negative number. Count will never be negative, so the program never ends! If this does happen, you can quickly close the DOS window by pressing the "X" in the top right hand corner, or by using the keyboard shortcut "ctrl + c" should do the trick.


For Loop


For loops are likely a good reason why do while loops are not often used (for me) and perhaps by others as well. The reason is that for loops provide a more compact way of writing counters and such. Look at the syntax:


for(initilization; condition; update)
{

statements to
be executed

} // end for loop

The for loop has three arguments. Initialization is for example where you would set the # for the counter to begin with. The second statement is the condition. This would be how long you want the counter to count. The third is update, where you update the value of the counter. Heres a simple example:



That code sample would count to 10, with a number on each a new line.


Arrays


Arrays can get complex, and confusing - expecially when you get into multi-dimensional arrays. But im just going to cover the basic array. Depending on how you will use the data, is depending on how you will declare the array. you can declare each element like this:




Or, you can assign them all the same value with one statement:


You can store data into an array with a loop:



They are as hard as you make them I guess. I haven't worked much with multi-dimensional arrays in Visual C++, so I believe thats a pretty good start with arrays.




1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

99,098 Views
1 Total Comments
4 Rating of 5 (2 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· C Tutorial
Your Article Here



"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank