Sunteți pe pagina 1din 19

Control Flow Statements

The statements inside your source files are generally

executed from top to bottom, in the order that they appear. We have run programs wherein statements are executed one after another in a fixed order.
Control flow statements, however, break up the flow of

execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.

Control Flow Statements


There are two types of control structures:

a) Decision control structures Allows us to select specific sections of code to be executed b) Repetition control structures Allows us to execute specific sections of the code a number of times

Decision Control Structures


if statement The if statement specifies that a statement (or block of code) will be executed if and only if a certain boolean statement is true.
The if statement has the form, if (boolean__expression) statement;

If statement
Or,

if (boolean _expression){ statement 1; statement2; . } Where boolean_expression is either a boolean expression or boolean variable.

if statement
Example, given the code snippet:

int grade = 68; if (grade > 60) System.out.println (Congratulations!);

Or,
int grade = 68; if (grade>60) { System.out.println (Congratulations!); System.out.println (You passed!); }

if statement
Coding guidelines:
a) The boolean__expression part of a statement should

evaluate to a boolean value, that means that the execution of the condition should either result to a value of true or false. b) Indent the statements inside the if block. For example, if (boolean__expression) { //statement1; //statement2; }

If else statement
if else statement The if else statement is used when we want to execute a certain statement if a condition is true, and a different statement if the condition is false.
The if else statement has the form: if (boolean_expression) statement; else statement;

if else statement
or can also be written as,

if (boolean_expression) { statement1; statement2; . } else { statement1; statement2; . }

if else statement
For example given the code snippet:

int grade = 68; if (grade>68) System.out.println(Congratulations!); else System.out.println(Sorry, you failed!);

if else statement
Or,

int grade = 68; if (grade>60) { System.out.println(Congratulations!); System.out.println(You passed!); } else{ System.out.println (Sorry you failed); }

if else statement
Coding guidelines:
1) To avoid confusion, always place the statement or

statements of an if or if else block inside brackets {}.


1) You can have nested if else blocks. This means that

you can have other if else blocks inside another if else block. For example,

if else statement
Sample code snippet:

if(boolean_expression){ if(boolean_expression){ . } } else{. }

If else if statement
if else if statement 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. The if-else-if statement has the form:
if(boolean_expression1) statement1; else if (boolean_expression2) statement2; else statement3;

if else if statement
For example,

int grade = 68; if (grade>90){ System.out.println(Excellent!); } else if (grade>60){ System.out.println(Very good!); } else{ System.out.println(Sorry you failed!); {

Example for if else-if


public class Grade{ public static void main (String [] args){ double grade = 92.0; if (grade >=90) { System.out.println(Excellent!); } else if ((grade < 90) && (grade >=80)) { System.out.println(Good Job!); } else if ((grade < 80) && (grade >=60)) { System.out.println(Study harder!); } else { System.out.println(Sorry, you failed!); } } }

Common errors when using if else statements


1.

The condition inside the if else statement does not evaluate to a boolean value. For example, //WRONG int number = 0; if (number) { // some statements here } The variable number does not hold a Boolean value.

Common errors when using if else statements


2. Using = instead of == for comparison. For example,
//WRONG int number = 0; if (number = 0) { //some statements here }

Common errors when using if else statements


//CORRECT int number = 0; if (number == 0) { //some statements here } 3) Writing elseif instead of else if.

S-ar putea să vă placă și