Download presentation
Presentation is loading. Please wait.
Published byClyde Casey Modified over 8 years ago
1
1 nd Semester 2007 1 Module6 Review Thanawin Rakthanmanon Email: fengtwr@ku.ac.th Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
2
1 nd Semester 2007 2 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)
3
1 nd Semester 2007 3 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--
4
1 nd Semester 2007 4 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 ? 1000110 2 = ?
5
1 nd Semester 2007 5 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
6
1 nd Semester 2007 6 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
7
1 nd Semester 2007 7 C# Reserved Words
8
1 nd Semester 2007 8 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
9
1 nd Semester 2007 9 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,768 -- 32,767 int4 byteStore integer-2.1 x 10 9 -- 2.1 x 10 9 long8 byteStore integer-9.2 x 10 18 -- 9.2 x 10 18 double16 byteStore real number± 5.0x10 -324 -- ± 1.7x10 308 stringN/AStore sequence of characters N/A Variable must be declared before used
10
1 nd Semester 2007 10 Boolean expression Operators Comparison = Equal = != Not equal != < Less < > Greater > <= Less than or equal to <= >= Greater than or equal to >= Boolean && And && || Or || ! Not !
11
1 nd Semester 2007 11 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
12
1 nd Semester 2007 12 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.PI3.1415927 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)2.302585 Ceiling(x) Smallest integer greater than or equal to x Math.Ceiling(4.1)5 Cos(x) Cosine of x radiansMath.Cos(Math.PI)
13
1 nd Semester 2007 13 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
14
1 nd Semester 2007 14 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)
15
1 nd Semester 2007 15 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
16
1 nd Semester 2007 16 Statement Types Assignment Statement Input Statement Output Statement
17
1 nd Semester 2007 17 Assignment Statement Assigning value to variable Syntax: = ; = ; int Width,High; Width=10;High=5; int Width = 10; int High = 5;
18
1 nd Semester 2007 18 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();
19
1 nd Semester 2007 19 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);
20
1 nd Semester 2007 20 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);
21
1 nd Semester 2007 21 Control Structure if/else switch While / do while for nested if nested loop
22
1 nd Semester 2007 22 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}
23
1 nd Semester 2007 23 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 }
24
1 nd Semester 2007 24 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
25
1 nd Semester 2007 25 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 ?
26
1 nd Semester 2007 26 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”);
27
1 nd Semester 2007 27 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
28
1 nd Semester 2007 28 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>
29
1 nd Semester 2007 29 Flowchart Symbols Overview Graphical representation Terminator Process Input/output Condition Connector Flow line
30
1 nd Semester 2007 30 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
31
1 nd Semester 2007 31 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)
32
1 nd Semester 2007 32 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
33
1 nd Semester 2007 33 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?
34
1 nd Semester 2007 34 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==0 2 4 6 8... QUIZ Display only even number between 1 to J
35
1 nd Semester 2007 35 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
36
1 nd Semester 2007 36 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?
37
1 nd Semester 2007 37 While / Do While WHILE (pre-checking) DO WHILE (post-checking)
38
1 nd Semester 2007 38 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;}
39
1 nd Semester 2007 39 For statement for ( [initializers]; [expression]; [iterators] ) statement; statement;STARTSTARTENDEND expressionexpression true false StatementStatement iteratorsiterators initializersinitializers
40
1 nd Semester 2007 40 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
41
1 nd Semester 2007 41 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 !
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.