all java programming statements must end with a period
Statements, Declarations, and Control Structures¶
Statements and Declarations¶
The following codification samples are all equivalent, and illustrate the use of comments and working with integer variables:
[Ada]
-- -- Ada computer program to declare and modify Integers -- procedure Main is -- Versatile declarations A , B : Whole number := 0 ; C : Whole number := 100 ; D : Integer ; begin -- Ada uses a regular assignment statement for incrementation. A := A + 1 ; -- Regular addition D := A + B + C ; death Main ;
[C++]
/* * C++ program to declare and modify ints */ int chief ( int argc , const char * argv []) { // Variable declarations int a = 0 , b = 0 , c = 100 , d ; // C++ shorthand for incrementation a ++ ; // Regular addition d = a + b + c ; }
[Java]
/* * Java program to hold and alter ints */ public course of instruction Main { public static void main ( String [] argv ) { // Variable declarations int a = 0 , b = 0 , c = 100 , d ; // Java tachygraphy for incrementation a ++ ; // Regular add-on d = a + b + c ; } }
Statements are terminated by semicolons in all three languages. In Ada, blocks of code are surrounded by the undemonstrative words begin
and stop
rather than away wavy braces. We can use both multi-delineate and single-ancestry annotate styles in the C++ and Coffee codification, and only single-short letter comments in the Ada code.
Ada requires variable declarations to be made in a specific area called the declarative part, seen here before the begin
keyword. Multivariate declarations start with the identifier in Ada, as opposed to start with the type American Samoa in C++ and Java (also note ADA's use of the :
separator). Specifying initializers is different A well: in Ada an initialization expression can apply to multiple variables (but will atomic number 4 evaluated separately for each), whereas in C++ and Java each variable is initialized individually. Altogether three languages, if you habituate a function as an initializer and that function returns different values along every invocation, each adaptable will get initialized to a different value.
Rent's move on to the imperative statements. Ada does not provide ++
operating theater --
shorthand expressions for increment/decrease operations; it is necessary to use a full assignment statement. The :=
symbol is old in Ada to perform value assignment. Unlike C++'s and Java's =
symbol, :=
send away not be used as part of an expression. And then, a statement like A := B := C ;
doesn't add up to an Adenosine deaminase compiler, and neither does a article wish if A := B then ...
. Both are compile-clip errors.
You can nest a block of code within an outer block if you wish to produce an inner scope:
with Ada.Text_IO ; use of goods and services Ada.Text_IO ; procedure Important is begin Put_Line ( "Before the inner block" ); declare Alpha : Integer := 0 ; begin Alpha := Alpha + 1 ; Put_Line ( "In real time inside the inner block" ); end ; Put_Line ( "After the inner choke up" ); end Main ;
It is OK to suffer an empty declarative split or to leave off the declarative portion entirely — just starting signal the inner block with begin
if you have No declarations to piss. However it is not OK to wealthy person an empty succession of statements. You must at least provide a null ;
statement, which does nothing and indicates that the omission of statements is intentional.
Conditions¶
The use of the if
command:
[ADA]
if Varied > 0 so Put_Line ( " > 0 " ); elsif Variable quantity < 0 then Put_Line ( " < 0 " ); else Put_Line ( " = 0 " ); end if ;
[C++]
if ( Variable > 0 ) cout << " > 0 " << endl ; else if ( Variable < 0 ) cout << " < 0 " << endl ; else cout << " = 0 " << endl ;
[Java]
if ( Variable > 0 ) System . out . println ( " > 0 " ); else if ( Variable < 0 ) Organisation . unconscious . println ( " < 0 " ); else Scheme . out . println ( " = 0 " );
In ADA, everything that appears 'tween the if
and then
keywords is the provisory expression — no parentheses required. Comparison operators are the identical, omit for equality ( =
) and inequality ( /=
). The English wrangle non
, and
, and or
substitute the symbols !
, &
, and |
, respectively, for playing mathematician operations.
It's more customary to use &&
and ||
in C++ and Java than &ere;
and |
when writing boolean expressions. The remainder is that &&
and ||
are short-circuit operators, which evaluate price alone As necessary, and &
and |
will categorically appraise altogether terms. In Adenosine deaminase, and
and or
will appraise all terms; and then
and operating theatre else
direct the encyclopedist to apply short circuit evaluation.
Here are what switch/guinea pig statements look like:
[ADA]
case Variant is when 0 => Put_Line ( "Zero" ); when 1 .. 9 => Put_Line ( "Positive Digit" ); when 10 | 12 | 14 | 16 | 18 => Put_Line ( "Even Number betwixt 10 and 18" ); when others => Put_Line ( "Something else" ); goal case ;
[C++]
switch ( Variable ) { case 0 : cout << "Zipp" << endl ; break ; case 1 : casing 2 : pillow slip 3 : case 4 : case 5 : case 6 : case 7 : event 8 : slip 9 : cout << "Positive Digit" << endl ; snap off ; case 10 : sheath 12 : case 14 : case 16 : case 18 : cout << "Flat Number 'tween 10 and 18" << endl ; break ; default : cout << "Something else" ; }
[Java]
switch ( Variable ) { causa 0 : System . out . println ( "Zero" ); break ; caseful 1 : case 2 : case 3 : case 4 : case 5 : suit 6 : case 7 : case 8 : case 9 : System . out . println ( "Positive Digit" ); better ; case 10 : instance 12 : case 14 : case 16 : case 18 : System . impermissible . println ( "Even Number between 10 and 18" ); break ; default option : System of rules . away . println ( "Something else" ); }
In Ada, the case
and end subject
lines surround the whole case statement, and each case starts with when
. So, when programming in Ada, replace switch
with example
, and replace incase
with when
.
Case statements in Ada require the use of distinct types (integers operating room numeration types), and require all possible cases to cost covered aside when
statements. If not whol the cases are handled, or if duplicate cases exist, the program will not compile. The default case, nonremittal :
in C++ and Java, can be specific using when others =>
in Ada.
In Ada, the break
instruction is implicit and program execution wish ne'er fall through to sequent cases. In order to fuse cases, you can specify ranges victimisation ..
and enumerate disjoint values using |
which neatly replaces the multiple case
statements seen in the C++ and Coffee versions.
Loops¶
In Ada, loops always start with the iteration
reserved word and ending with end loop
. To leave the eyelet, use exit
— the C++ and Java equivalent being fall apart
. This statement can set apart a terminating circumstance exploitation the exit when
sentence structure. The loop
opening the block can be preceded by a while
or a for
.
The while
loop is the simplest one, and is very similar across each three languages:
[Ada]
while Variable < 10_000 coil Variable := Variable * 2 ; end loop ;
[C++]
while ( Variable < 10000 ) { Variable = Variable * 2 ; }
[Java]
while ( Versatile < 10000 ) { Shifting = Variable * 2 ; }
ADA's for
curl, withal, is quite different from that in C++ and Java. It always increments or decrements a loop index inside a discrete range. The loop index (or "loop parameter" in Ada parlance) is local to the scope of the loop and is implicitly incremented or decremented at each iteration of the eyelet statements; the political platform cannot directly change its value. The type of the loop parameter is derived from the range. The range is always given in ascending order even if the loop iterates in descending order. If the opening bound is greater than the ending bound, the interval is considered to be empty and the cringle table of contents will non represent executed. To specify a loop iteration in decreasing tell, economic consumption the reverse
reserved formulate. Here are examples of loops going in both directions:
[Ada]
-- Outputs 0, 1, 2, ..., 9 for Variable in 0 .. 9 loop Put_Line ( Integer ' Image ( Varying )); oddment loop ; -- Outputs 9, 8, 7, ..., 0 for Variable in reverse 0 .. 9 loop Put_Line ( Integer ' Image ( Variable quantity )); finish grummet ;
[C++]
// Outputs 0, 1, 2, ..., 9 for ( int Variable = 0 ; Variable <= 9 ; Protean ++ ) { cout << Variable << endl ; } // Outputs 9, 8, 7, ..., 0 for ( int Inconstant = 9 ; Variable >= 0 ; Variable -- ) { cout << Variable << endl ; }
[Coffee]
// Outputs 0, 1, 2, ..., 9 for ( int Variable = 0 ; Variable <= 9 ; Variable ++ ) { System . out . println ( Variable ); } // Outputs 9, 8, 7, ..., 0 for ( int Variable = 9 ; Variable >= 0 ; Variable -- ) { System . outgoing . println ( Unsettled ); }
Ada uses the Integer
type's ' Image
attribute to convert a numerical value to a Drawstring. In that respect is no unstated conversion between Whole number
and String
as there is in C++ and Java. We'll have a more in-depth look at much attributes by and by happening.
It's prosperous to express iteration over the table of contents of a container (for example, an lay out, a leaning, or a map) in Ada and Java. For good example, assuming that Int_List
is defined as an array of Whole number values, you can use:
[ADA]
for I of Int_List loop Put_Line ( Integer ' Image ( I )); end loop ;
[Coffee]
for ( int i : Int_List ) { System . impermissible . println ( i ); }
all java programming statements must end with a period
Source: https://learn.adacore.com/courses/Ada_For_The_CPP_Java_Developer/chapters/04_Statements_Declarations_and_Control_Structures.html
Posting Komentar untuk "all java programming statements must end with a period"