Csc 201 Lecture 4

  • Uploaded by: pavanil
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Csc 201 Lecture 4 as PDF for free.

More details

  • Words: 978
  • Pages: 19
Lecture-4

Agenda • • • • • •

Quiz paper discussion Homework discussion Review Lecture Operators Control Flow Statements Example Programs

Homework package ifapp; import java.util.Scanner; import java.util.Random; public class Main { public static void main(String[] args) { // TODO code application logic here int even_num,limit=0, check = 0, n = 5; Random rand = new Random(); int myrand = rand.nextInt(n + 1); System.out.println("A Random number generated is:" +myrand); System.out.println("Enter the limit for printing even numbers:"); Scanner s = new Scanner(System.in); limit = s.nextInt(); // System.out.println("The generated even numbers are:"); for(int i = 1; i <= limit ; i++) { if(i%2==0) { System.out.println("The number " +i+ " is an even number"); if(i==myrand) {System.out.println("The number "+ i +" matches the random number:"+myrand); } } else {System.out.println("The number " +i+ " is not an even number"); } } } }

Primitive Java • • • • • • • •

Java source files are saved with .java extension. Javac compiles the program, generates .class files which contain bytecode. Java bytecode is interpreted by running the Java interpreter known as Virtual Machine. Using JIT compiler, the bytecode is converted to the system code as though the program had been compiled initially on that platform. Java Input Stream (System.in) usually keyboard input and output stream (System.out) usually display output. Java is case sensitive. Java is a strongly statically typed language : Means all variables must be declared first before they can be used! But they can be declared anywhere in the program, not at the first line! Static typing -> The type is checked at compile time.

Assignment Operators • • • • •



‘=‘ is basic assignment operator. ‘+=‘ adds value on right-hand-side of the += operator to the variable on left hand side. Ex : c += b; means c = c + b; ‘-=‘ operator. Ex: c = c – b; Similarly *= and /=. Unary operators : Require only 1 operand. unary minus ‘-’ = Ex : -x Autoincrement operator : ++ Autodecrement operator: -Prefix increment and postfix increment : ++j and j++. The value of of the expression is the new value of j (pre increment). In post increment, value of expression is the original value of j.

Post and Pre Increment Operator public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a = 12, b = 8, c = 8, res; res = a * ++b;// b is incremented, b becomes 9 and then multipled with a System.out.println("a is " + a + " b is: " + b + " res is : " + res); res = a * c++;// c is multiplied and then incremented System.out.println("a is " + a + " c is: " + c + " res is : " + res); } }

Loops While loop? For loop? Do-while loop? Why do loops fall into an infinite state?

Control Flow Statements -Review • If –else : decision making statements, which action to perform when. • Switch statement : decision-making statements based on integer expression. • While : The set of instructions keep on executing until the boolean condition returns false. • Do-while : same as while except the loop is executed atleast once. • For loop : initialization ; condition ; increment/decrement

Sample Programs 1) Write a ‘For’ loop to print numbers until 10. 2) Write a program to accept cost of items from the console. Once the user enters item cost as 0.0 add the cost of all items entered and display the sum to the user.

double item=0.0, sum=0.0; System.out.println("Enter the cost of items, please note enter item cost as 0.0 to get your total cost:"); Scanner s = new Scanner(System.in); while(true) { item = s.nextDouble(); sum = sum + item; if(item == 0.0) break; } System.out.println("Your total is:" +sum);

item = s.nextDouble(); while(item != 0.0) { sum = sum + item; item = s.nextDouble(); } System.out.println("Your total is:" +sum);

Control Flow Statements Nested-If statements : An If statement within an If statement. Ex : if(x > 0) if(y > 0) System.out.println(“case 1”); else System.out.println(“case 2”); What is the order of execution?when x = 5, y = 5? When x = -5, y = 5?

Control – Flow statements Nested-If-else statements If () stmt-1 Else if() <stmt2> else if() <stmt3> Good link for if-else structs! http://www.javacoffeebreak.com/books/extracts/javanotesv 3/c3/s5.html

For loops They are efficient if we know how many times you want to execute the group of statements. There are usually counters used in such loops. Ex: I, j etc for(int i=0 ; i < 10 ; i++) i is the counter used for counting the no.of times Loop runs

Sample Programs using Loops Computing the factorial of a number? Fact(n) = n * n-1 * n-2 ..1 5! = 5 * 4 * 3 * 2 * 1 for(int i = 2; i <= num; i++) { fact = fact * i;}

Factorial of n int num, fact=1; System.out.println("Enter a number to calculate factorial:"); Scanner s = new Scanner(System.in); num = s.nextInt(); fact = num; for(int i = num-1; i > 1; i--) { fact = fact * i; System.out.println(i);} System.out.println(fact);

Some Examples • For(int I = 10; I >= 0; i--) System.out.println(i); What is the o/p? • For(int I = 100; I >= 0 ; i-=10) System.out.println(i); What is the o/p here?

Empty statements Simply consists of a semicolon ‘;’ Ex: if(x < 0) { x = -x; }; Ex: for(int i=0 ; i<10 ; i++); System.out.println(“Hello”); How many times will Hello be printed?

Program to print a triangle of * int num; Scanner s = new Scanner(System.in); num = s.nextInt(); for(int i=1; i <= num ; i++)//for no.of rows { for(int j=1; j<= i ; j++)// to print no.of times i.e cols { System.out.print("*"); } System.out.println(" "); }

Related Documents

Csc 201 Lecture 4
June 2020 2
Csc 201-lecture 8
June 2020 1
Csc 201-lecture 7
June 2020 1
Csc 201-lecture 6
June 2020 1
Csc 201-lecture 14
July 2020 3

More Documents from ""

Csc 201-lecture 14
July 2020 3
Csc 201-lecture 7
June 2020 1
Csc 185-eigth
June 2020 2
Csc 201 Lecture 4
June 2020 2
Csc 185 Lab - 7
June 2020 1