Difference between revisions of "The If-Statement"

From CompSciWiki
Jump to: navigation, search
Line 22: Line 22:
 
</pre>
 
</pre>
 
This is pretty simple, and yet is there another any other way of computing the absolute value, and if so why isn't there an easer math formula? Well lets think about it; is this not the way we as humans compute the absolute value of a number? I would say no it isn't. This technique is way too mathy, and if we did compute the number it would take forever. what we do instead is we check to see if the number is negative and if it is then we remove the negative sign. So braks this sentence up into three parts <BR>
 
This is pretty simple, and yet is there another any other way of computing the absolute value, and if so why isn't there an easer math formula? Well lets think about it; is this not the way we as humans compute the absolute value of a number? I would say no it isn't. This technique is way too mathy, and if we did compute the number it would take forever. what we do instead is we check to see if the number is negative and if it is then we remove the negative sign. So braks this sentence up into three parts <BR>
(1)we check to see if the sign is negative. <BR>
+
(1) we check to see if the sign is negative. <BR>
(2)if it is then we want to do somthing <BR>
+
(2) if it is then we want to do somthing <BR>
(3)that somthing is we want to remove the negative sign <BR>
+
(3) that somthing is we want to remove the negative sign <BR><BR>
 
Rember what I sayed earlyer: "that sometimes you may not want your program to read every line of code".<BR>  
 
Rember what I sayed earlyer: "that sometimes you may not want your program to read every line of code".<BR>  
 
Here we only want to execute the 3rd part if the sign is negative, conversely<BR>
 
Here we only want to execute the 3rd part if the sign is negative, conversely<BR>

Revision as of 04:09, 2 December 2007

COMP 1010 Home > Control Structures


Introduction

If-Statements are one of the simplest and most widely used control structures. This section will teach you how they are used and when and why to use them.

   

{{{Body}}}

The If-Statement

What is an if statement?

Now that you have a firm grasp of variables you may have wondered what is the point of having Boolean as a variable?

Yes I'm aware that you aredy know that they can store eather true or False, but what is the importance in storing this information if all you know is how to do is simply print its contents out to the screen?

In this section you will be able to see why boolean is important. So far the programs you have written have read every line from the top to the bottom, but sometimes you may not want your program to read every line of code. now you are probly asking yourself why wouldn't I want my program to read every line of code every time! if I didn't want my program to run some lines of code I wouldn't have ritten them in the first place. Now let me explane this with an example

First Example

lets say we want the absolute value of a number, there are two ways of doing this
there is a math way, and there is another way. The math way involves some math you learnt back in your high school days. To get the absolute value of a number you take some number and square it after that you take the square root of that answer. So lets program this.

00 public static void main(String[] args)
01 {
02     int number;
03     number = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter a number"));
04     number = number * number;
05     number = Math.Sqrt(number);
06     System.out.println("The number" + number);
07 }//main

This is pretty simple, and yet is there another any other way of computing the absolute value, and if so why isn't there an easer math formula? Well lets think about it; is this not the way we as humans compute the absolute value of a number? I would say no it isn't. This technique is way too mathy, and if we did compute the number it would take forever. what we do instead is we check to see if the number is negative and if it is then we remove the negative sign. So braks this sentence up into three parts
(1) we check to see if the sign is negative.
(2) if it is then we want to do somthing
(3) that somthing is we want to remove the negative sign

Rember what I sayed earlyer: "that sometimes you may not want your program to read every line of code".
Here we only want to execute the 3rd part if the sign is negative, conversely
we don't want to execute the 3rd part if the sign is positive
So how is this done? This is done by the use of somthing called if-Statement hence the name of the section


00 public static void main(String[] args)
01 {
02     int number;
03     bool isNumberNegative;
04
05     number = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter a number"));
06
07       //Check to see if the number is negative
08     isNumberNegative = number < 0;
09
10     //and if the number is negative
11     if(isNumberNegative)
12     {
13        //we remove the negative sign
14        number = number * -1;
15     }
16
17     System.out.println("The number" + number);
18
19 }//main


A First Example

In this example the user is asked for a number and the program tells the user if the number is even. Pay careful attention to lines 05 to 08.

00 public static void main(String[] args)
01 {
02     int number;
03     number = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter a number"));
04
05     if(number%2 == 0)
06     {
07         System.out.println("The number is even");
08     }//if
09
10 }//main

The keyword if is used to tell the computer to make a decision based on a condition. If the condition returns a value of true then the lines of code contained inside the body is executed. If the condition returns false the code inside the body is skipped entirely.

In this example the condition is number%2 == 0. You can always identify the condition of an if-statement by looking at what is contained in the parentheses after the word if. The condition must always return either true or false. Any other values (such as a number, string, or character) will result in an error when you try to compile your program.

The body of the if-statement is everything contained within the curled braces. In this case the body contains one line: System.out.println("The number is even");.

Together the keyword if, the condition, and the body form what is called an if block or if statement.


Anatomy of the If-Statement

As described in the previous example an if-statement is made up of several parts:

  • the keyword if
  • the condition
  • the body

The general form for the if-statement is:

if (condition)    //"if" and condition on same line, condition is contained inside parentheses
{
    body          //all statements that should be executed if and only if the condition returns a value of true
}//if

Normally the body is contained inside braces (the {...} characters). However, if the body is only one statement these braces may be omitted. Thus:

if(condition)
{
    System.out.println("The condition was true");
}

and

if(condition)
    System.out.println("The condition was true");

are the same. If the body has more than one statement it is very important to put braces around it. If not then the if-statement will only control the line of code right after the if(condition) line and all others would be executed regardless. It's often a good practice to always put braces around the body, even if it is only one line so as to avoid issues if you wish to add to the statement.