Results 1 to 3 of 3

Thread: If / else / else if / switch box in Java

  1. #1
    Join Date
    Oct 2008
    Posts
    54

    If / else / else if / switch box in Java

    A condition will allow you to execute a portion of code or not according to the result of Boolean variables, ie you can say "if X is false then I do that, if this and if any of the previous n 'is met, I had rather that. "

    All this is in Java code with instructions. The most common are the if / else.

    The if / else:

    The if statement is translated into french by "if". It allows you to perform an action if a condition is true or false:

    Code:
    if (condition) 
    ( 
    / / code 
    ) 
    
    Example of if statement: 
    
    public class Test 
    ( 
    public int variable = 20; 
    
    public Test () 
    ( 
    if (variable = 20) 
    ( 
    System.out.println ( "The variable 'variable' is equal to 20"); 
    ) 
    ) 
    )
    In our example, the text displayed as the variable is equal to 20. Note that we use the == operator for comparison with data of primitive types. The equals sign is simply a sign of trust.

    Else instruction is translated into french by "otherwise". It allows you to perform an action if the first condition in the "if" is not performed. Here is an example where we show that the variable is not equal to 20

  2. #2
    Join Date
    Oct 2008
    Posts
    54

    Re: If / else / else if / switch box in Java

    Example Instruction else:

    Code:
    public class Test 
    ( 
    public int variable = 15; 
    
    public Test () 
    ( 
    if (variable = 20) 
    ( 
    System.out.println ( "The variable 'variable' is equal to 20"); 
    ) 
    else 
    ( 
    System.out.println ( "The variable 'variable' is not equal to 20"); 
    ) 
    ) 
    )
    The instruction else if:

    Here is an example of instruction else if I think that will be more explicit than a thousand words:

    Code:
    public class Test 
    ( 
    public int variable = 15; 
    
    public Test () 
    ( 
    if (variable = 20) 
    ( 
    System.out.println ( "The variable 'variable' is equal to 20"); 
    ) 
    else if (variable = 15) 
    ( 
    System.out.println ( "The variable 'variable' is not equal to 20 and is equal to 15"); 
    ) 
    else if (variable = 10) 
    ( 
    System.out.println ( "The variable 'variable' is not equal to 20 or 15 and is 10"); 
    ) 
    else 
    ( 
    System.out.println ( "The variable 'variable' is not equal to 20 or 15 or 10"); 
    ) 
    ) 
    )

  3. #3
    Join Date
    Oct 2008
    Posts
    54

    Re: If / else / else if / switch box in Java

    The switch / case:

    The switch is useful when you need to manage a lot of if / else if / else. It has a shorter syntax and is more appropriate for this type of case.

    Operation:

    Code:
    switch (variable) 
    ( 
    case 'value1': 
    action1; 
    break; 
    case 'value2': 
    action2; 
    break; 
    case 'value3': 'value4': 
    action 3; 
    break; 
    default: 
    Action 4; 
    break; 
    )

    Here in our example of how this instruction, we note the case 'value3': 'valeur4':. This line means that if the variable variable is either 'value3' or 'valeur4', then execute "Action 4". You can save as many values as you want. If no value matches, or the instructions contained in the default run block (s)

    Here is our previous example (based on else if) translated with the switch / case:

    Code:
    public class Test 
    ( 
    public int variable = 15; 
    
    public Test () 
    ( 
    switch (variable) 
    ( 
    case 20: 
    System.out.println ( "The variable 'variable' is equal to 20"); 
    break; 
    case 15: 
    System.out.println ( "The variable 'variable' is not equal to 20 and is equal to 15"); 
    break; 
    case 10: 
    System.out.println ( "The variable 'variable' is not equal to 20 or 15 and is 10"); 
    break; 
    default: 
    System.out.println ( "The variable 'variable' is not equal to 20 or 15 or 10"); 
    break; 
    ) 
    ) 
    )
    Negation:

    The denial is used when one wishes, for example, say "if this variable is not equal to". You can use the operator! = (Which means "different") but you can also simply use the exclamation point, which means "no". Beware, the exclamation point applies to a Boolean variable (true or false), so you should put some brackets.

    Example:

    Code:
    public class Test 
    ( 
    public int variable = 15; 
    
    public Test () 
    ( 
    if (! (variable = 20)) 
    ( 
    System.out.println ( "The variable 'variable' is not equal to 20"); 
    ) 
    ) 
    )

Similar Threads

  1. Switch in Java
    By cloud101 in forum Software Development
    Replies: 3
    Last Post: 19-01-2012, 07:04 PM
  2. How to use switch statement in java
    By Raju Chacha in forum Software Development
    Replies: 2
    Last Post: 19-01-2012, 06:40 PM
  3. Help!! Java 5, boxing and auto switch.
    By Dewei in forum Software Development
    Replies: 5
    Last Post: 22-09-2010, 10:15 PM
  4. How to switch between Java Virtual Machine
    By Tarank in forum Software Development
    Replies: 3
    Last Post: 18-09-2010, 04:19 PM
  5. Switch construct in java
    By mehdi in forum Software Development
    Replies: 3
    Last Post: 06-08-2009, 06:59 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,523,853.92111 seconds with 17 queries