Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:"— Presentation transcript:

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

2 2 Basic control structures l Selection structures »if statement »switch statement l Repetition structures »while statement »for statement »do statement l Flow breaking statements »break »continue »return l Exception handling stuctures »try { } catch ( ) { }

3 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 4 Relational Operators l To find whether a student has passed ICS102, we compare the student mark with the pass mark score. l For comparing these 2 marks, we use relational operators such as > l Comparing two data items using relational operators is called relational operation. l Expressions containing operands operated with relational operators are called relational expressions. l A relational expression will result in either true or false, with regard to the operand values l 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 5 Relational Operators OperatorUseReturn true if > op1 > op2 op1 is greater than op2 >= op1 >= op2op1 is not less than o p2 < 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 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 75 Pass mark in the Lab component is 20 out of 25 Now the student mark has 2 components: classMark = 64 labMark = 21 Here first we have to check whether pass in class mark and whether pass in lab separately classMark >= 60 labMark >= 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 7 Logical Operators Where && is called AND operator which results in true if both the sides of it are true. For Boolean operands / expressions OperatorUseReturns true if && op1 && op2op1 and op2 are both true || op1 || op2 either op1 or op2 is true ! ! op1 op1 is false For && operator : Opr 1Opr 2Result True False TrueFalse

8 8 Logical Operators For || operator : For ! Operator : Opr 1Opr 2Result True FalseTrue FalseTrue False ! OprResult TrueFalse True

9 9 Operator precedence l 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 10 if statement syntax l Syntax if (condition) statement l 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 11 if statement Flow diagram Flow diagram for if statement statement condition false true

12 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 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 14 if else statement syntax l 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 15 If else statement Flow diagram Flow diagram for if else statement statement1 condition false true statement2

16 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 17 Grouping of statements Several statements can be grouped together into a block statement l Block of statements are surrounded/ enclosed in a pair of matching curly braces l A block of statements can be used wherever a statement is called for in the Java syntax

18 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 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) l 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 20 switch statement l Syntax switch (controllingExpression) { case value1 : Statements; break; case value2 : Statements; break;. default : Statements; } l Interpretation »Use the switch statement to conditionally perform statements based on resultant value of some expression.

21 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 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 ?


Download ppt "1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:"

Similar presentations


Ads by Google