CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Contact Contact by Office hours 376 Joshi Mondays & Wednesday 3:00 pm – 4:00 pm
Get input from key board Scanner keyboard = new Scanner(System.in); keyboard is the given name to the variable here. You can use any name you like. Object typenameCreate new object and assign to name
Useful Arithmetic & Relational operators > greater than, = greater than or equal to, <= less than or equal to, etc. Example: 5 > 6 will output false. Number 5 is not greater than 6. Boolean operators && - logical AND || - logical OR Example: x==5 && y<5, this expression outputs true if and only if value of x is 5 and value of y is less than 5.
If statements Syntax if ( ) { runs if is true } else { runs if is false }
If else if … if( ) { your code here } else if( ) { your code here } else { your code here }
Print statements System.out.print() Prints a line and stays in the current line Sytem.out.println() Prints a line and goes to a new line System.out.printf(“ ”, ) Ex: System.out.printf(“%.2f \n", );
Pseudo Code Not a Java code or any programming language Human readable code similar to a programming language. Uses to describe algorithms. Example: Algorithm – if input value is greater than 5, out put to the console a success message. Otherwise exit program. Pseudo Code – Get user input value x. if x is greater than 5 then out put to console “Success” else exit from program You see that pseudo code has nothing to do with Java! More examples :
Flow Chart Condition Statement input true false
Example If then else
sum = 0 count = 1 REPEAT IF count is even THEN sum = sum + count count = count + 1 UNTIL count > 20 DISPLAY sum Pseudo code Flow chart
You can find useful information and helpful tips about flow charts at,