Personal tools
You are here: Home → Comp 170 Introduction to OOP → Lab Materials → Lab5 → controlStatement
Document Actions

controlStatement

by Mit Patel — last modified 2006-02-15 06:06 PM

Examples of if statements

Click here to get the file

Size 1.7 kB - File type text/x-java

File contents

import java.util.Scanner;

public class ControlFlow
{
    public static void main(String args[]){
        Scanner input = new Scanner( System.in );
        
        double i;
        
        System.out.print("Enter an integer: ");
        i = input.nextDouble();
        
        //----------------------IF WITHOUT ELSE----------------------------------------
        if (i > 0){
            System.out.println("i is greater than 0");
        }
        
        if (i < 0){
            System.out.println("i is smaller than 0");
        }
        
        if (i == 0){
            System.out.println("i is equal to 0");
        }
        //------------------------------------------------------------------------------
        
        if (i == 0){
            System.out.println("i as a zero is boring");        
        } else {
            System.out.println("i as a non zero is interesting");
            }
        
        
        //----------------------MULTIBRANCH IF WITH ELSE--------------------------------
        if (i > 0){
            i = i+10;
            System.out.println("i changed to: " +i);
        }else if (i < 0){
            i++;
            System.out.println("i is " +i);
        }else if (i == 0){
            i = 90;
            System.out.println("i can't be zero.  So I change to " +i);
        }
        
        //---------------------NESTED IF------------------------------------------------
         
         if(i < 10){
            System.out.print("Cold");
         } else{
            if (i < 50){
              System.out.print ("Normal");
            }
            else 
              System.out.print("Hot");
         }
         
    }

}
« October 2027
Su Mo Tu We Th Fr Sa
12
3456789
10111213141516
17181920212223
24252627282930
31
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: