Sponsored links
 
  Search in the Site:

SESSION 6 Part7
Basics of how to write - continued
  Statements

Basics of how to write: Statements
 
Statements are
"a single process".


For example, the process thistime of
System.out.println("Hello world!");
is also a statement.

A program is something that
creates a large process by bringing
together smaller processes
.

Put another way, a program can also
be called a "collection of  statements".
Ummm…

It's like I get it,
but I don't get it.

So it's like,
statement =
process =
command
, right?
Yes, yes,
statements and
processes
and commands
are all similar things,
just from different
viewpoints.

We're not scholars,
we don't have to be
precise.
Still no good?
  Kind of…


There are different types
of statements, such as:
* expression statements
* declarative statements
* control statements


This call, which is a "standard
output process (with a new line)",
is an expression statement.

In future, we will be studying
various types of statements,
including control statements
such as   the 'if' and 'switch'
statements.
  Anyway, just try
and keep them
in mind.
Again,
kind of…
 

Okay!




Basics of how to write:Statements
  Rules for statements
1 Always write a ";" (semicolon) at the end of a statement.
2 However, you don't need a ";" (semicolon) at the
end of a
block.

There are several rules for how to write statements,
so try to remember them.


Always be sure to put a
 ";" (semicolon) at the end
of a statement.


This defines that it is the end
of the command.

You can think of this as being like the "." (period)
at the end of a sentence.


If we were to translate this
process, it would be something like
"make this display 'hello world' in
the standard output."

Also, take care that you confuse it
with the ":" (colon).
Try looking
at the picture
again.

Oh, this is a
 good example,
if I do say so myself.

"Hello.".

That'd be weird,
wouldn't it?
 
I see…

But you don't need a ";"
at the end of a block.


This is just like how
a "." isn't needed
after double quotes
in English.


Hey, hey,
is it okay to write
two commands
on the same line?
Yes, it's okay to write two
on the same line.

In that case, they will be run
in order from the first one.


But it becomes difficult to read,
so usually it's one per line.
Try and do it like that.


Also, you can put a new line in
the middle of a statement.
And in brackets, too.

However, you can't put a new line
in the middle of a command.
 
  If you break up a
command word part
way through,
the meaning is lost.

It becomes like
two different words.


Important points for statements
 
    Two or more statements on a single line is possible.
(But you need to use semicolons.)
   
                 System.out.println("Hello ");System.out.println("world!");    
 
    Inserting new lines and blank spaces within ( ) is possible.
This won't change the processing result.
   
                 System.out.println(
                                                          "Hello world!"      
);
   
         
    New lines partway through " " are not possible.    
                 System.out.println("He
                                             llo world!"
);
   
         
    However, if you close the " " once, you can link them
to the next " " by using +. You can link them even on
separate lines.This won't change the processing result.
   
                System.out.println("He" + "llo w" +
                                                                         "or"
                                            
+ "ld!");
   
         
    Inserting new lines and blank spaces partway through a command
is not possible.
   
                 System.out.prin
                                  tln(
"Hello world!");
   
         
                 System.out.prin     tln("Hello world!");    
 
        indicates half-width spaces.


Wow, so you can
connect " "with +.

And the + is okay on the
previous line or the
next line,huh?

But if you write it like the
example, the position of the
+ is difficult to understand.
It looks as
if the "or" isn't
attached to
either +.

That's because
this is just an example
showing that you can
do this sort of thing.
It is, isn't it?

Generally, the linking of
character string data…

System.out.println("He" +
                            "llo w" +
                            "or" +
                            "ld!");
is consolidated at the end of
sentences like this.

In future we'll take a more
detailed look at
the operation of character
strings.

  Indents

   
Indents make programs easier to read
by repositioning the characters.

Mainly, they line up the text with the
Tab key via the IDE's automatic styling
feature.

I explained a bit about them at the
beginning of this session,
but this is something that's very important.

As an example,
let's take a look at KNMain.java with the
indents removed.

It's difficult to understand which are the
class brackets,
and which are the main function brackets,
isn't it?
 
 
Woah…

Which block
is it in?

 
   
 


Your first program code(KotoTest's KNMain.java)Without indents
 
001   /*    
002   * To change this template, choose Tools | Templates    
003    * and open the template in the editor.    
004    */    
005        
006   package kototest;    
007        
008    /**    
009    *    
010    * @author Koto    
011    */    
012    public class KNMain {
013        
014   /**    
015   * @param args the command line arguments    
016   */    
017   public static void main(String[] args) {    
018   // TODO code application logic here    
019        
020   System.out.println("Hello world!");    
021        
022   }    
023        
024 }
 


You see, it's easier to understand
when it has indents, isn't it?

You use various different brackets
with Java,
however they are always a pair,
with a start and an end.

If you haven't indented, and you
mistakenly delete one,
when it comes to which bracket
isn't closed, and which brackets
each thing is nested within…

Well, with a big program, you'll
have no idea what's going on.
It'll also be the cause of bugs.

This is very important for
writing a program that's easy
to read.
So keep it in mind.
"Always strive to
write a progam
that's easy to read."
Right?
At any rate, for now
the IDE will tell you
the bracket pairs.

Try clicking on the bracket.
Oh, you're right!

The color changed.


Your first program code(KotoTest's KNMain.java)
 
001    /*    
002     * To change this template, choose Tools | Templates    
003     * and open the template in the editor.    
004     */    
005        
006   package kototest;    
007        
008    /**    
009     *    
010     * @author Koto    
011     */    
012    public class KNMain {
013        
014           /**    
015            * @param args the command line arguments    
016            */    
017            public static void main(String[] args) {    
018                   // TODO code application logic here    
019        
020                  System.out.println("Hello world!");    
021        
022           }    
023        
024 }
 

 
 
home Content previous page next page
[ad]