1 basic control structures overview l relational and logical operations l selection structures »if...

22
1 Basic control structures Overview Relational and Logical Operations Selection structures » if statement » switch statement Preview:

Post on 21-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

1

Basic control structures

Overview

Relational and Logical Operations

Selection structures

»if statement

»switch statement

Preview:

2

Basic control structures

Selection structures» if statement» switch statement

Repetition structures» while statement» for statement» do statement

Flow breaking statements» break» continue» return

Exception handling stuctures» try { } catch ( ) { }

3

Selection structures

» if statement

– Syntax

– Interpretation

– Flow diagram for if statement

– Example

» if else statement

– Syntax

– Interpretation

– Flow diagram for if else statement

– Example

» Group of statements

» Nested if statement

» switch statement

– Syntax

– Interpretation

– Example

4

Relational Operators

To find whether a student has passed ICS102, we compare the student mark with the pass mark score.

For comparing these 2 marks, we use relational operators such as >

Comparing two data items using relational operators is called relational operation.

Expressions containing operands operated with relational operators are called relational expressions.

A relational expression will result in either true or false, with regard to the operand values

Example : Given that

stuMark = 80

passMark = 60

To check for pass, the relational expression is :

stuMark >= passMark

Here >= is a relational operator.

This expression will result in true, for those values of stuMark which are greater than or equal to the value of passMark

5

Relational Operators

Operator Use Return true if

> op1 > op2 op1 is greater than op2

>= op1 >= op2 op1 is not

less than op2

< op1 < op2 op1 is less than op2

<= op1 <= op2 op1 is less than or

equal to op2

== op1 == op2 op1 and op2 are equal

!= op1 != op2 op1 and op2 are

not equal

Examples :

if (a > b) statement1

If (x == y) statement2

6

Logical Operators

In reality, to pass ICS102, A pass in both the lecture and lab. components is

required.Pass mark in the lecture component is 60 out of 75Pass mark in the Lab component is 20 out of 25

Now the student mark has 2 components:classMark = 64labMark = 21

Here first we have to check whether pass in class mark and whether pass in lab separately

classMark >= 60labMark >= 20

After this, only if both expressions are true, the student has passed the course. So to combine two such conditions , we use Logical operators

classMark >= 60 && labMark >= 20

7

Logical Operators

Where && is called AND operator which results in true if both the sides of it are true.

For Boolean operands / expressions

Operator Use Returns true if

&& op1 && op2 op1 and op2 are both true

|| op1 || op2 either op1 or op2 is true

! ! op1 op1 is false

For && operator :

Opr 1 Opr 2 Result

True True True

True False False

False True False

False False False

8

Logical Operators

For || operator :

For ! Operator :

Opr 1 Opr 2 Result

True True True

True False True

False True True

False False False

! Opr Result

True False

False True

9

Operator precedence

Operator precedence from higher to lower. Operators with higher precedence are evaluated before operators with a relatively lower precedence. Operators on the same line have equal precedence.

postfix operators [ ] . (params) expr++ expr--

unary operators ++expr --expr +expr -expr ~ !

creation or cast new (type)expr

Multiplicative * / %

Additive + -

relational < > <= >= instanceof

equality == !=

Logical AND &&

Logical OR ||

Conditional ?:

assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

10

if statement syntax

Syntax

if (condition)

statement

Interpretation

» If the condition is true, the statement is executed; if condition is false, the statement is skipped

» This provides basic decision making capabilities

11

if statement Flow diagram

Flow diagram for if statement

statement

conditionfalse

true

12

if statement Example

import TextIO;

class IfExample {

static TextIO stdin = new TextIO(System.in);

static final String message1 =

" Illustrates the use of if

statement.\n ";

static final String message2 =

"Enter a number to find whether it is an even number.\n ";

static final String message3 =

" The Number is an Even Number\n ";

public static void main(String[]args) throws java.io.IOException {

int number;

System.out.println(message1); System.out.println(message2);

number = stdin.readInt();

if (number % 2 == 0 ) System.out.println(message3);

}

}

13

if statement Example

In the previous example, what will happen if the given number is not an even number?

The answer is , the message The Number is an Even Number

will not be printed.

But it would be nice to print,

The Number is not an Even Number

To have this alternate decision, Java has one more type of if statement with else clause.

14

if else statement syntax

Syntax

» An else clause can be added to an if statement to make it an if-else statement:

if (condition)

statement1

else

statement2

Interpretation

» If the condition is true, statement1 is executed; if the condition is false, statement2 is executed

» This provides basic decision making capabilities with alternate decision.

15

If else statement Flow diagram

Flow diagram for if else statement

statement1

conditionfalse

true

statement2

16

If else statement Example

import TextIO;

class IfExample {

static TextIO stdin = new TextIO(System.in);

static final String message1 =

" Illustrates the use of if

statement.\n ";

static final String message2 =

"Enter a number to find whether it is even or not.\n ";

static final String message3 =

" The Number is an Even Number\n ";

static final String message4 =

" The Number is an Odd Number\n ";

public static void main(String[]args) throws java.io.IOException {

int number;

System.out.println(message1); System.out.println(message2);

number = stdin.readInt();

if (number % 2 == 0 ) System.out.println(message3);

else

System.out.println(message4);

}

}

17

Grouping of statements

Several statements can be grouped together into a block statement

Block of statements are surrounded/ enclosed in a pair of matching curly braces

A block of statements can be used wherever a statement is called for in the Java syntax

18

If else if statement

This is used when we want to execute one block of statements out of more than two blocks, based on a sequence of conditions.

Example :

int testscore;

char grade;

if (testscore >= 90)

grade = 'A';

else if (testscore >= 80)

grade = 'B';

else if (testscore >= 70)

grade = 'C';

else if (testscore >= 60)

grade = 'D';

else

grade = 'F';

19

Nested if statement

The body of an if statement or else clause can be another if statement. These are called nested if statements

An else clause is matched to the latest preceeding if (no matter what the indentation implies)

Example :

import TextIO;class NestedIf { static TextIO stdin = new

TextIO(System.in);

public static void main(String[]args) throws java.io.IOException

{ int number;number = stdin.readInt(); if ( number > 0 )

System.out.println(”it is +ve\n");else if ( number < 0 )

System.out.println(” it is -ve\n"); else System.out.println(”it is 0\n"); }}

20

switch statement

Syntaxswitch (controllingExpression) {

case value1 :

Statements;

break;

case value2 :

Statements;

break;

.

.

default :

Statements;

}

Interpretation» Use the switch statement to conditionally

perform statements based on resultant value of some expression.

21

switch statement Example

import TextIO;Public class DayOfWeek { static TextIO stdin = new TextIO(System.in); public static void main(String[] args) { int dayNumber;

dayNumber = stdin.readInt();

switch (dayNumber) {case 0: System.out.println("Saturday");

break;case 1: System.out.println("Sunday");

break;case 2: System.out.println("Monday");

break; case 3: System.out.println("Tuesday");

break; case 4: System.out.println("Wednesday");

break;case 5: System.out.println("Thursday");

break;case 6: System.out.println("Friday");

break;}

}

22

switch statement Example( with out break statement )

import TextIO;Public class DayOfWeek { static TextIO stdin = new TextIO(System.in); public static void main(String[] args) {

dayNumber = stdin.readInt(); switch (dayNumber) {

case 0: System.out.println("Saturday");case 1: System.out.println("Sunday");case 2: System.out.println("Monday");case 3: System.out.println("Tuesday");case 4:

System.out.println("Wednesday");case 5: System.out.println("Thursday");case 6: System.out.println("Friday");

}}

What will be the output, if the input is 3 ?