0% found this document useful (0 votes)
7 views32 pages

W6-Presentation02-Control Structures

The document explains decision control structures in Java, including if, if-else, and if-else if-else statements, which allow programs to execute code blocks based on boolean conditions. It also discusses common errors associated with these structures and introduces the switch statement for handling multiple alternative actions. Examples and flowcharts are provided to illustrate the usage of each control structure.

Uploaded by

igcasan.jc07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views32 pages

W6-Presentation02-Control Structures

The document explains decision control structures in Java, including if, if-else, and if-else if-else statements, which allow programs to execute code blocks based on boolean conditions. It also discusses common errors associated with these structures and introduces the switch statement for handling multiple alternative actions. Examples and flowcharts are provided to illustrate the usage of each control structure.

Uploaded by

igcasan.jc07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Control Structures

Decision Control Structures


- these are statements that allow the program to decide which
block of codes to be executed based from the given condition.
The following are the control structures used in Java:

• if Statement
- enables the program to execute a statement or block of
codes if and only if the boolean expression is true.
The if-statement is can be express this form,

if (boolean_expression)
statement;
or
If (boolean_expression){
statement1;
statement2;
...
}
The figure below shows the execution flow of the if statement:

Boolean Expression
true false

Statement
For example:

if(age>=18)
System.out.println("Qualified to vote!");

Below is the flowchart for the sample code above:


if Statement Sample Program:

Output:
Coding Guidelines:
1.Using if-statement without curly braces makes the code shorter, but it is prone to
errors and hard to read.
2.You should indent the statements inside the block, for example:
if( boolean_expression ){
//statement1;
//statement2;
}
3. Inside the block you can put any statement, meaning you have another if
statement inside the if statement, fir example:
if(boolean_expression){
if (boolean_expression){
//Statements
}
}
• if-else Statement
-is used when you want the program decide from which of the
two sets of statements is going to be executed based on whether the
condition is true or false.
The if-else statement has the form,
if( boolean_expression )
statement;
else
statement;
or can also be written as,
if( boolean_expression ){
statement1;
statement2;
...
}
else{
statement1;
statement2;
...
}
Below is the flowchart for if-else statement:

Based from the flowchart above, statement1 will be executed if the


boolean_expression evaluates to true, otherwise the statement2 will be executed.
For example:

if(age>=18)
System.out.println("Qualified to vote!");
else
System.out.println("To young to vote!");
Below is the flowchart for the sample code above:

if-else Statement Sample Program:


Output:
• if-else if-else Statement
-is used when the program have multiple alternative action to take.
This statement is actually an if-else statement with another if-else
statement inside the else block.
Below is the form of nested-if statement:

if( boolean_expression ){
statement1;
statement2;
...
}
else{
if( boolean_expression ){
statement1;
statement2;
...
}
}
we usually write this as:
if( boolean_expression ){
statement1;
statement2;
...
}
else if( boolean_expression ){
statement1;
statement2;
...
}
Below is the flowchart for if-else if-else statement:

In the flowchart shown above, if boolean_expression1 evaluates to true, the


program will execute the statement1 and skips the other statements, if false,s the
program will proceed to evaluate the boolean_expression2. If
boolean_expression2 evaluates to true, the program executes statement2 and
skips the else statement, otherwise the program will execute statement3.
For example:
if(age>=18){
System.out.println("Qualified to vote!");
}
else if(age < 0){
System.out.println("Invalid age!");
}
else{
System.out.println("Too young to vote!");
}
Below is the flowchart for the sample code above:
if-else if-else Statement Sample Program:
Output:
Common Errors when using the decision control structure:

1. Only boolean value is accepted in the condition statement. For


example,
//WRONG
int num = 0;
if( num ){
// statements
}

The variable num does not hold a boolean value.


2. Using = instead of == for comparison. For example,
int num = 5;
if( num = 5 ){ //error in the condition
// statements
}

This should be written as,


int num = 5;
if( num == 5 ){
// statements
}
3. Adding semi-colon at the end of if statement. For example,
if(num == 5);
// statements

4. Writing elseif instead of else if.


int num = 5;
if(num = 5){ //error in the condition
// statements
}elseif(num == 0){ //error, no space between else and if
// statements
}
This should be written as,
int num = 5;
if(num == 5){
// statements
}else if(num == 0){
// statements
}
Switch Statement
- is another way to allow the program to have multiple alternative
action.
Below is the swith-statement form:

switch( switch_expression ){
case case_value1:
// statements
break;
case case_value2:
// statements
break;
...
case case_valueN:
// statements
break;
default:
//statements
}

Switch_expression is a variable or expression that must have a type value


of char, short, int, long or string.
The switch statement first evaluates with the switch_expression, and then it will
check if value of first case (case_value1) matches the value of switch_expression.

For example:

int num = 2;
switch(num){
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
case 3:
System.out.println("Three");
break;
case 4:
System.out.println("Four");
break;
default:
System.out.println("Invalid Number");
}
Below is the flowchart of the example above:
Sample switch Statement Program:

You might also like