+ All Categories
Home > Documents > CSC 201-Lecture 3

CSC 201-Lecture 3

Date post: 30-May-2018
Category:
Upload: pavanil
View: 219 times
Download: 0 times
Share this document with a friend

of 39

Transcript
  • 8/14/2019 CSC 201-Lecture 3

    1/39

    CSC 201

    Lecture - 3

  • 8/14/2019 CSC 201-Lecture 3

    2/39

    Agenda

    Review Lecture 2

    Type Casting

    Type Conversion Conditionals and Loops

    In-Class Quiz Tuesday : Sep 8th

    Next class : Book assignment will be givendue Tuesday Sep 8th.

  • 8/14/2019 CSC 201-Lecture 3

    3/39

    Review Of Lecture-2

    Intro to Java.

    Compiling/Executing Java programs

    JDK, JRE, JVM Data Types, Variable, constants

    Java program structure

    Type Conversion - Introduction

  • 8/14/2019 CSC 201-Lecture 3

    4/39

    Errors

    Every programming language has some set

    of rules to be followed like Grammar in

    English.

    2 kinds of errors : Compile-time and

    Runtime.

    Compile-time error : Syntax errors.

  • 8/14/2019 CSC 201-Lecture 3

    5/39

    Java program structure

    Consists of:

    Variables

    OperatorsExpressions, Statements and blocks

    Control flow statements

  • 8/14/2019 CSC 201-Lecture 3

    6/39

    Variables

    What is an identifier/variable?

    Rules for writing/creating an identifier.

    What happens if we give the foll.?int x = 10.0;

    What if I write int x = 10;

    (OR) int x;x = 10;

    How to declare a float variable in Java?

  • 8/14/2019 CSC 201-Lecture 3

    7/39

    Operators

    Arithmatic : +, -, *, /, %

    Unary : +, -, ++, --, !

    Boolean : Can have 2 values T or F. Conditional operators >=,

  • 8/14/2019 CSC 201-Lecture 3

    8/39

    Converting Strings to primitive

    value for cmd line args

    Rule: Whenever you use a + operator with

    string as one of its operands, Java

    automatically converts other to a string

    produces string as a result.

    Ex : string a = Hello;string b = csc;

    string c = 201;

    System.out.println(a + b + c);

    What is the output?

  • 8/14/2019 CSC 201-Lecture 3

    9/39

    Expressions,Statements and

    Blocks

    Expression is made of variables, operatorsand methods.

    Ex : int a = 0 (x+y)/100

    Statements : Expressions can be madeinto statements by terminating with ;

    Ex : a = a + 1;

    System.out.println(Hi); Blocks : Group of zero or more statements

    between balanced braces.

  • 8/14/2019 CSC 201-Lecture 3

    10/39

    Java Input and Output

    Input : is any information that is needed by

    your program to complete its execution.

    Output : Any information that we get from

    the computer.

    Many ways to accept input from console.

    One method : Scanner class. Java.util package contains some classes

    for I/O.

  • 8/14/2019 CSC 201-Lecture 3

    11/39

    Example

    import java.util.Scanner;

    public class Scanning {

    public static void main(String[] args) {

    System.out.println("Enter a integer:"); Scanner myscan = new Scanner(System.in);

    int i = myscan.nextInt();

    System.out.println("Value of i is : " +i);

    }

    }

  • 8/14/2019 CSC 201-Lecture 3

    12/39

    Type Conversion

    What is the importance of a data type?

    Java is a strongly typed language.

    Java is a statically typed language.

    In Java : Explicit type conversion, Explicit cast. Difference between Typecast and Type

    Conversion?

    If you cast a variable into a string you are onlytemporarily considering it as a string.

    Whereas with conversion you actually change itpermanently.

  • 8/14/2019 CSC 201-Lecture 3

    13/39

    Default Data type values

    Byte = 0

    Short = 0

    Int = 0

    Long = 0L Float = 0.0f

    Double = 0.0d

    Char = '\u0000 String (or any object) = null

    Boolean = false

  • 8/14/2019 CSC 201-Lecture 3

    14/39

    Type conversion and Typecasting

    Type conversion : Integer.parseInt()

    Double.parseDouble()

    Type Casting : Type conversion forprimitive types.

    Ex: (int) 2.71 = 2

    11 * (int) 0.3 = ?

  • 8/14/2019 CSC 201-Lecture 3

    15/39

    Java program structure

    Consists of:

    Variables

    OperatorsExpressions, Statements and blocks

    Control flow statementsDecision Making

    Loop statements

    Branching statements

  • 8/14/2019 CSC 201-Lecture 3

    16/39

    Booleans operators

    Boolean type has 2 values : True or False

    3 important bool operators : &&, ||, !

    If we have 2 bool values a, b we can have following values

    a && b is T ifBOTH operands are T

    F ifEITHER operand is F

    a || b is T ifEITHER operands are T

    F ifBOTH operands are F

    !a is T if a is F

    F if a is T

  • 8/14/2019 CSC 201-Lecture 3

    17/39

    Comparison operators

    ==, != , =

    These operators are defined for each primitive type and produce a booleanresult.

    Ex : Results for below?

    2 == 2

    2 == 3

    2 != 3

    2 != 2

    2 < 3

    2 > 3

    2 = 3If a and b are T, F

    13) Calculate (!(a && b) && (a || b))

    14) Calculate ((a && b) || !(a || b))

  • 8/14/2019 CSC 201-Lecture 3

    18/39

    Control Flow Statements

    Decision making statements : If, If-else,switch

    IF STATEMENT: If different action is to be

    performed on different inputs, we use IFstatement. It is basically a decision makingstatement. Its construct is :

    Form-1If ()

    { }

  • 8/14/2019 CSC 201-Lecture 3

    19/39

    Form 2 : if-else

    If else statement

    if()

    { ; }

    else()

    { ; }

  • 8/14/2019 CSC 201-Lecture 3

    20/39

    Form-3 : Nested If else

    if()

    ;

    else

    if() { ; }

    else

    if() { ; }

    ..

  • 8/14/2019 CSC 201-Lecture 3

    21/39

    Example If else

    Class test

    {

    Public static void main(String[] args)

    {

    int x = 2;

    if(x < 5){

    System.out.println( Number is less than 5.);

    }

    else

    System.out.println(Number is greater than or equal to 5.);}

    }

  • 8/14/2019 CSC 201-Lecture 3

    22/39

    Nested If-else

    int score;

    char grade;

    Scanner scan = new Scanner(System.in);

    score = scan.netInt();

    if(score >= 80)

    grade = A;else

    if(score >= 70)

    grade = B;

    else

    if(score >= 60)grade = C

    System.out.println(Grade is : +grade); o/p : Correct

    More than one statementis satisfied here!

    But once a condn is satisfied,

    Remaining are not evaluated.

  • 8/14/2019 CSC 201-Lecture 3

    23/39

    int score;

    char grade;

    Scanner scan = new Scanner(System.in);

    score = scan.nextInt();

    if(score >= 80)

    grade = A;else

    if(score >= 70)

    grade = B;

    else

    if(score >= 60)grade = C

    System.out.println(Grade is : +grade); O/P = ?

    Remove!

  • 8/14/2019 CSC 201-Lecture 3

    24/39

    Switch statement

    It is a shortcut to certain form of if-statement. If we have a stack of if

    statements, Java has a shorthand i.e

    switch-case statements.if (x == 0) dosomething();

    else if (x == 1) dosomethingelse();

    else if(x == 2) dosomethingelse();

    ..

  • 8/14/2019 CSC 201-Lecture 3

    25/39

    Syntax for switch statement

    Switch (x) //x must be variable/expression

    {

    Case 0: dosomething(); break;

    Case 1: dosomethingelse(); break;Case 2: dosomethingelse(); break;

    ..

    Case n: dosomethingelse(); break;Default : dosomethingelse();

    }

  • 8/14/2019 CSC 201-Lecture 3

    26/39

    switch(x) : x must be a variable or an expression that canbe cast to an int without loss of precision.

    Differences between if-else and switch

    An if-else can be used to make decisions on range ofvalues or conditions.

    A switch statement can make decisions only on integervalues.

  • 8/14/2019 CSC 201-Lecture 3

    27/39

    Switch Example

    Class switch_demo

    {

    public static void main(String[] args)

    {

    int x;

    System.out.println(Enter a number between 1 and 3 inclusive:);

    Scanner s = new Scanner(System.in);x = s.nextInt();

    switch(x)

    {

    case 1: System.out.println(You printed A); break;

    case 2: System.out.println(You printed B); break;

    case 3: System.out.println(You printed C); break;

    default: System.out.println(No letter is chosen);}

    }

    }// we can also write the same using the If else loop.

    Importance of

    Break?

  • 8/14/2019 CSC 201-Lecture 3

    28/39

    Looping Statements

    There are 3 basic forms of looping: while,

    for, do statements.

    While loop:

    Syntax

    while ( )

    { }The statements get executed until the

    boolean expression evaluates to False.

    The bool expression must

    evaluate to T for the

    Statements to execute

  • 8/14/2019 CSC 201-Lecture 3

    29/39

    While loop example

    int a = 0;

    while( a

  • 8/14/2019 CSC 201-Lecture 3

    30/39

    Example

    Class whiledemo

    {

    public static void main(String[] args)

    {

    int count = 1;while(count < 11)

    {

    System.out.println(Count is : + count);

    count++;}

    }

    }

  • 8/14/2019 CSC 201-Lecture 3

    31/39

    Do-While Loop

    Do-while statement: Same as While statement. Theexpression is evaluated at the bottom instead of top. So,loop will run atleast once.

    int count = 1;

    do{

    System.out.println(Count is: + count);

    count++;

    }while(count < 11);}

  • 8/14/2019 CSC 201-Lecture 3

    32/39

    For Loop

    If you want to initialize one or more variables

    and may want to increment one or more

    variable. The loop is repeatedly executed until a

    particular condition is satisfied.Syntax:

    For (initialization ; boolean-exp ; increment)

    {set of statements

    }If the expression evaluates to

    false., the loop is

    terminated

  • 8/14/2019 CSC 201-Lecture 3

    33/39

    Example

    Class for_demo

    {

    public static void main(String[] args)

    {

    for(int I = 1; I < 11; i++)

    {

    System.out.println(Count is : +i);

    }

    }

    }

    How to create an infinite loop with for?

    Invoked after eachIteration of the loop!

  • 8/14/2019 CSC 201-Lecture 3

    34/39

    Branching Statements

    Break statement : If in some scenario you

    want to exit the loop without completing

    the rest of the execution of statements

    Break was used in our switch statement.

    Continue statement : A continue statement

    returns to the beginning of the innermost

    enclosing loop w/o completing the rest ofthe statements of the loop.

  • 8/14/2019 CSC 201-Lecture 3

    35/39

    Continue statement example

    Public static void main(String[] args){

    int num;

    for(num = 1; num < 15; num++)

    {

    if(num >= 9){ System.out.println(The number +num+ is greater than or

    equal to 9!);}

    else

    continue;

    System.out.println(This statement executes:);}

    }//what is the output here?

  • 8/14/2019 CSC 201-Lecture 3

    36/39

    Infinite Loops

    What is an infinite loop?

    while(true)

    { }

    for(; ; )

    {} //infinite loop

    May be used in embedded control code which some may be designedto run forever.

    Sometimes used, when you dont know the exact condition under whichyou want to use a while, but you can break the loop by using breakas of its statements in the body.

  • 8/14/2019 CSC 201-Lecture 3

    37/39

    Program to check if a number is

    prime or notpublic static void main(String[] args){

    int num_to_check, mod, I;

    System.out.println(Enter a number to check if it is prime or not:);

    //check using scanner class

    for(I = 2; i< num_to_check ; i++)

    { mod = num_to_check % i;

    if(mod == 0)

    {

    System.out.println(Not a prime number);

    break;

    }

    }if(I == num_to_check)

    System.out.println(Prime Number!);

    }

  • 8/14/2019 CSC 201-Lecture 3

    38/39

    Announcements

    Homework 1 is due Thursday. It is posted

    on my blog.

    There will an In-class quiz on Tuesday

    Sep8th 2009.

    The prime number example will be posted

    with comments.

    Next lab session.

    Exam timings : 12:15 12:45

  • 8/14/2019 CSC 201-Lecture 3

    39/39

    Homework output sample run

    A random number generated is : 2

    Enter the limit for printing even numbers : 5

    The number 1 is not an even number

    The number 2 is an even nummber

    The number 2 matches the random number 2

    The number 3 is not an even number

    The number 4 is an even number

    The number 5 is not an even number


Recommended