PresentCh03 - Decision and Repetition Statements
PresentCh03 - Decision and Repetition Statements
Programming
Computer Science Year II
Compiled by: Gebreigziabher A.
1
Decision & Repetition
Statements
2
* A running program spends all of its time executing statements. The
order in which statements are executed is called flow control or
(control flow). Flow control in a program is sequential, from one
statement to the next, but may be diverted to other paths by
branch statements.
* Program Control Statements allow us to change the ordering
(sequence) of how the statements in our programs are executed.
There are different forms of Java statements:
Declaration statements are used for defining variables.
Assignment statements are used for simple, algebraic
computations.
Branching statements are used for specifying alternate paths of
execution, depending on the outcome of a logical condition.
Loop statements are used for specifying computations, which need
to be repeated until a certain logical
3 condition is satisfied.
*At the end of this chapter students should be able to
Use decision control structures (if, else, switch) which allows
selection of specific sections of code to be executed
Use repetition control structures (while, do-while, for) which
allow executing specific sections of code a number of times
Use branching statements (break, continue, return) which allows
redirection of program flow
4
* Decision statements are Java statements that allows us to select and execute
specific blocks of code while skipping other sections.
5
2.1.2 The if-else statement
*The if-else statement is used to execute a certain statement if a condition is true,
and a different statement if the condition is false.
*General form: if(expression)
statement1;
else
statement2;
*First expression is evaluated. If the outcome is true then statement1 is executed.
Otherwise, statement2 is executed.
*Example: if(score>=50) {
System.out.println("Congratulations! You Passed!”; ");
}
else {
System.out.println(”You Failed!”);
System.out.println(”You Must Take This Course Again!”);
6
}
* The statement in the else-clause of an if-else block can be another if-else structures.
* This cascading of structures allows us to make more complex selections.
* Example:
if(score<0 && score>100) { if(CGPA<0 && CGPA>4) {
System.out.println(“ Score [0-100]”); System.out.println(“CGPA [0-4]”);
} }
if(score>=85 && score<=100) { if(CGPA>=3.5 && CGPA<=4) {
System.out.println(“ A Grade”); System.out.println(“ Excellent!”);
} }
else if(score>=75 && score<85 ){ else if(CGPA>=3.0 && CGPA<3.5){
System.out.println(“ B Grade”); System.out.println(“ Very Good!”);
}
}
else if(CGPA>=2.5 && CGPA<3.0 ) {
else if(score>=65 && score<75 ) {
System.out.println(“ Good!”);
System.out.println(“ C Grade”);
}
} else if(CGPA>=2.0 && CGPA<2.5 ) {
else if(score>=50 && score<65 ) { System.out.println(“Satisfactory!”);
System.out.println(“D Grade”); }
} else {
else { System.out.println(“Poor!”);
System.out.println(“F Grade”); 7 }
}
* Another way to indicate a branch is through the switch keyword.
* The switch construct allows branching on multiple outcomes.
* Multiple-choice, provides a way of choosing between a set of alternatives.
* General form:
switch (expression) {
case: constant1:
statements;
...
case: constantn:
statements;
default:
statements;
}
* First expression (called the switch tag) is evaluated, and the outcome is compared to
each of the numeric constants (called case labels), in the order they appear, until a
match is found. The final default case is optional and is exercised if none of the earlier
cases provide a match.
8
switch(op)
// The 4 basic arithmetic operators using switch
{
import java.io.*;
case '+':
public class switch1
{
System.out.println("Sum="+(a+b));
public static void main(String args[])throws IOException break;
{ case '-':
String s; System.out.println("Sub="+(a-b));
char op; break;
int a, b; case '*':
BufferedReader aa=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Mul="+(a*b));
System.out.print("The First Number:="); break;
s=aa.readLine();//to read an input data; case '/':;
a=Integer.parseInt(s);//convert the given string in to integer System.out.println(“Quo="+(a/b));
System.out.print("The Second Number:="); break;
s=aa.readLine();//to accept a string from the use default:
b=Integer.parseInt(s);
System.out.println("Invalid Operator");
System.out.print("An Operator:=");
} } }
op=(char)aa.read();//to accept a character from the user
9
* Repetition statements are Java statements that allows us to execute specific
blocks of code a number of times. There are three types of repetition
statements, the while, do-while and for loops.
15
public class Power {
private int x; // instance variables
private int y;
public Power(int a, int b) { //A constructor to Initializes instance variable
x=a;
y=b;
}
public double computePower() {//a method to access the data members
double z;
z=Math.pow(x,y);
return z;
}
public static void main(String args[]) {
Power p= Power(2,3); //Object creation with a parameterized constructor
double result=p. computePower(); //Assign the return value of the function to the variable res
System.out.println("Output :"+result);
}
} 16