Expression, Scanner Class and Control Structures

 

Expression:

A combination of variables, constants, and operators is called an expression.

 Arithmetic expressions:

An expression in which arithmetic operators are used is called arithmetic expression.

Examples: c=x+y;

Relational expressions: An expression in which relational operators are used is called relational expression

Examples: x <= y, x + y > 2

Logical expressions: An expression in which logical operators are used is called logical expression
Examples: x > y && x == 10, x == 10 || y == 5

 

Scanner class

In java by using the Scanner class, we can get the input from the user.

In order to use the scanner class we have to import it.

import.java.util.Scanner;

Scanner in = new Scanner(System.in);

Int a = in.nextInt();

Input types found in scanner class are listed below:

Method

Description

next()

Reads a word from the user

nextBoolean()

Reads a boolean value from the user

nextByte()

Reads a byte value from the user

nextDouble()

Reads a double value from the user

nextFloat()

Reads a float value from the user

nextInt()

Reads a int value from the user

nextLine()

Reads a String value from the user

nextLong()

Reads a long value from the user

nextShort()

Reads a short value from the user

 Example:

Here, is an simple program to display tables using scanner class.

import java.util.Scanner;



public class Tables {


public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner in = new Scanner(System.in);

System.out.print("Enter The Table : ");

int t = in.nextInt();

System.out.print("Enter The Limit : ");

int n = in.nextInt();

for(int i=1;i<=n;i++)

{

System.out.println(t + " x " + i + " = " + (t * i));

}


}


}


Output:

Enter The Table : 5

Enter The Limit : 5

5 x 1 = 5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25


Control Structures or Control Statements

         Program flow can be specified through the use of control structures.

There are three basic types of flow of control, known as:

     Sequence logic, or sequential flow

     Selection logic, or conditional flow

     Iteration logic, or repetitive flow

  1. Sequence Logic

As the name implies, sequential logic operates on a serial or sequential flow, where the computer's execution is dictated by the set of instructions it receives. 

2. Selection Logic (Branching)

Selection logic is only a set of rules that selects one written assertion from among multiple options.

3. Iteration Logic (Looping)

Repetition of a statement up to the point where the condition is met can be helpful. 

Post a Comment

0 Comments