CH 6 CONTROL STRUCTURES

CONTROL STRUCTURES

 Control Flow Statements: The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, breakup the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.

(a) Selection:


A selection statement selects among a set of statements depending on the value of a controlling expression.
1. if statements:

The if statement allows selection (decision making) depending upon the outcome of a condition. If the condition evaluates to true then the statement immediately following if will be executed and otherwise if the condition evaluates to false then the statements following the else clause will be executed.
1.  Simple if: The syntax of if statement is as shown below:
 Syntax:
if (conditional expression)
{ Statement Block; }
2. if-else : The syntax of if-else statement is as shown below:
Syntax:
if (conditional expression)
{ Statement Block; }
else
{ Statement Block; }
Example :
Private void jButton1ActionPerformed(java.awt.event.ActionEventevt) {
// Code to check eligibility to vote with else condition:
if (Integer.parseInt(jTextField1.getText())>=18)
JOptionPane.showMessageDialog(null,"You are eligible To VOTE");
else
JOptionPane.showMessageDialog(null,"You are NOT eligible To VOTE");
}
3. Nested if else: These control structures are used to test for multiple conditions as against the simple if statement which can be used to test a single condition. The syntax of nested if else is as follows:
Syntax:
 if (conditional expression1)
{ statements1; }
 else if (conditional expression2)
 { statements2; }
 else if (conditional expression3)
 { statements3; }
else
{ statements4; }
Example :  Code for the Week Day Finder Application
Private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
// To find the day of the week
if (jRadioButton1.isSelected())
jTextField1.setText("Monday");
else if (jRadioButton2.isSelected())
jTextField1.setText("Tuesday");
else if (jRadioButton3.isSelected())
jTextField1.setText("Wednesday");
else if (jRadioButton4.isSelected())
jTextField1.setText("Thursday");
else if (jRadioButton5.isSelected())
jTextField1.setText("Friday");
else if (jRadioButton6.isSelected())
jTextField1.setText("Saturday");
else if (jRadioButton7.isSelected())
jTextField1.setText("Sunday");
else
jTextField1.setText("Day - Not Selected");
}
**Do not use a semicolon after the parenthesis of the conditional expression of the if statement.

2. switch:
This selection statement allows us to test the value of an expression with a series of character or integer values. On finding a matching value the control jumps to the statement pertaining to that value and the statement is executed, till the break statement is encountered or the end of switch is reached. The syntax of the switch statement is as follows:
switch (Variable/Expression)
{
case Value1 : statements1 ; break ;
 case Value2 : statements2 ; break ;
default: statements3 ;
}
** Always include a default clause in your switch statement
Example :  
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
// Code to calculate discount depending upon customer type:
double FinalAmount=0;
double BillAmount = Double.parseDouble(jTextField1.getText());
switch(jComboBox1.getSelectedIndex())
{
case 0: FinalAmount=BillAmount; //No Discount for new customer
break;
case 1: FinalAmount=0.90*BillAmount; //10% Discount for silver
break;
case 2: FinalAmount=0.80*BillAmount; //20% Discount for gold
break;
case 3: FinalAmount=0.70*BillAmount;//30% Discountfor platinum
break;
default:FinalAmount=BillAmount;
}
jTextField2.setText(Double.toString(FinalAmount));
}

(b) Looping:

These statements are used to perform a set of instructions repeatedly while the condition is true.
1.      For loop: The syntax of the for loop is:
for( initialization; test expression; increment/decrement expression)
{ statements; }
Example :  

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int LastNumber=Integer.parseInt(jTextField1.getText());
if (jRadioButton1.isSelected()) //Even Numbers required
{
for (int I=2;I<=LastNumber;I+=2)
jTextArea1.setText(jTextArea1.getText()+" " +Integer.toString(I));
}
else if (jRadioButton2.isSelected())//Odd Numbers required
{
for (int I=1;I<=LastNumber;I+=2)
jTextArea1.setText(jTextArea1.getText()+" " +Integer.toString(I));
}
else
JOptionPane.showMessageDialog(this,"Click to select [Even] or [Odd] Option");
}

2.      While loop : The while loop is an entry-controlled loop. It means that the loop condition is tested before executing the loop body. If the loop condition is initially false, for the first iteration, then loop may not execute even once. The syntax of the while loop is as follows:
 while(test expression)
{ loop body }
Example :  
int f=1,i=2;
while(++i<5)
 {
       f*=i;
 }
System.out.println(f);

3.       do while : Do..While loop is an exit-controlled loop. In the do..while loop, the test occurs at the end of the loop. This ensures that the do..while loop executes the statements included in the loop body at least once. The syntax of the loop is as follows:
 do
{
 loop body
}while (test expression);

Example :  
int f=1,i=2;
do {
       f*=i;
 }while(++i<5);
System.out.println(f);

(c) Jump:


1.     break: The break is used to break from an enclosing do, while ,for or switch statement. Syntax: break;
2.     continue: The continue statement stops the execution of the current iteration and causes control to begin with next iteration. Syntax: continue;
3.     return : Return is used to return value from the method Syntax: Return <value>;


Java IDE Programming

Commonly available Swing Controls in Java are:
·         jFrame: A Frame is a container control, in which all the controls can be placed.
·         jLabel: JLable allows placing un-editable text or picture.
·         jTextField: JTextFeild allows placing editable text on the Frame/Pane. User can enter text in a textFiled during runtime.
·         jbutton: is used to initiate an action when it is clicked.
·         jList: is a group of values or items from which one or more selections can be made.
·         jComboBox: jComboBox is similar to jList but also allow to enter editable text during run time. It is a combination of jTextFiled and jList.
·         jRadioButton: Allow us to choose a single item from a group of jRadioButton options.
·         jCheckBox: Allow us to choose one or more items from a group of jCheckBox options. jPasswordField: Allow us to enter a text during the run time but shows an encrypted text instead of the original text
·         jTextArea: JTextArea is a multi-line text component to enter or edit text.

Swing Controls Methods and Properties: 


These are the Swing Controls available with NetBeans IDE and their concern methods and properties are given below.
Swing Controls
Methods
Properties
jButton
• getText()
• setText()

• Background
• Enabled
• Font
• Foreground
• Text
• Label
jLabel
• getText()

• Background
• Enabled
• Font
• Foreground
• Text
jTextField
• getText()
• isEditable()
isEnabled()
setText()

• Background
• Editable
• Enabled
• Font
• Foreground
• Text
jRadioButton
• getText()
setText()
isSelected()
setSelected()

• Background
• Button Group
• Enabled
• Font
• Foreground
• Label
jCheckBox
• getText()
setText()
isSelected()
setSelected()

• Button Group
• Font
• Foreground
• Label
• Selected
• Text

jButtonGroup

• Add

jComboBox
•getSelectedItem() •getSelectedIndex()
setModel()

• Background
• ButtonGroup
• Editable
• Enabled
• Font
• Foreground
• Model
• SelectedIndex
• SelectedItem
jList
getSelectedValue()

• Background
• Enabled
• Font
• Foreground
• Model
• SelectedIndex
• SelectedItem
• SelectionMode

jTable
• addRow()
• getModel()

• model

JoptionPane
showMessageDialog()

• getRowCount()
• removeRow()
• addRow()



2 comments:

  1. Visit my blog
    khushiaggarwal.blogspot.com

    ReplyDelete
  2. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.Surya Informatics

    ReplyDelete