ECE 114-4 Control Statements-2 Dr. Z. Aliyazicioglu Cal Poly Pomona Electrical & Computer Engineering Cal Poly Pomona Electrical & Computer Engineering 1 Assignment Operations C++ provides several assignment operators Example: c=c+3; Can be also written a: c +=3; Similar: c=c*3; c *=3; c=c/3; c /=3; c=c%3; c %=3; c=c-3; c -=3; Cal Poly Pomona ECE 114-4 2 1
Increment and Decrement Operator Operator ++ ++ -- -- Called Prefix increment Postfix increment Prefix decrement Postfix decrement Sample Exp. ++a a++ --a a-- Example: a=3; b=5; x=a++ - b; a=3; b=5; x=a++ - --b; a=3; b=5; x=a++ - b++; x=-2, a=4 b=4,x=-1,a=4 X=-2,a=4,b=6 Cal Poly Pomona ECE 114-4 3 Example // Counter-controlled repetition #include "stdafx.h" #using <mscorlib.dll> using namespace System; int _tmain() int counter = 1; // initialization while ( counter <= 10 ) // repetition condition Console::WriteLine( S 0, counter.tostring() ); ++counter; // increment return 0; counter=counter+1; Cal Poly Pomona ECE 114-4 4 2
Precedence of the Operators Operators :: () ++ -- static_cast<type>() ++ -- + - * / % + - < <= > >= ==!=?: = += -= /= %= Associatively Right to left Right to left Right to left Type Scope resolution Parenthesis Unary postfix Unary cast Unary prefix Multiplicative Additive Relational Equality Condition Assignment Cal Poly Pomona ECE 114-4 5 auto Standard C++ Keywords bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum explicit extern float for friend goto if inline int long mutable namespace new operator private protected public register rinterpret_cast return short signed sizeof static static_cast struct switch template this throw try typedef typeid typename union unsigned using virtual void volatile wchar_t while Cal Poly Pomona ECE 114-4 6 3
_alignof _declspec _except _identifier _int16 _m64 naked novtable stdcall _unhook Microsoft specific keywords for C++ _asm deprecated _fastcall _if_exitsts _int32 _m128 noinline property _super uuid _assume dllexport _finally _if_not_exists _int64 _m128d _noop _raise thread _uuidof _based dllimport _forceinline _inline _interface _m128i noreturn selectany _try/_expec t _virtualinheritence _cdecl _event _hook int8 _leave _multiple_inhe ritence nothrow _single_inherit ence _try/_finally _w64 _wchar_t Cal Poly Pomona ECE 114-4 7 MC++ keywords (also Microsoft specific) _abstarct _nogc _try_cast _box _pin _delegate _property _gc _sealed Cal Poly Pomona ECE 114-4 8 4
Questions 1. If the originally x=3, y=2, and z=4, what is the value of each of the following expressions? a. a = z - (x + z) % 2 + 4 ; b. b = ++x * y++ - z ; c. x += y *=z -= 2 ; 2. If x=3, y=1, and z=-1, what is the value of the following expressions: a. x && y z && 0 b. x - y < z + 3 c.!(x <= y) d. x 2 * y + y < z * 2 / 3 Cal Poly Pomona ECE 114-4 9 Questions 3. Determine the values of each variable after the calculation is performed. (at the beginning all variables have the integer value 5) a. product *= x++; b. Quotient /= ++x; 4. Identfy and correct the errors in each of the following While (c<=5) product *= c; ++c; If (gender == 1) Console::WriteLine( S Man ); Else; Console::WriteLine( S Man ); Cal Poly Pomona ECE 114-4 10 5
Questions 5. What is the output of the following program #include "stdafx.h" #using <mscorlib.dll> using namespace System; int _tmain() int sum, x; x=1; sum=0; while (x<=10) sum +=x; x++; Console::WriteLine( S The sum is 0,sum.ToString() ); return 0; Cal Poly Pomona ECE 114-4 11 Flow Control for if and while statements if (test) statements; next statements; while (test) statements; next statements; test True test True False Statements; False Statements; Next Statements; Next Statements; Cal Poly Pomona ECE 114-4 12 6
The for Repetition Statement The for repetition handles all the detail of the countercontrolled repetition. for (counter initial value; condition; increment counter) for (int counter=1; counter <=10; counter++) for (exp-1; exp2; exp3) Statements; Reminder exp1; while (exp2) statements; exp3; Cal Poly Pomona ECE 114-4 13 Examples: // Counter-controlled repetition with the for structure #include "stdafx.h" #using <mscorlib.dll> using namespace System; int_tmain() // Initialization, repetition condition, and incrementing for ( int counter = 1; counter <= 10; counter++ ) Console::WriteLine( S 0, counter.tostring() ); return 0; for loop Cal Poly Pomona ECE 114-4 14 7
Example // Summation #include "stdafx.h" #using <mscorlib.dll> using namespace System; int _tmain() int sum=0; for ( int n = 1; n <= 10; n+=2 ) sum+=number; Console::WriteLine( S Sum is 0,sum.ToString() ); return 0; Cal Poly Pomona ECE 114-4 15 Example of for structure for (int i=1; i <=100; i++) for (int i=100; i >=1; i--) for (int i=10; i <=70; i+=10) for (int i=20; i >=5; i-=2) Counter =1; test True Counter++; Statements; False Next Statements; Cal Poly Pomona ECE 114-4 16 8
The switch Multiple-selection Statement The switch structure consists of a series of case labels, and an optional default case switch (x) case 1: y=2+z; case 2: y=z-2; break; case 4: y=2; break; default: y=0; Cal Poly Pomona ECE 114-4 17 The do while Structure The do while loop structure is similar to while structure. In the do/while structure the loopcondition is not executed until after the action is performed at least once. do statements; while (condition); Example x=0; do y=z+x; x+=2; while (x < 20); Cal Poly Pomona ECE 114-4 18 9
The break and continue Statement The break and continue statements after the flow of control. The break statement, when executed in a while, for, do while, or switch structure, causes immediate exit from structure. The continue statement, when executed in a while, for, or do/while structure, skip the remaining statements in the body of that structure, and proceeds with the next iteration of the loop. Cal Poly Pomona ECE 114-4 19 #include "stdafx.h" #using <mscorlib.dll> using namespace System; Example: int _tmain() int x; // x declared here so it can be used after the loop for ( x = 1; x <= 10; x++ ) if ( x == 5 ) break; // skip remaining code in loop if x is 5 Console::Write( String::Concat.ToString(), S ) ); Console::WriteLine( S \nbroke out of loop at x =0, x.tostring() ); return 0; Cal Poly Pomona ECE 114-4 20 10
Logical Operators MS C++ provides logical operators that are used more complex conditions by combining simple conditions: The logical operators: && (and), (or) and,! (NOT) Example: if (gender ==1 && age >=65) ++ seniorfemales; Cal Poly Pomona ECE 114-4 21 Logical Operators Truth Table for the && Exp1 Exp2 Exp1 && Exp2 Truth Table for the Exp1 Exp2 Exp1 Exp2 Cal Poly Pomona ECE 114-4 22 11
Logical Operators C++ provides the! Operator to enable a programmer to reverse the meaning of the condition. Example: if (!(x==y)) ConsoleWriteLine( S x is not equal to y ); Exp.! Exp. Cal Poly Pomona ECE 114-4 23 Logical Operators!(x==y)!(x!=y)!(x>y)!(x>=y)!(x<y)!(x<=y) x!=y X==y x<=y x<y x>=y x>y Cal Poly Pomona ECE 114-4 24 12