KEMBAR78
Lect8 | PPTX
Decision Making in Java
•if statement
•else if statement
•If else if statement
•Nested if statements
•Switch Statement
4/10/2019 1Jamsher Bhanbhro(F16CS11)
Decision Making
• In decision making there are one or more
conditions to be checked during the execution
of a program.
• The decision depends upon our conditions as
it may return true or false result.
4/10/2019 Jamsher Bhanbhro(F16CS11) 2
If statement
If statement is used to check one or more
Boolean conditions to execute a block of
statements or code.
• Syntax: if(Boolean Statements){//code body to
be executed if the statement is true}
E.g: if(a>11){
System.out.println(“Your
Condition is true”);}
4/10/2019 Jamsher Bhanbhro(F16CS11) 3
If else statement
• An if statement can be followed by the else statement if
statement will be executed when your condition is true
and in the case of the else statement it will be executed
when if’s condition will be false.
• I.E:
int a=3;
if(a>2){
System.out.println(“If is true”);}
else{
System.out.println(“If is false”);}
4/10/2019 Jamsher Bhanbhro(F16CS11) 4
If else if statement
• An if statement is followed by an optional else if
statement.
• One else if statement is correct than all followed
statements will not be executed.
• If(Bool Cond){}else if(cond){}else
if(cond){}……
int a=11;
If(a<11){}
else if(a<9){}…
4/10/2019 Jamsher Bhanbhro(F16CS11) 5
Nested If
• It means we can use if or else if inside the if
statement.
• Than all conditions will be executed
sequentially.
• If(Bool Cond){if(bool cond){}}….
4/10/2019 Jamsher Bhanbhro(F16CS11) 6
Switch Statement
• A value to be sated in a variable.
• And on that value conditions will be checked accordingly.
• Each value is called case, variables can be integer type, char
type or Boolean type.
• Understand syntax of the Switch statement in the following
program
I.E: int a;
Switch(a){
case ‘1’: System.out.println(“Jamsher”);}
break;
case’2’: ….}
4/10/2019 Jamsher Bhanbhro(F16CS11) 7
Why Decision making is used in
programming?
4/10/2019 Jamsher Bhanbhro(F16CS11) 8
4/10/2019 Jamsher Bhanbhro(F16CS11) 9

Lect8

  • 1.
    Decision Making inJava •if statement •else if statement •If else if statement •Nested if statements •Switch Statement 4/10/2019 1Jamsher Bhanbhro(F16CS11)
  • 2.
    Decision Making • Indecision making there are one or more conditions to be checked during the execution of a program. • The decision depends upon our conditions as it may return true or false result. 4/10/2019 Jamsher Bhanbhro(F16CS11) 2
  • 3.
    If statement If statementis used to check one or more Boolean conditions to execute a block of statements or code. • Syntax: if(Boolean Statements){//code body to be executed if the statement is true} E.g: if(a>11){ System.out.println(“Your Condition is true”);} 4/10/2019 Jamsher Bhanbhro(F16CS11) 3
  • 4.
    If else statement •An if statement can be followed by the else statement if statement will be executed when your condition is true and in the case of the else statement it will be executed when if’s condition will be false. • I.E: int a=3; if(a>2){ System.out.println(“If is true”);} else{ System.out.println(“If is false”);} 4/10/2019 Jamsher Bhanbhro(F16CS11) 4
  • 5.
    If else ifstatement • An if statement is followed by an optional else if statement. • One else if statement is correct than all followed statements will not be executed. • If(Bool Cond){}else if(cond){}else if(cond){}…… int a=11; If(a<11){} else if(a<9){}… 4/10/2019 Jamsher Bhanbhro(F16CS11) 5
  • 6.
    Nested If • Itmeans we can use if or else if inside the if statement. • Than all conditions will be executed sequentially. • If(Bool Cond){if(bool cond){}}…. 4/10/2019 Jamsher Bhanbhro(F16CS11) 6
  • 7.
    Switch Statement • Avalue to be sated in a variable. • And on that value conditions will be checked accordingly. • Each value is called case, variables can be integer type, char type or Boolean type. • Understand syntax of the Switch statement in the following program I.E: int a; Switch(a){ case ‘1’: System.out.println(“Jamsher”);} break; case’2’: ….} 4/10/2019 Jamsher Bhanbhro(F16CS11) 7
  • 8.
    Why Decision makingis used in programming? 4/10/2019 Jamsher Bhanbhro(F16CS11) 8
  • 9.