Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture 05

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Programming

Fundamentals
Fall 2022

Lecture 5
Dr. Farhan Aadil
farhan.aadil@ciit-attock.edu.pk
DrAadil.com

Please turn OFF your Mobile Phones!


Programming Fundamentals
• What are Control Structures?
– decision (if) Statements
– Switch Statements
What are Control
Structures?
• Control structures alter the flow of the program, the sequence of
statements that are executed in a program.

• They act as "direction signals" to control the path a program takes.

• Two types of control structures in Java:


– decision statements
– loops
Program Flow
• Java will execute the statements in your code in a specific sequence or flow
which can be described through a flow diagram:

a simple program statement

statement
statement

statement

statement
statement statement

statement
statement
Decision Statements

• A decision statement allows the code to execute a statement or block


of statements conditionally.

• Two types of decisions statements in Java:


– if statements
– switch statements
Programming Fundamentals
• What are Control Structures?
– If Statements
– Switch Statements
If Statement Syntax
if (expression/condition) {
// block of code to be executed if the condition is true
}
else {
// block of code to be executed if the condition is false
}
rest_of_program
• expression must evaluate to a boolean value, either true or false
• If expression is true, statement is executed and then
rest_of_program
• If expression is false, statement is not executed and the program
continues at rest_of_program
If-Else Flow Diagram
The if-else decision statement
executes a statement if an expression is
yes no
is true and a different statement if it is “expression”
not true. true?

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);

System.out.print ("Enter your age: ");


int age = scan.nextInt();
System.out.println ("You entered: " + age);
if (age < 18)
System.out.println ("You are not eligible for license");
else
System.out.println ("You are eligible");
}
}
Chained If-Else Statements

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

• Flow of Switch Statement


Switch Statement Syntax
• The switch statement enables you to test several cases generated by a given
expression.
• For example:
switch (expression) {
case value1:
statement1;
Break;
case value2:
statement2;
Break;
default:
default_statement;
}
Switch Example
public class SwitchExample {
public static void main(String[] args) {
int number=20;
switch(number)
{
case 10:
System.out.println("10");
break;

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

class SwitchDate{ case 4: System.out.println("thursday");


public static void main(String[] args){ break;
int week = 3; case 5: System.out.println("friday");
switch (week){ break;
case 6:
case 1: System.out.println("monday"); System.out.println("saturday");
break; break;
case 7:
case 2: System.out.println("sunday");
System.out.println("tuesday"); break;
break; default: System.out.println("Invalid
week");
case 3: System.out.println("wednesday"); break;
break; }
}
}
Switch
switch (num) {
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("1 through 5");
break;
case 6:
case 7:
case 8:
case 9:
case 10:
System.out.println("6 through 10");
break;
Switch

char c= 'a';
switch(c)
{
case 'a’:
case 'A’:
System.out.print("Hello world");
break;
}
Grade Report
public class GradeReport
{

public static void main (String[] args)


{
int grade, category;

Scanner scan = new Scanner (System.in);

System.out.print ("Enter a numeric grade (0 to 100): ");


grade = scan.nextInt();

category = grade / 10;

System.out.print ("That grade is ");


Grade Report
switch (category)
{
case 10:
System.out.println ( "a perfect score. Well done." );
break;
case 9:
System.out.println ( "well above average. Great." );
break;
case 8:
System.out.println ( "above average. Nice job." );
break;
case 7:
System.out.println ( "average.");
break;
case 6:
System.out.println ( "below average." );
System.out.println ( "See the instructor." );
break;
default:
System.out.println ( "not passing." );
}
}
}
Important links

• My email: farhan.aadil@ciit-attock.edu.pk
• My web page: DrAadil.com

Thanks for listening!


Ready for Questions & Answers ☺

COMSATS University Islamabad, Attock Campus

You might also like