Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Programming Fundamentals

Academic Year 2014-2015


Yusra A.Salih
Control Structures
Three Basic Control Structures
 All programs can be written with just these types of
structures
 Sequence structure(sequential )
 Statements run one after the other
 Non-Sequence structure (Non-Sequential)
1-Selection structure
 Depending on a condition, do one thing;
otherwise, do something else
 Examples in Java: if, if else, and switch.
2- Repetition structure
 Repeat some actions over and over
 Examples in Java: for loops, while loops, and
do/while loops.
Selection structure
The if Statement
 The if statement allows the programmer to
make decisions on whether a section of
code executes or not.
 The if statement uses a boolean expression
as an argument to decide if the next
statement or block of statements executes.
if(boolean expression is true)
execute next statement.
The if structure
 Pseudocode:
if some Boolean expression is true
do this
 Example1:
if ( x == y )
System.out.println(" x is equal to y!" ) ;
 Example2:
if ( temperature >= 85 )

;System.out.println( "It is hot out!" )


if Flow Chart

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

”Print “You faild ”Print “You passed


Example
import java.util.Scanner;
public class PassFail
{
public static void main(String[] args)
{
int grade;
Scanner sc= new Scanner(System.in);
System.out.println("What is your grade?");
grade = sc.nextInt();

/* find out if grade is passing */


if ( grade >= 65 )
{
System.out.println ( "You passed!!!" );
System.out.println ( "Congratulations!" );
}
else
{
System.out.println ("You failed!");
}
}
}
example: Write a java program that prompt the user to enter 3
.numbers then find the minimum number and print it’s value

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”.

Q2:Write java program that prompt the user to enter 2


numbers then check the second number if it is equal to
zero, print out” Division by zero is not possible.Please run
the program again and enter a number other than
zero”.Otherwise devide first number by second number and
print out the result.

Q3:Write a java program that prompt the user to enter 3


numbers then find the biggest number and print it’s value.
H.W
Q5:Write a java program that prompt the user to enter the
length of the side of a square, and print its area. Produce an
error message if the length is negative. (i.e.validation)
Q6:Computing Weekly Wages: Gross pay depends on the
pay rate and the number of hours worked per week.
However, if you work more than 40 hours, you get paid
time-and-a-half for all hours worked over 40.Write a java
program that asks the user to enter pay rate and hours
worked then compute gross pay .

You might also like