Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to programming in java

Similar presentations


Presentation on theme: "Introduction to programming in java"— Presentation transcript:

1 Introduction to programming in java
Lecture 07 Conditional if statements

2 Basic boolean Operators
Meaning Example == equal (4-2)==(8-6) is true != Not equal 3 !=2 is true but 4!=(6-2) is false > Greater than (3>2) is true >= Greater or equal (3>=2) is true < Less than (3<2) is false <= Less or equal (3<=4) is true

3 Compound Boolean expressions
Operator Meaning Example && And True && true is true (3>2) && (1<=10) is true True && False is False (3>2) && (1>10) is False False && False is false (3<2) && (1>10) is False || OR True || true is true (3>2) || (1<=10) is true True|| False is True (3>2) || (1>10) is True False || False is false (3<2) || (1>10) is False

4 Compound Boolean Expressions (Cont)
True && True = True False && False = False (True && False) = (False && True) = False True || True = True False || False = False (True || False) = (False || True) = True && ||

5 If – The Conditional Statement
The if statement evaluates an expression and if that evaluation is true then the specified action is taken if ( x < 10 ) x = 10; If the value of x is less than 10, make x equal to 10 It could have been written: if ( x < 10 ) x = 10; Or, alternatively: if ( x < 10 ) { x = 10; }

6 Executes a block of statements only if a test is true
The if statement Executes a block of statements only if a test is true if (test) { statement; ... } Example: int x = 2; if (x >= 0) { System.out.println(“ x is positive");

7 Executes one block if a test is true, another if false
The if/else statement Executes one block if a test is true, another if false if (test) { statement(s); } else { } Example: int x = 2; if (x >= 0) { System.out.println(“ x is positive"); else { System.out.println(“x is negative.");

8 Example1 What does this program do?
// public class IsEven { public static void main(String[] arguments) int x = Integer.parseInt(args[0]) if (x % 2 == 0) System.out.println(“YES”); } else System.out.println(“No”); //end of program

9 Example2 What does this program do?
// public class IsOdd { public static void main(String[] arguments) int x = Integer.parseInt(args[0]); if (x % 2 == 1) System.out.println(“YES”); } else System.out.println(“No”); //end of program

10 Example3 What does this program do?
// public class FirstIsMultipleOfSecond { public static void main(String[] arguments) int x = Integer.parseInt(args[0]); int y = Integer.parseInt(args[1]); if (x % y == 0) System.out.println(“YES”); } else System.out.println(“No”); //end of program

11 Example4 What does this program do?
// public class OneIsMultipleOfOther { public static void main(String[] arguments) int x = Integer.parseInt(args[0]); int y = Integer.parseInt(args[1]); if ((x % y ==0 ) || (y%x ==0 )) System.out.println(“YES”); } else System.out.println(“No”); //end of program

12 Example 5 Nested Ifs // program that takes the exam final mark prints out the corresponding grade. public class Examgrade { public static void main(String[] arguments) double testscore = double.parseDouble(args[0]); 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'; } System.out.println("Grade = " + grade); } //end of program

13 Practice Question Write a program, DayofTheWeek.java, that takes one command line integer n = 1, 2, ...,7. The program prints the following n = 1 it prints Monday n = 2 it prints Tuesday n = 3 it prints Wednesday n = 4 it prints Thursday n = 5 it prints Friday n = 6 it prints Saturday n = 7 it prints Sunday Hints: you can either use Nested ifs or a switch statement.

14 Solution ( nested if else) DayofTheWeek.java
public class DayofTheWeek { public static void main(String[] args) int day = Integer.parseInt(args[0]); String today; if(day ==1) { today= "Monday";} else if(day==2) {today= "Tuesday";} else if(day == 3) {today= "Wednesday";} else if(day==4) {today= "Thursday";} else if(day==5) {today= "Friday";} else if(day==6) {today= "Saturday";} else if(day==7) {today= "Sunday";} else {today= "Wrong number Entered";} System.out.println(" Today is " + today); } //end of program


Download ppt "Introduction to programming in java"

Similar presentations


Ads by Google