1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
1 nd Semester Sentinel Loops: example static void Main() { int N, SUM; int N, SUM; SUM = 0; SUM = 0; N = 0; N = 0; while (N != -1) { while (N != -1) { SUM = SUM+N; SUM = SUM+N; Console.Write( ” Enter number or -1 to quit”); Console.Write( ” Enter number or -1 to quit”); N = int.Parse(Console.ReadLine()); N = int.Parse(Console.ReadLine()); } Console.WriteLine(“The sum is {0}.”, SUM); Console.WriteLine(“The sum is {0}.”, SUM);} Console.Write( ” Enter number or -1 to quit”); N = int.Parse(Console.ReadLine()); Question 1 st Time (redundant part)
1 nd Semester Problem – Triangle shape static void Main() { int N=0, M=0; for ( ; ; ) { for ( ; ; ) { Console.Write(”*”); } Console.WriteLine(); } *** *** M = 3 M > 0 M-- QUIZ static void Main() { int N=0, M=0; for ( ; ; ) { for ( N=0 ; N<M ; N++ ) { Console.Write(”*”); } Console.WriteLine(); } *** ** * M = 3 M > 0 M-- N = 3 N >= MN >= MN >= MN >= M N--
1 nd Semester Introduction to Computer & Programming What is RAM and ROM ? What is main memory & secondary memory ? What is high-level language ? Which type of language C# is, machine, assembly, high-level ? = ?
1 nd Semester Program Structure namespace method1 method2 Class Class namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine(“Hello"); } namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine(“Hello"); } method3 method1 method2 Class namespace :::: Program structure in this semester
1 nd Semester Naming Rules (for Identifier) Letters(a-z, A-Z), digits(0-9) and underscores(_) First character letter or _ Up to 63 characters long Must not be a reserved word * Case Sensitive Example KU66 ≠ kU66 ≠ku66 KU66 ≠ kU66 ≠ ku66
1 nd Semester C# Reserved Words
1 nd Semester Variable Declaration Syntax: ; Example: We can also assign its initial value. Example: int radius; double area; int a,b,c; bool isokay; int k = 200; bool done = false; Variable must be declared before used
1 nd Semester Variable & Data Types TypeSizeDescriptionRange bool1 byteStore truth valuetrue / false char1 byteStore one charactercharacter code 0 – 255 byte1 byteStore positive integer0 – 255 short2 byteStore integer-32, ,767 int4 byteStore integer-2.1 x x 10 9 long8 byteStore integer-9.2 x x double16 byteStore real number± 5.0x ± 1.7x stringN/AStore sequence of characters N/A Variable must be declared before used
1 nd Semester Boolean expression Operators Comparison = Equal = != Not equal != < Less < > Greater > <= Less than or equal to <= >= Greater than or equal to >= Boolean && And && || Or || ! Not !
1 nd Semester Arithmetic expression Operators + - * / % (remainder after division) 11 % 2 1 //true if Y is even (Y%2 == 0) //true if Y is even //true if Y is even (Y%2 != 1) //true if Y is even Example (x > 12) && (x < 20) will be true if x is between 12 and 20 If x is 10, the above is false If x is 15, the above is true
1 nd Semester The Math Class The Math class inside the System namespace provides a large collection of math methods and constants Some examples: Method/ Constant Value returnedExample CallResult PI Value of Math.PI Max(x,y) Larger of the twoMath.Max(1,2)2 Abs(x) Absolute value of x Math.Abs(-1.3)1.3 Sqrt(x) Square-root of x Math.Sqrt(4.0)2.0 Round(x) Nearest integer to x Math.Round(0.8)1 Pow(x,y) xyxy Math.Pow(3,2)9.0 Log(x) Natural log of x Math.Log(10) Ceiling(x) Smallest integer greater than or equal to x Math.Ceiling(4.1)5 Cos(x) Cosine of x radiansMath.Cos(Math.PI)
1 nd Semester Incrementing Operators Operators ++ and -- can be used to increment and decrement a variable's value by 1, respectively Example: Stateme nt Description x++ Add 1 to the variable x x-- Subtract 1 to the variable x int n = 0; n++; // is equivalent to n = n+1, or n += 1 n++; // n is now 2 n--; // n is now 1 int n = 0; n++; // is equivalent to n = n+1, or n += 1 n++; // n is now 2 n--; // n is now 1
1 nd Semester Modify-And-Assign Operations Arithmetic operators can be combined with "=" to produce a modify-and-assign operation Ex. StatementDescription var += expression Increment var by the value of expression var -= expression Decrement var by the value of expression var *= expression Multiply var by the value of expression, then store the result in var var /= expression Divide var by the value of expression, then store the result in var sum += x; // is equivalent to sum = sum + x prod *= 2.5; // is equivalent to prod = prod * 2.5 y -= 3+a; // is equivalent to y = y – (3+a)
1 nd Semester Statements A statement is a unit of command to instruct your program A method consists of one or more statements class Hello { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } class Hello { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } Statement#1 Statement#2
1 nd Semester Statement Types Assignment Statement Input Statement Output Statement
1 nd Semester Assignment Statement Assigning value to variable Syntax: = ; = ; int Width,High; Width=10;High=5; int Width = 10; int High = 5;
1 nd Semester Input Statement Console.ReadLine() Return string Use to get the input from user Convert string to other data type int.Parse() Convert string to integer double.Parse() Convert string to double Example string st; st = System.Console.ReadLine();
1 nd Semester Example: Input Statement Ex1: string myname; myname = System.Console.ReadLine(); Ex2: int Width,High; string temp1; temp1 = System.Console.ReadLine(); Width = int.Parse(temp1); temp1 = System.Console.ReadLine(); High = int.Parse(temp1);
1 nd Semester Output Statements Use the method Write or WriteLine in the Console class (which is in System namespace) Basic usage: Advanced usage: Even more advanced usage: Console.WriteLine(”Size {0}x{1}”, width, height); double salary=12000; Console.WriteLine("My salary is {0:f2}.", salary); Console.WriteLine("Hello");Console.WriteLine(area);
1 nd Semester Control Structure if/else switch While / do while for nested if nested loop
1 nd Semester if statement ”condition”true Execute the specific statement when the ”condition” becomes true Syntax: if (condition) statement; //true statement; //true if (condition) { statement1; //true statement1; //true statement2; //true statement2; //true}
1 nd Semester if…else… statement conditiontrue statement1 If condition is true execute statement1 conditionfalse statement2 If condition is false execute statement2 Syntax: if (condition) statement1; //true statement1; //trueelse statement2; //false if (condition) statement1; //true statement1; //true else { statement2; //false statement2; //false statement3; //false }
1 nd Semester Nested if statement int N; N = int.Parse(Console.ReadLine()); if (N >= 0) { if (N==0) if (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is zero number”); else else Console.WriteLine(“N is positive number”); Console.WriteLine(“N is positive number”);}else Console.WriteLine(“N is negative number”); Console.WriteLine(“N is negative number”); if#1 if#2
1 nd Semester Nested if statement int N; N = int.Parse(Console.ReadLine()); if (N >= 0) { if (N==0) if (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is zero number”); else else Console.WriteLine(“N is positive number”); Console.WriteLine(“N is positive number”);}else Console.WriteLine(“N is negative number”); Console.WriteLine(“N is negative number”); if#1 if#2 QUIZ How to write this program without nested if ?
1 nd Semester Nested if statement int N; N = int.Parse(Console.ReadLine()); #1if (N >= 0) #2{ #3 if (N==0) #4 Console.WriteLine(“N is zero number”); #5 else #6 Console.WriteLine(“N is positive number”); #7} #8 else #9 Console.WriteLine(“N is negative number”); if#1 if#2 Answer if ((N>=0) && (N==0)) // or (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is zero number”); if ((N>=0) && !(N==0)) // or (N>0) Console.WriteLine(“N is positive number”); Console.WriteLine(“N is positive number”); if (N < 0) Console.WriteLine(“N is negative number”); Console.WriteLine(“N is negative number”);
1 nd Semester Switch-case example char op; Console.Write("Select + - / * :"); op=char.Parse(Console.ReadLine()); switch (op) { case '+': Console.Write("{0}+{1}={2}", x,y,x+y); break; case '-': Console.Write("{0}-{1}={2}", x,y,x-y); break; : default: Console.Write("Try again"); break; } must be int, char, string must be int, char, string int day_num; day_num= int.Parse(Console.ReadLine()); switch( day_num ) { case 1: Console.Write ("Sunday"); break; case 2: console.Write("Monday"); break; : default : Console.Write(“Try again"); break; } <expression> int version char version
1 nd Semester Switch-case example (continue) string op; Console.Write("Select + - / * :"); op=Console.ReadLine(); switch (op) { case “+”: Console.Write("{0}+{1}={2}", x,y,x+y); break; case “-”: Console.Write("{0}-{1}={2}", x,y,x-y); break; : default: Console.Write("Try again"); break; } must be int, char, string must be int, char, string string version <expression>
1 nd Semester Flowchart Symbols Overview Graphical representation Terminator Process Input/output Condition Connector Flow line
1 nd Semester Flowchart of if statement statement1 if (condition) statement2; //true statement2; //true else { statement3; //false statement3; //false} statement4; true false CONDITION START statement1 statement2 statement3 statement4
1 nd Semester Flowchart of Switch-case statement1 label int day_num; day_num= int.Parse(Console.ReadLine()); switch( day_num ) { case 1: Console.Write ("Sunday"); break; case 2: console.Write("Monday"); break; : default : Console.Write(“Try again"); break; } (day_num) label=1 label=2 default statement2 statement n (day_num=3) label=3 statement3 :::: (day_num=2) (day_num=1)
1 nd Semester While statement while (condition) statement; statement; while (condition) { statement-1; statement-1; statement-2; statement-2;... statement-N; statement-N;} For Multiple Statements For Single Statement
1 nd Semester How this partial code works? static void Main() { int N, SUM; int N, SUM; SUM = 0; SUM = 0; N = 0; N = 0; while (N >= 0) { while (N >= 0) { Console.Write(”Please input N: ”); Console.Write(”Please input N: ”); N = int.Parse(Console.ReadLine()); N = int.Parse(Console.ReadLine()); if (N>=0) if (N>=0) SUM = SUM+N; SUM = SUM+N; } Console.WriteLine(“SUM = {0}”, SUM); Console.WriteLine(“SUM = {0}”, SUM);} How to exit this loop?
1 nd Semester How this partial code works? static void Main() { int N, J; int N, J; J = int.Parse(Console.ReadLine()); J = int.Parse(Console.ReadLine()); N = 1; N = 1; while ( ) { while ( ) { if ( ) if ( ) Console.WriteLine(”{0}”, N); Console.WriteLine(”{0}”, N); }} N <= J N++; How to display odd number between 1 – J on screen? N%2== QUIZ Display only even number between 1 to J
1 nd Semester do…while statement do { statement; statement; } while (condition); do{ statement-1; statement-1; statement-2; statement-2;. statement-N; statement-N; } while (condition); For Multiple Statements For Single Statement
1 nd Semester Sentinel Loops: do…while static void Main() { int N, SUM; int N, SUM; SUM = 0; SUM = 0; N = 0; N = 0; do do { Console.Write( ” Enter number or -1 to quit”); Console.Write( ” Enter number or -1 to quit”); N = int.Parse(Console.ReadLine()); N = int.Parse(Console.ReadLine()); if (N!= -1) if (N!= -1) SUM = SUM+N; SUM = SUM+N; } while (N != -1); } while (N != -1); Console.WriteLine( ” The sum is {0}.”, SUM); Console.WriteLine( ” The sum is {0}.”, SUM);} How to rewrite with while loop?
1 nd Semester While / Do While WHILE (pre-checking) DO WHILE (post-checking)
1 nd Semester For statement for ( [initializers]; [expression]; [iterators] ) statement; statement; For Multiple Statements For Single Statement for ( [initializers]; [expression]; [iterators] ) { statement-1; statement-1; statement-2; statement-2;.. statement-N; statement-N;}
1 nd Semester For statement for ( [initializers]; [expression]; [iterators] ) statement; statement;STARTSTARTENDEND expressionexpression true false StatementStatement iteratorsiterators initializersinitializers
1 nd Semester Display 10 to 100 on screen static void Main() { int i; int i; for ( ; ; ) { for ( ; ; ) { Console.WriteLine(“{0}”, i); Console.WriteLine(“{0}”, i); }} static void Main() { int i; int i; i = 10; i = 10; while (i <= 100) { while (i <= 100) { Console.WriteLine(“{0}”, i); Console.WriteLine(“{0}”, i); i++; i++; }} while version for version i=10 ; i<=100 ; i++ QUIZ
1 nd Semester More Variations Display 0,2,4,...,20 Display 1,3,5,...,19 Display 15,12,9,...,0 for (i = 1; i <= 19; i += 2) Console.WriteLine(i); for (i = 1; i <= 19; i += 2) Console.WriteLine(i); for (i = 15; i >= 0; i -= 3) Console.WriteLine(i); for (i = 15; i >= 0; i -= 3) Console.WriteLine(i); for (i = 0; i <= 20; i += 2) Console.WriteLine(i); for (i = 0; i <= 20; i += 2) Console.WriteLine(i); Try !