1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.

Slides:



Advertisements
Similar presentations
2 nd Semester Selection Statement Computer and Programming (204111)
Advertisements

Selection Statement – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Implementing Control Logic in C# Svetlin Nakov Telerik Corporation
Thanachat Thanomkulabut
Introduction to C# Programming ณภัทร สักทอง Application Program Development.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
C# Programming: Expressions, conditions, and repetitions Computer and Programming.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Introduction to Computer and Programing Thanachat Thanomkulabut.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.
Input & Output Statement
Selection Statement – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012.
Selection Statement Thanachat Thanomkulabut. Outline 2  Boolean expression  if  if statement  nested if  nested if statement  switch case  switch.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
C# Programming: Basic Concept Part 2 Computer and Programming (204111)
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.
1 INPUT/OUTPUT Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Input/Output Statement Thanachat Thanomkulabut. Input Statement  Console.ReadLine()  Return string  Use to get the input from user  Convert string.
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 4 – C Program Control
Chapter Topics The Basics of a C++ Program Data Types
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Computing with C# and the .NET Framework
Thanachat Thanomkulabut
Chapter 4 – Control Structures Part 1
Chapter 3: Understanding C# Language Fundamentals
Basic Elements of C++ Chapter 2.
Objectives Explain selection constructs Describe loop constructs
DFC1023 PROBLEM SOLVING AND PROGRAM DESIGN
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I UTPA – Fall 2012 This set of slides is revised from lecture slides.
.Net Programming with C#
Advanced Programming Lecture 02: Introduction to C# Apps
Java Tokens & Data types
C# Programming.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 3 – Introduction to C# Programming
Module5 Looping Techniques: Part 2
Module5 Looping Techniques: Part 2
elementary programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Iteration (Loop) partII
Module 2 Variables, Data Types and Arithmetic
Presentation transcript:

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 !