/*********************************************************** * C++ Phile * * Expressions && Statements * * * * By Infinite_Reality aka `uN[In$tall]` * * * * April, 2004 * ***********************************************************/ /* Contents of Phile * * * 1-- Statements * -- What are statements? * -- Whitespace * -- Simple statements * -- Compound statements * * * 2-- Expressions * -- What are expressions? * * * 3-- Operators * -- What are operators? * -- Assignment operator * -- Mathematical operators * -- Combining assignment and mathematical operators * * * 4-- Increment and Decrement * -- What is increment? * -- What is decrement? * -- Prefix and postfix increment/decrement * * * 5-- Precedence * -- What is precedence? * -- Using parentheses * * * 6-- Relational Operators * -- What are relational operators? * -- List of relational operators * -- Equals, not equals * -- Greater than, less than * -- Great than or equal to, less than or equal to * * * 7-- IF, ELSE * -- If statement * -- Else clause * -- Advanced If statements [nesting] * -- Using braces in nested If statements * * * 8-- Logical Operators * -- What are logical operators? * -- AND * -- OR * -- NOT * * * 9-- Credit0rz * * */ |==================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1) STATEMENTS -==- => What are Statements? A statement in C++ controls the sequence of execution, evaluates an expression, or does shit all [ie: null]. All statements end with a semicolon [;]. => Whitespace-- Whitespace is exactly that: whitespace in your source code. It is made up by spaces, tabs, and new lines [return/enter]. Whitespace is ignored by the compiler, and is usually only used to make source code easier to read by us non-computer humans. For example: [code] x = a + b; [/code] is the same as [code] x = a + b ; [/code] They are both legitimate codes according to the compiler, but which one is easier for you to read? When using whitespace, don't be a "whitespace wally" and do the second example. Be sensible, k. => Simple statements-- [code] x= a + b; [/code] Simple enough? ;) That is a simple statement. It is as simple as can be. But something you might want to know: don't think of this statement algebraically. Don't confuse this simple statement with what you would do in algebra class, this does not read "x equals the sum of 'a' and 'b'". Instead, this _should_ be interpreted as "assign the value of (a + b) to 'x'", or "assign to 'x', (a + b)". They appear the same thing, algebraically and C++-lly, but they're not when you start using them in complex situations. The right hand side is assigned to the left hand side, always. '=' does not mean "equals", but is the C++ symbol for the 'assignment operator', and thus does completely different things [read my first guide, it covers 'assignment' well enough]. => Compound statements-- A compound statement is just a complex simple statement [as in English, a sentence is a complex clause]. A compound statement is written between braces [ { compound statement here } ]. Every statement in a compound statement must end with a semicolon [as do all statements [;]], the compound statement itself does not. For example: [code] { x = a; a = b; b = x; } [/code] Basically, it's swapping values around from 'x' to 'a', 'a' to 'b', and 'b' to 'x', or 'x = a = b = x'. A big circle of swapping values. Not hard for a first example, eh? Good. |==================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 2) Expressions -==- => What are expressions? Expressions are any statement that returns a value. All expressions are therefore statements [simple and compound]. Neato huh? For example: [code] 3.2 float PI = 3.14; SecondsPerMinute [/code] Are all expressions, as they return a value [assuming SecondsPerMinute is assigned to 60]. [code] x = a + b; [/code] is also an expression, and thus can be on the right side of the assignment operator [r-value]. You'll read about operators below. |==================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 3) Operators -==- => What are operators? An operator is a person that sits on their arse and answers people all day. Dial 0 to talk to yours. An operator in C++ is slightly different--it is a symbol that causes the compiler to take an action in a program. => Assignment operator-- [=] This causes the operand on the left side of the assignment operator to have its value changed to the value on the right side. [NOTE: Operand is a mathematical term referring to the part of an expression operated upon by an operator. Savvy?] Anything on the left side of the assignment operator is called an l-value. Coincidentally, anything on the right side is called an r-value. Wowies. Constants are always r-values, and l-values are never constants. [code] x = 35; // 35 is the r-value, constant. 35 = x; // this would never work, because the r-value is on the left side, and that is not allowed. [/code] All l-values are r-values, but not all r-values are l-values. An example of an r-value that is not an l-value is a literal. For example, you can write x = 5;, but not 5 = x;. => Mathematical operators-- There are five [5] mathematical operators in C++ that you should know about: o addition [+] o subtraction [-] o division [/] o mulitplication [*] o modulus [%] I think the only ones needing some explanation are division and modulus; the rest should be already used in basic maths. Divsion of integers in C++ is not like everyday division. When you divide 21 into 4 [21 / 4], the answer is a real number [numbers that have a fraction]. In this case, 21 / 4 results in 5.25, or five [5] remainder one [1]. Integers in C++ don't have fractions, or remainders, so when dividing in C++, the remainder is automatically "gone". If you used 21 / 4 in C++, you'd simply get 5. This is where modulus comes in: the modules operator [%] returns the remainder value of the integer division. So, 21 % 4 is shown with a remainder of 1. This is pronounced "twenty-one modulo four", and the reult of using the modulo operator is the modulus. Thus, the remainder 1 is the modulus. When you have unevenly divisible numbers such as 21 / 4, you use the two operators in sync, for example: [code] 21 / 4 = 5 //no remainder 21 % 4 = 1 //is the remainder Therefore, 21 divided by 4 is equal to 5 remainder 1 :) [/code] => Combining assignment and mathematicall operators-- This will pretty much just show you how you can add a value to a variable, and then have the resulting value assigned back into the original variable. I'll be using the variable Age, value of 18. [code] int Age = 18; // declares Age an integer variable with starting value of 18 int temp_; // declares temp_ an integer variable, non-decimal temp_ = Age + 2; // adds 2 to Age [18 + 2], and assigns it back into temp_ Age = temp_; // reassignes Age with the value of temp_, which was 18 + 2, or 20 [/code] Makes sense? But this way is bulky and icky, not to mention wasteful. So to do that easier, we'll put the variable on the same side of the assignment operator :) For example: [code] Age = Age + 2; [/code] Much easier, yeh? This is much better than the previous way. In algebra this equation would be meaningless, but in C++ it is read as "add two to the value of Age, and then assign the result back into Age". Make it even simpler to write? For example: [code] Age += 2; [/code] :D Even simpler still. This equation, however, introduces something I haven't explained yet: the self-assigned operator. In this case, it's the self-assigned addition operator [+=]. This operator adds the r-value to the l-value, and then reassigns the result into the l-value; same as all the other ways, only simpler. It is read as "Age plus-equals two". If Age had a value of 18 to start with, it would equal 20 after doing this one time. Make sense? Other self-assigned operators are: o subtraction [-=] o division [/=] o multiplication [*=] o modulus [%=] |==================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 4) Increment and Decrement -==- => What is increment? Increasing a value by 1 is called incrementing. The increment operator is [++], double-plus signs. For example: [code] // Take C, and increase it by 1: C++; // is also known as... C = C + 1; // ...which is also known as... C += 1; // ...follow? [/code] => What is decrement? Decreasing a value by 1 is called decrementing The decrement operator is [--], double-minus signs. Amazing, huh? => Prefix and postfix increment/decrement-- The increment and decrement operators both come in two ways: prefix and postfix. As the names suggest, the prefix variety is written before the variable name [++Age, --Age], and the postfix is written after [Age++, Age--]. This is mostly only worried about when using more complex statements when you are incrementing or decrementing a variable and then assigning the result to another variable. The prefix operator is evaluated BEFORE the assignment, the postfix is evaluated AFTER the assignment. The semantics of the prefix operator is this: increment the value, then fetch it. Obviously the postfix is going to be different: fetch the value, then increment the original. As confusing as it can be at first, it gets easier with practice. For example: [code] int a = 5; int x = ++a; [/code] That is an example of prefix incrementing. It tells the compiler to increment a and fetch the value and assign it to x. So a = 5 is now a = 6 after the increment of 1 [++a], and then x = 6 because you assigned a's value to it. For example: [code] int a = 5; int x = a++; [/code] This is now postfix increment. It doesn't do as the first did, but instead tells the compiler to fetch the value of a and assign it to b, and then go back and increment a. So x = 5, because you assign the value of a to x, and then the statement goes back and increments a 1 value, so a now = 6. The result is a = 6, and x = 5. See why I said that a = x does not mean "a is equal to x", but rather "assign the value of x to a" [back at the start]? |==================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 5) Precedence -==- => What is precedence? Legally, precedence is a case that has already happened that may be used as an example to persuade in a trial, but in C++ it is the order of execution. In mathematics, it is known as the "Order of Operations", and consists of things similar to B.I.D.M.A.S or B.O.D.M.A.S, which stand for "Brackets, Indices, Division, Multiplication, Addition and Subtraction". Precedence is the order in which things happen in a program, such as mulitplication before addition. For example: [code] myHeight_Cm = 160 + 10 * 3 [/code] As in mathemtics you do in C++ with this equation--you multiply then add, because multiplication comes before addition. The result to this equation would then be (160 + (10 * 3)) = (160 + (30)) = (190), NOT ((160 + 10) * 3) = ((170) * 3) = (510). Basically, precedence is done just as in mathematics, so if you keep to the rules you learn in mathematics you should be fine--group similarities, follow the BIDMAS/BODMAS rules, etc etc. I'll write a list of Precedence in an update at a later time. => Using parentheses-- Alos known as "nesting" parentheses. With complex expressions you may need to next parentheses, setting one pair within another. For example: [code] TotalPersonSeconds = (((NumMinutesToThink + NumMinutesToType) * 60 ) * (PeopleInTheOffice + PeopleOnVacation)) [/code] Just like in mathematics, you read form the inside out. You add NumMinutesToThink and NumMinutesToType, take the total and multiply by 60. Find the total of PeopleInTheOffice and PeopleOnVacation and then multiply that by the total of the previous equation. Make sense, working form the inside out? Just like using Algebra, only each string would have been defined earlier on. That is using Parentheses, and it's very easy. |==================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 6) Relational Operators -==- => What are relational operators? These are operators used to determine whether two numbers are equal, greater than, less than, or not equal to each other. Every relational expression returns either 1 or 0: true, or false. => List of relational operators-- [code] Name Operator -------------------------------------- Equals == Not Equals != Greater Than > Less Than < Great Than, or Equal To >= Less Than, or Equal To <= [/code] => Equals, not equals-- If the integer variable for SonAge has the value of 19, and the integer variable FatherAge has the value of 40, we can determine if they are equal by using the relational "equal" operator. For example: [code] SonAge == FatherAge; // is the value of SonAge equal to FatherAge? [/code] This expression evaluates to 0 [false], because the variables do not equal; 19 does not equal 40. To make it evaluate to 1 [true], we would have to change the operator sign. For example: [code] SonAge != FatherAge; // is the value of SonAge not equal to FatherAge? [/code] Thus by changing == [equal] to != [not equal], we change the expression and result in true, as 19 does not equal 40. NOTE: Do not confuse [=] with [==]. They are not the same operator sign: = is the assignment operator, == is the equals relational operator. => Greater than, less than-- Again, this is the same idea as in anything above algebra mathematics. For example: [code] SonAge < FatherAge; // is the value of SonAge less than the value of FatherAge? [/code] What do you think? Is 19 less than 40? Yes, it is, and thus the expressions is true. Switching the < [less than] with a > [greater than] will make the expression false, or 0, because 19 is not greater than 40. => Greater than or equal to, less than or equal to Ok, getting repetitive now, but this is the same as in mathematics, too. For example: [code] SonAge >= FatherAge; // is the value of SonAge greater than, or equal to the value of FatherAge? [/code] False [0]. Obviously 19 is not greater than OR equal to 40. Thus, the expression is false. For example: [code] SonAge <= FatherAge; // is the value of SonAge less than, or equal to the value of FatherAge? [/code] Yes, it is! 19 is less than or equal to 40. Any number under 41 would be true in this expression. I hope this is abundantly clear now, I am sick of these already. |==================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 7) IF, ELSE -==- => If statement-- The program will flow along line by line as it appears in the source. It is the job of the If statement to test for a condition, that is, whether two variables are equal, and then branch off to different parts of the code depending on the result. The If statement is generally along these lines: For example: [code] if (expression) statement; [/code] The expression in the parentheses can be anyting at all, but usually contains one of the relational expressions. If the expression has the value of zer0, it is considered false, and the statement is thus skipped. Any expression that is evaluated mathematically to zer0 will return false; everything will return true. If it has any non-zer0 value, it will be considered true and the statement executed. For example: [code] if (hugearseNumber > weaksmallNumber) hugearseNumber = weaksmallNumber; [/code] The first line comapres hugearseNumber to weaksmallNumber. If hugearseNumber is larger, the second line assigns its value to that of weaksmallNumber. => Else clause The else clause will allow the If statement to branch off; one for true, another if it is false. For example: [code] if (expression) statement; else statement; [/code] Easy enough to read? So let's take some source code from a simple If, Else input program: [code] 0: #include 1: 2: int main(); 3: { 4: int numOne, numTwo; 5: 6: cout << "Please enter a huge arse number: " << endl; 7: cin >> numOne; 8: cout << "Please enter a weak small number: " << endl; 9: cin >> numTwo; 10: if (numOne > numTwo) 11: cout << "\n Thanks dude.\n"; 12: else 13: cout << "\nShit son, you did it wrong. The second number is bigger =(\n"; 14: 15: return 0; 16: } [/code] 4: declares numOne and numTwo as integer variables, where the user will input a value later on 7: is the first user input for numOne 9: is the second user input, this time for numTwo 10: is where the If statement is evaluated. If the condition is true, numOne is greater than numTwo, then the statement on line 11: is run. If it it is false, and numOne is not greater than numTwo, then the code moves to line 12: 12: is the branch off of the If statement. It is this line that will be seen if numOne is not greater than numTwo Remember: the statements can be either simple, or compound in braces. => Advanced If statements [nesting]-- To get confusing, you can also use an If statement and Else clause inside a pre-existing If statement or Else clause. These are complex If statements, and may look like: For example: [code] if (expression1) { if (expression2) statement1; else { if (expression3) statement2; else statement3; } } else statement4; [/code] This large and obnoxious If statement basically says: If expression1 is true, and expression2 is true, execute statement1. If expression1 is true but expression2 is false, then If expression3 is true, execute statement2. If expression1 is true but expression2 and expression3 are false, execute statement3. If expression1 is false, execute statement4. See why they're called complex If statements? Maybe a real example will help deaden the confusion: [code] 0: #include 1: 2: int main(); 3: { 4: /****** Goals: *************************************************** 5: * Ask for two numbers * 6: * Assign the numbers to bigNum and lilNum * 7: * If bigNum is bigger than lilNum, see if they are evenly divisible * 8: * If they are, see if they are the same number * 9: ************************************************************************/ 10: 11: int bigNum, lilNum; 12: cout << "Enter two numbers: \nBigger number: "; << endl; 13: cin >> bigNum; 14: cout << "Smaller number: " << endl; 15: cin >> lilNum; 16: cout << "\n\n"; 17: 18: if (bigNum >= lilNum) 19: { 20: if (bigNum % lilNum) == 0); //checks for even divisibility 21: { 22: if (bigNum == lilNum) 23: cout << "They are the same...\n"; 24: else 25: cout << "They are evenly divisible...\n"; 26: } 27: else 28: cout << "No, they are not evenly divisible...\n"; 29: } 30: else 31: cout << "Uh, dumbarse, you put the smaller number bigger than the bigger number...\n"; 32: 33: return 0; 33: } [/code] o Two numbers are input by the user, and then compared in the first If statement to see if bigNum is greater than, or equal to lilNum. If it is not, then the Else clause on line 30 is executed and the rest is skipped. o IF the first If statement is true, the block of code beginning on line 19 is executed, and the second If statement on line 20 is evaluated. This checks to see if bigNum modulo lilNum is evenly divided, that is, no remainder. If this is true, then line 22 is executed and checks for equality of the two numbers, and then displays the appropriate message for either result. o If the If statement on line 20 fails, the Else statement on line 27 is executed. Still following? =] => Using braces in nested If statements-- It is ok to leave out the braces [{ and }] on If statements that are only a single statement, and it's ok to nest If statements as I showed above; however, when you are writing large nested statements, this type of practice can cause lots of unnecessary confusion. Remember, whitespace and indentation are a convenience for the programmer and readers of the source code. It is easy to cofuse the logic and inadvertantly assign an else to the wrong If statement with all the nesting. Basically, just try to keep things in clear vision, and don't use large, confusing, elaborate nested If statements and Else clauses. Last thing you want to do is make a simple mistake with one Else clause that ruins the entire program. One more example of something you can do with nesting: [code] if (x > y) // if x is bigger than y if (x < z ) // and if x is smaller than z x = y; // ten set x to the value in y [/code] |==================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 8) Logical Operators -==- => What are logical operators? Logical operators are asked when you want to ask more than one relational question at a time, such as "Is it true that x is great than y,and also true that z is greater than z?". A program might need to determine both of these conditions are true, or that some other condition is true, in order to take an action. Using a metaphor I read somewhere: Imagine a new alarm system in a house, that has the logic, "If the door alarm sounds AND it is after 6:00pm AND it is NOT a holiday, OR if it is a weekend, then call the police". The three logical operators of C++ are used to make thi kind of evaluation of action. The operators are: [code] Operator Symbol Example ------------------------------------------------------------- AND && expression1 && expression2 OR || expression1 || expression2 NOT ! !expression [/code] => AND-- A logical AND statement evaluates two expressions, and if both expressionsa are true, the logical AND statement is true as well. If it is true that you need to gasoline, AND you have money, THEN it is true you can fill up the car. For example: [code] if ((x == 5) && (y == 5)) [/code] would evaluate of both x and y are equal to 5, and it would evaluate false if either one is not equal to 5. Both sides must be true for the entire expression to be true. => OR A logical OR statement evaluates two expressions, too. If either one is true then the expression is true. If you have money OR a credit card, THEN you can pay the price. YOu don't need both money and a credit card, only one or the other (although having both would be better :P). For example: [code] if ((x == 5) || (y == 5)) [/code] evaluates true if either x or y is equal to 5, or if both are equal to 5. If x is equal to 5, the compiler won't even bother to check on y. => NOT The logical NOT statement evaluates true if the expression being tested is false. Again, if the expression being tested is false, the value of the test is true :D Hehe. For example: [code] if (!(x == 5)) [/code] is true only if x is NOT equal to 5. This is exactly the same as writing [code] if (x != 5) [/code] |==================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 9) Credit0rz -==- Shouts: Shoutouts go to all The Goon Squad (TGS) members-- Raven, Mind, NoUse, Knightmare, Enjoi; and to all the staff at HackersCenter (Hs'C)-- VbFavre69, Zinho, Digital_Spirit, sync. Good people, all of them. To all my other mates, Subby, wkd, asTHma, sn4k3, bread/g0ra, Kevin, Dyndrilliac, $GX, Tesla, D@MN3D, Raab_Himself, and to all the people at Hackerlounge.com, Brain-hack.org, Anomalous-security.org, and Hs'C.com forums. Info: Infinite, aka `uN[In$tall]` irc.brain-hack.org #hackerlounge http://www.hackerlounge.com http://www.tgs-security.com http://www.Hackerscenter.com http://www.Brain-hack.org