DO NOT HAVE A SEMICOLON AFTER if CONDITION.

What is logically wrong with the statement

if (amount <= balance)
   newBalance = balance - amount;
   balance = newBalance;

and how do you fix it?

Answer: Only the first assignment statement is part of the if statement. Use braces to group both assignment statements into a block statement.

 

Need else or not?

import java.util.Scanner;

public class Tester
{
   public static void main(String [] args)
   {
	int score ;
	char grade;

	Scanner input = new Scanner(System.in);
	System.out.println("Enter your score");
	score = input.nextInt();

        if (score >=90) 
           grade = 'A'; 
        else 
          if (score >=80) 
              grade = 'B'; 
          else 
            if (score >=70) 
                grade = 'C'; 
            else 
              if (score >=60) 
                  grade = 'D'; 
              else 
                  grade = 'F';

	System.out.println("Your test score is "+ score 
         + ", which is equivalent to the grade " + grade + ".");
   }
}

 

import java.util.Scanner;

public class Tester
{
   public static void main(String [] args)
   {
	int score ;
	char grade;

	Scanner input = new Scanner(System.in);
	System.out.println("Enter your score");
	score = input.nextInt();

        if (score >=90) 
           grade = 'A'; 
         
        if (score >=80) 
           grade = 'B'; 
         
        if (score >=70) 
           grade = 'C'; 
           
        if (score >=60) 
           grade = 'D'; 
        else    
           grade = 'F';

	System.out.println("Your test score is "+ score 
         + ", which is equivalent to the grade " + grade + ".");
   }
}

 

 
A ticket for a movie costs 
(i) $10 for a person who is older than 13; 
(ii) $6 for a senior who is 60 or older 
(iii) $0 for a kid who is 13 or younger.   
What is wrong with the following segment?

if(age > 13)
   ticketPrice = 10;
else
   if(age > = 60)
     ticketPrice = 6;