Presentation is loading. Please wait.

Presentation is loading. Please wait.

5/17/20151. 2 Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.

Similar presentations


Presentation on theme: "5/17/20151. 2 Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while."— Presentation transcript:

1 5/17/20151

2 2 Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while - for loops - switch statements

3 5/17/20153 if - else construct... The if-else construct executes some operation based on successful completion of a condition, that returns only Boolean values. If the condition is not satisfied, then the set of instructions under ‘else’ are executed. Begin Result When condition is true False True Result When condition is false if(condition) { //Statements if condition is true } else { //Statements if condition is false } if(condition) { //Statements if condition is true } else { //Statements if condition is false } If (condition)

4 5/17/20154 if - else construct... The if-else construct executes some operation based on successful completion of a condition, that returns only Boolean values. If the condition is not satisfied, then the set of instructions under ‘else’ are executed. Begin If (condition) Begin if(condition) { //Statements if condition is true } else { //Statements if condition is false } if(condition) { //Statements if condition is true } else { //Statements if condition is false }

5 02/10/105 Example: if-else... class Constructs { public static void main(String arg[]) { int a=10; if(a<10) { System.out.println(“a is greater than 10”); } else { System.out.println(“a is less than 10”); } } class Constructs { public static void main(String arg[]) { int a=10; if(a<10) { System.out.println(“a is greater than 10”); } else { System.out.println(“a is less than 10”); } }

6 5/17/20156 Ternary (conditional) operator... The ternary operator is considered to be an alternative to the if- else construct. test ? Pass : Fail ; test – the condition to be tested Pass - value given here is returned when the test is satisfied Fail – value given here is returned when the test is failed.

7 02/10/107 Example: ternary operator... class Constructs { public static void main(String arg[]) { int avg=75; char result; result=avg>50 ? ‘A’ : ‘F’ System.out.println(result); } class Constructs { public static void main(String arg[]) { int avg=75; char result; result=avg>50 ? ‘A’ : ‘F’ System.out.println(result); }

8 5/17/20158 Nested if - else construct....dfssdf dsdfsdf Begin If (condition) Begin if(condition) { //Statements if condition is true } else { //Statements if condition is false } if(condition) { //Statements if condition is true } else { //Statements if condition is false }

9 5/17/20159 Example: Nested if-else... Modify your program, so that it is able to find the grade of each student based on the average. You must use following criteria to obtain the grades. Example 2 (cont): Store information of students Range of AverageGrade 85 to 100A 75 to 84B 65 to 74C 55 to 64S 45 to 54w <45F

10 02/10/1010 While loop... The while loop executes some operation repeatedly, until the condition returns false. while(condition) { //body of the loop } while(condition) { //body of the loop } When the condition fails, then the loop terminates.

11 02/10/1011 Example: While loop... class Constructs { public static void main(String arg[]) { int i=1; while(i<=10) { System.out.println(i); i++; } class Constructs { public static void main(String arg[]) { int i=1; while(i<=10) { System.out.println(i); i++; } Write a JAVA program to display consecutive numbers from 1 to 10, using while loops.

12 02/10/1012 Do-While loop... The do-while loop is similar to the while loop except the condition to be evaluated is given at the end. do { //body of the loop } while(condition); do { //body of the loop } while(condition); The loop is executed at least once even when the condition is false.

13 02/10/1013 Example: Do-While loop... class Constructs { public static void main(String arg[]) { int i=1; do { System.out.println(i); i++; } while(i<=10) ; } class Constructs { public static void main(String arg[]) { int i=1; do { System.out.println(i); i++; } while(i<=10) ; } Write a JAVA program to display consecutive numbers from 1 to 10, using do-while loops.

14 02/10/1014 For loop... A for loop repeats a set of statements, until a condition is satisfied. There are three major parts in a for loop namely; initialization, test (condition) and the expression. for(initialization; test; expression) { //Body of the for loop } for(initialization; test; expression) { //Body of the for loop } Initialization- A variable is initialized to a value. Test – This is the condition of the loop and it returns a Boolean value. Expression – The expression is evaluated every time that the loop is executed.

15 02/10/1015 Example: For loop... class Constructs { public static void main(String arg[]) { for(int i=0;i<10;i++) { System.out.println(i+1); } class Constructs { public static void main(String arg[]) { for(int i=0;i<10;i++) { System.out.println(i+1); } Write a JAVA program to display consecutive numbers from 1 to 10, using for loops.

16 02/10/1016 Switch statement... The switch statement dispatches different parts of the code based on the value of a single variable or expression. The value of expression is compared with each of the literal values in the case statements. If they match, the corresponding instructions are executed. Otherwise an optional default statement is executed. switch(test) { case value1: //Statement1 break; ……………………………………………………………………………………………………. default: //Statement } switch(test) { case value1: //Statement1 break; ……………………………………………………………………………………………………. default: //Statement } Test is a variable or an expression that evaluates to byte, char or int. It cannot be float, double, long, string or any other object

17 02/10/1017 Example: Switch Statement... int num=2; switch(num) { case 1: System.out.println("January"); break; case 2: System.out.println(“February"); break; …………………………………………………………………………………………………………………………………………………………………. default: System.out.println("Invalid Number"); } int num=2; switch(num) { case 1: System.out.println("January"); break; case 2: System.out.println(“February"); break; …………………………………………………………………………………………………………………………………………………………………. default: System.out.println("Invalid Number"); } Write a JAVA program to display the name of the month when the number is given.

18 02/10/1018 Break & Continue... The break keyword halts the execution of current loop and forces control out of the loop. The continue keyword starts the next iteration of the loop rather than halting the execution of entire loop. for(int i=0;i<10;i++) { if(i==5) continue; System.out.println(i); } for(int i=0;i<10;i++) { if(i==5) continue; System.out.println(i); } for(int i=0;i<10;i++) { if(i==5) break; System.out.println(i); } for(int i=0;i<10;i++) { if(i==5) break; System.out.println(i); }

19 02/10/1019 Exercises... Write JAVA programs to display following outputs. You must use programming constructs.


Download ppt "5/17/20151. 2 Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while."

Similar presentations


Ads by Google