Professional Documents
Culture Documents
Programming Fundamentals: Academic Year 2014-2015 Yusra A.Salih
Programming Fundamentals: Academic Year 2014-2015 Yusra A.Salih
true
temperature >=85 Print "It is hot out!"
false
If Statements and Boolean
Expressions
public class BoolExp
{
public static void main (String[] args)
{
;int x=10,y=20
if (x > y)
;System.out.println(“X is greater than Y”)
if(x == y)
;System.out.println(“X is equal to Y”)
if(x != y)
;System.out.println(“X is not equal to Y”)
}
}
Programming Style and if
Statements
Rules of thumb:
The conditionally executed statement should be
on the line after the if condition.
The conditionally executed statement should be
indented one level from the if condition.
If an if statement does not have the block curly
braces, it is ended by the first semicolon
encountered after the if condition.
if(expression) No semicolon here.
Semicolon ends statement here.
statement;
Block if Statements
If curly braces are used to group conditionally executed
statements, the if statement is ended by the closing curly
brace.
if(expression)
{
statement1;
statement2;
} Curly brace ends the statement.
Remember that if the curly braces are not used, then only the
next statement after the if condition will be executed
conditionally.
if(expression)
statement1; Only this statement is conditionally executed.
statement2;
statement3;
Indentation
Everything within the block of code (even if it is
an implicit block because we only use one
statement) should be indented
helps you see the block at a quick glance.
Avoid writing code like this:
if (grade >= 65) {
System.out.println("You passed!!!\n");
System.out.println ("Congratulations!\n");
}
This is valid Java code, but it is not easy to view
the block: bad style
Common error: misplaced
semi-colon
Remember, Java requires that you use a semicolon
to terminate a statement.
A complete if statement is formed as follows:
if (boolean expression)
Statement or block of code;
Therefore, if you place a semicolon after the
conditional as in
if (boolean expression);
Statement or block of code;
The compiler will interpret the semicolon as a null
statement. In other words, nothing will happen if the
expression evaluates to true and the statement of block of
code will be executed whether or not the boolean
expression is true.
if-else Statements
The if-else statement adds the ability
to conditionally execute code based if
the expression of the if statement is
false.
if(expression)
statementOrBlockIfTrue;
else
statementOrBlockIfFalse;
if with a twist: if else
Pseudocode:
if some Boolean expression is true
do this
otherwise
do something else
Example:
if ( grade >= 65 )
System.out.println( "You passed!" );
else
System.out.println( "You failed!" );
if/else Flow Chart
False True
grade
>=60
import java.util.Scanner;
public class minimum
{
public static void main(String[] args)
{
int grade1, grade2, grade3;
Scanner sc= new Scanner(System.in);
System.out.println(“Eneter the first grade");
grade1 = sc.nextInt();
System.out.println(“Enter the second grade");
grade2 = sc.nextInt();
System.out.println(“Enter the third grade");
grade3 = sc.nextInt();
/* find out if minimum grade */
if(grade1<grade2)
if(grade1 < grade3 )
{
System.out.println (“Minimum grade is “ + grade1);
}
if(grade2<grade3)
{
System.out.println (“Minimum grade is “ + grade2);
}
Else{
System.out.println (“Minimum grade is “ + grade3);
}
}
H.W
Q1:Write a java program that prompt the user to enter 3
scores and compute the average, then test, if it is greater
than 90,print out “Congratulation very high scores”.