6 Control Structures

Only available on StudyMode
  • Topic: Statement, Control flow, For loop
  • Pages : 9 (1291 words )
  • Download(s) : 110
  • Published : December 17, 2012
Open Document
Text Preview
s
6Control Structures
Objectives
At the end of the lesson, the student 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 Control Structures

• Control structures
– allows us to change the ordering of how the statements in our programs are executed • Two types of Control Structures
– decision control structures
• allows us to select specific sections of code to be executed – repetition control structures
• allows us to execute specific sections of the code a number of times Decision Control Structures
• Decision control structures
– Java statements that allows us to select and execute specific blocks of code while skipping other sections • Types:
– if-statement
– if-else-statement
– If-else if-statement
if-statement
• if-statement
– specifies that a statement (or block of code) will be executed if and only if a certain boolean statement is true. • if-statement has the form:
if( boolean_expression )
statement;
or
if( boolean_expression ){
statement1;
statement2;
}
– where,
• boolean_expression is either a boolean expression or boolean variable. if-statement Flowchart
Example 1
int grade = 68;
if( grade > 60 )
System.out.println("Congratulations!");
Example 2
int grade = 68;
if( grade > 60 ){
System.out.println("Congratulations!");
System.out.println("You passed!");
}
Coding Guidelines
1. 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 a false. 2. Indent the statements inside the if-block.

For example,
if( boolean_expression ){
//statement1;
//statement2;
}
if-else statement
• if-else statement
– used when we want to execute a certain statement if a condition is true, and a different statement if the condition is false. • if-else statement has the form:
if( boolean_expression ){
statement1;
statement2;
. . .
}
else{
statement3;
statement4;
. . .
}
Flowchart
Example 1
int grade = 68;
if( grade > 60 )
System.out.println("Congratulations!");
else
System.out.println("Sorry you failed");
Example 2
int grade = 68;
if( grade > 60 ){
System.out.println("Congratulations!");
System.out.println("You passed!");
}
else{
System.out.println("Sorry you failed");
}
Coding Guidelines
1. To avoid confusion, always place the statement or statements of an if or if-else block inside brackets {}. 2. 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( boolean_expression ){
if( boolean_expression ){
//some statements here
}
}
else{
//some statements here
}
if-else-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 statement has the form:

if( boolean_expression1 )
statement1;
else if( boolean_expression2 )
statement2;
else
statement3;
Flowchart
Example
Common Errors
1. The condition inside the if-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.
2. Writing elseif instead of else if.
Common Errors
3. Using = instead of...
tracking img