473,323 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,323 developers and data experts.

Program Structures

1,510 Expert 1GB
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
Apr 11 '07 #1
0 3969

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually...
2
by: Kalpana Shridhar | last post by:
I am on a project where I am converting COBOL to C++ on the mainframe. I am new to COBOL and have a lot of questions - any websites or any suggestions from experienced people will be really...
10
by: atlanta | last post by:
this is a simple C++ program to write. "Write a complete and functioning structured program that successfully compiles on Visual C++ 6, that uses two-dimensional array (5x5) that stores...
5
by: BabyBoo24 | last post by:
I must create ten multiple choice questions.The questions will be weighted Question1: 10.0 points question2: 15.0 points, etc. Each question has different weights. The weights must be defined as...
7
by: Randy Yates | last post by:
Hi, I work in an embedded environment in which we often use a mix of C and assembly code. Thus a recurring requirement is to be able to take a C header file with structure definitions as input...
6
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler...
2
by: thomasfarrow | last post by:
At work, our development team has a development standards document that insists Structures should never be used. I'm looking to change this standard but need a suitable argument in order to make...
16
by: Martin Joergensen | last post by:
Hi, I wanted to try something which I think is a very good exercise... I read in data from the keyboard and store them in a structure. There's a pointer called "data_pointer" which I use to...
29
by: 63q2o4i02 | last post by:
Hi, I'm interested in using python to start writing a CAD program for electrical design. I just got done reading Steven Rubin's book, I've used "real" EDA tools, and I have an MSEE, so I know what...
11
by: ruffiano | last post by:
A colleague of mine who is a C developer wrote several functions in C which I now need to invoke in my C++ application. This is normally not a problem except that the header file that he wrote...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.