Friday, May 11, 2012

Control Structures in JAVA!


   There are three(3) control structures use in java that can be used in some specific conditions.

1. if-else structure- use to implement an action in java under specified conditions.
               
*if selection structure does specific action under only one condition/relation.
Example:
If your grade is equal or greater than 50,print "pasado ka". if condition is TRUE, print, if FALSE
ignore/skip the next line.

code:  if(grade=>50)
System.out.println("pasado ka");

*if-else selection structure gives an option on what will the compiler do(2 actions).
Example :
If your grade is equal or greater than 50,print "pasado ka". if false,print("bagsak").

code:   if(grade=>50)
System.out.println("pasado ka");
else
System.out.println("bagsak");

*if structure also called single-selection structure
*if-else structure is called double-selection struture

2. switch structure- is a multiple-selection structure.
unlike if-else structure, switch does multiple actions can be performed.
Example: the user will choice the letter
A.If your grade is 50 above.
B.If your grade is 70 above.
C.If your grade is 80 above.
D.If your grade is 90 above.
 
code:   char pagpipilian;
switch (pagpipilian){
case 'A':
  System.out.println("mag-aral ka");
  break;
case 'B':
  System.out.println("mag-aral pa ng konti");
    break;
case 'C':
  System.out.println("masipag mag-aral");
  break;
case 'D':
  System.out.printlng("huwarang mag-aaral");
  break;
default:
  System.out.println("wala sa pagpipilian");
}
Common Programming Error: Forgetting to put break statement in a switch structure is a logic error.

3.  the third structure is repeatition structures o looping.

No comments:

Post a Comment