Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Notes – Week 2 Lecture-2

Similar presentations


Presentation on theme: "Lecture Notes – Week 2 Lecture-2"— Presentation transcript:

1 Lecture Notes – Week 2 Lecture-2
Chapters 3 & 4

2 One-way if Statements /* This a program fragment from the previous slide //If the radius is not a negative number if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } A one-way if statement starts with keyword if, followed by a Boolean expression for the condition, then followed by a statement body. 2

3 One-way if Statements } if (Boolean-expression) { statement(s);
if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } if (Boolean-expression) { statement(s); } 3

4 Relational Operators The examples are Boolean expressions. A Boolean expression consists of a variable or value on each side of a relational operator. The expression is evaluated as true if the relation operator is satisfied; otherwise it is evaluated as false. 4 4

5 Two-way if-else Statements
/* This a program fragment from the previous slide //If the radius is not a negative number if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } else { System.out.println("Negative input"); If there are two alternative actions to be taken, we use a two-way if-else statement: one action is taken when the Boolean expression is evaluated as true while another is taken otherwise. For each alternative action, we would need a block of statements. 5

6 Two-way if-else Statements
if (boolean-expression) { statement(s); \\first block of statement } else { statement(s); \\second block 6 6

7 Multi-way if-else Statements
/* This a program fragment for a multi-way if-else statement if (score >= 90.0) System.out.print(”A”); \\ if score >= 90.0 else if (score >= 80.0) System.out.print(”B”); \\ if score < 90.0 and >= 80.0 else if (score >= 70.0) System.out.print(”C”); \\ if score < and >=70.0 else if (score >= 60.0) System.out.print(”D”); \\ if score < 70.0 and >= 60.0 else System.out.println(”F");\\ score < 60.0 This statement has 4 nested if-else statements and 5 alternative actions (5 ways). 7 7

8 Multi-way if-else Statements
else if else if else if else 8 8 8

9 Question: Write a Program that will take students marks, based on marks, his/her grade will be assigned using grade variable. At the end of IF statements. You are suppose to print the grade. Note: You need to use print statement only once and grade will contain char data type.

10 End


Download ppt "Lecture Notes – Week 2 Lecture-2"

Similar presentations


Ads by Google