Connecting Tech Pros Worldwide Forums | Help | Site Map

Program Structures

Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#1   Apr 11 '07
Program Structures
Structured programming has only three structures:
Sequence, Selection and Iteration.

Selection allows choice of paths in program.

The if statement

if (condition)
then operation

e.g. in pseudo code
Expand|Select|Wrap|Line Numbers
  1.         if month is October
  2.         then write "new term"
  3.         if month is June
  4.         then write "exams!"
This may be extended to include an else clause:
a)
Expand|Select|Wrap|Line Numbers
  1.         if mark is greater than or equal to 40
  2.         then write "pass"
  3.         else write "fail"
To take this further:
b) [code]
if mark is greater than or equal to 70
then write "brilliant"
else if mark is greater than or equal to 40
then write "pass"
else write "fail" [code]

Indentation: line else up with corresponding if.

Indentation makes meaning clear. Else is lined up to match the if that it belongs to.
Assume that mark is int
a)
Expand|Select|Wrap|Line Numbers
  1.         if (mark>=40)
  2.              System.out.println("pass");
  3.         else System.out.println("fail");
b)
Expand|Select|Wrap|Line Numbers
  1.         if (mark>=70)
  2.              System.out.println("brilliant");
  3.         else if (mark>=40)
  4.                   System.out.println("pass");
  5.              else System.out.println("fail");
The comparison operators are:
< <= > >= == !=

A program using Scanner to read data from the keyboard and if-then-else:
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class Ifmarks
  4. {
  5.   public static void main(String args[])
  6.     {
  7.      Scanner keyboard=new Scanner(System.in); // open Scanner
  8.      final int PASS = 40;
  9.      final int DISTINCTION = 70;
  10.      System.out.print("type a mark: ");
  11.      int mark = keyboard.nextInt();           //read an integer
  12.      if (mark>=DISTINCTION)
  13.           System.out.println(" brilliant");
  14.      else if (mark>=PASS)
  15.                System.out.println(" pass");
  16.           else System.out.println(" fail");
  17.     }
  18. }
Here we also see the use of constants: if a value will not change it may be declared as a constant which, by convention, are written in upper case. This has the advantage that if a constant changes (such as the distinction mark above) it need only be changed once rather than having to edit multiple occurances in a large program.

More than one statement can be placed on a branch of an if by including in {}. e.g. assuming that i is int indicate if it 0, positive or negative and if negative change its sign

Expand|Select|Wrap|Line Numbers
  1.    if (i<0)
  2.         {
  3.           System.out.println("i is negative, inverting");
  4.           i=-i;
  5.         }
  6.    else
  7.           if (i==0) System.out.println("zero");
  8.           else
  9.           {
  10.             System.out.println("i is positive");
  11.             System.out.println("no change");
  12.           }
  13.  
The else is lined up with corresponding if and the {} are lined up vertically



Closed Thread