![]() |
|
||||||
AssignFinally I will cover the assign function. With this spetacular function (i know you're excited), you can assign portionsof one string into another! Like always, the syntax is: destinationString.assign(sourceString,startAssign, numOfCharsToAssign) Now, lets see how she works: In the sample above, you see we start with a string that has first and last. We then assign into the string, first, 6 characters. And then we assign the string, last, 8 characters starting with the 7th from the fullName string. You can use this function in another way, like duplicating characters. Say you needed an exact amout of characters and might even need to edit the amount. Instead of just assigning them by hand, like : string str1 = "________________"; You can use the assign function to assign the variable a number of characters. See the code below: The assign statement assigns 35 of character in the 2nd argument into the string, bar. You can easily change that 35 to 1000, or whatever. Thats an advantage to you. If / ElseMoving on a bit, we'll talk about conditions. In C++ you'll be making some decisions in your programs, so If else and elseif come to the rescue! The syntax is pretty much standard, but again its all different; like opposed to Visual Basic. So follow these syntax guidelines: if (condition) { do this and this } //end if else { do this and that } //end else If you only have one statement, then the brackets are not required. The operators you will use are:
*Note: When you use varX = something; thats different when you are comparing something in your condition and use '==', dont try to use '=' in your condition or you will get an error. Thats pretty common occurance, so be alert!! Lets get a code sample started showing you the ropes using if/else and some operators:You can look at that and make out whats going on. On the bottom condition with the letters, I defined the the characters as character literal constants (single quotes, [string liter consants are double]) so there is a difference between the case of the letters. Nested ConditionsThats about all there is to If/Else, unless you're unaware of working with nested conditions. This is where you will build multiple conditions to each er, condition. Like for example heres another code to look at (just the meat): The inital condition is asking for gender, if you answer 'male' then you are also asked if you are over 21, if you say 'yes' it shows message, if you say no, it shows a different message. And if you answer anything other than 'male' to the first condition, it shows the last message. Else-if and SWITCHTechnically, you can set more conditions upon conditions... Another method is to use else if. I will say right now that its not necessairly worth using if you have alot of conditions, i would use it on only a few conditions, because there is a better solution to multiple conditions. Before I get there, heres how you can use else if: if(something == 'this' || something == 'THIS') do this else if(something == 'that' || something == 'THAT') do that else if(something == 'this-n-that' || something == 'THIS-N-THAT') do this-n-that else do nothing I think thats fine, a few conditions and else if never hurt anyone, but if you want to save your fingers and have quite a few conditions to deal with, I recommend to you switch. It will be easier to read and can deal with alot of conditions quite easily. All you need to know is the syntax, so here it is to follow: switch(<VARIABLE>) { case 'A': case 'a': <code to do something here> break; case 'B': case 'b': <code that does something for these conditions> break; case 'C': case 'c': <something for these conditions> break; default: <code that executes if VARIABLE meets none of the above> } //ends switch Let me explain a bit. You include in the parenthesis your variable you want to perform the check on. If the value of that variable meets the case of 'A' or 'a' it will execute the code stated on the last condition if a case is blank. Just like you see with each condition, the code of case 'a' would also be executed if the variable's value was 'A' - Notice at the bottom is default: This is the code that will execute if none of the cases match the variable. Now after the code you want executed, on the next line is a break statement. The break statement will break the switch, or drop out of it and continue on with the rest of your program.
No Comments for this page. |
|
||||||||||||||||||||||||||||||