Lecture 05
Lecture 05
Lecture 05
Fundamentals
Fall 2022
Lecture 5
Dr. Farhan Aadil
farhan.aadil@ciit-attock.edu.pk
DrAadil.com
statement
statement
statement
statement
statement statement
statement
statement
Decision Statements
if (expression){
statement1; execute execute
} else { statement1 statement2
statement2;
}
rest_of_program..
execute
rest_of_program
License Program
public class Input {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
if (grade == 'A')
System.out.println("You got an A.");
else if (grade == 'B')
System.out.println("You got a B.");
else if (grade == 'C')
System.out.println("You got a C.");
else
System.out.println("You got an F.");
Chained If-Else Statements
if (grade == 'A')
System.out.println("You got an A.");
if (grade == 'B')
System.out.println("You got a B.");
if (grade == 'C')
System.out.println("You got a C.");
else
System.out.println("You got an F.");
Nested If statements
MinOfThree.java
…
int num1, num2, num3, min = 0;
Scanner scan = new Scanner (System.in);
System.out.println ("Enter three integers: ");
num1 = scan.nextInt();
num2 = scan.nextInt();
num3 = scan.nextInt();
if(expression)
………………………………
System.out.println ("Minimum value: " + min);
}
}
Programming Fundamentals
• What are Control Structures?
– If Statements
– Switch Statements
Switch Statements
case 20:
System.out.println("20");
break;
case 30:
System.out.println("30");
break;
default:
System.out.println("Not in 10, 20 or 30");
}
}
}
Switch Example
• This is how it is accomplished with a switch:
switch (grade) {
case 'A':
System.out.println("You got an A.");
break;
case 'B':
System.out.println("You got a B.");
break;
case 'C':
System.out.println("You got a C.");
break;
default:
System.out.println("You got an F.");
}
• if-else chains can be sometimes be rewritten as a “switch” statement.
• switches are usually simpler and faster
Switch Example
char c= 'a';
switch(c)
{
case 'a’:
case 'A’:
System.out.print("Hello world");
break;
}
Grade Report
public class GradeReport
{
• My email: farhan.aadil@ciit-attock.edu.pk
• My web page: DrAadil.com