Documente Academic
Documente Profesional
Documente Cultură
Statement
Arithmetic
Operators
Operator Name Description Example
== Equal to x == y
!= Not equal x != y
if (num>=18)
{
System.out.println("Qualified To Vote");
}
}
}
◦The condition is usually are relational
expression that sometimes indicates
comparison. If this condition evaluates to
logical true, the statement(s) will be
executed.
◦If the condition is true,
there is an statement to
be evaluated and if the
If-else condition is false, the
statement statement in the else
clause is the one to be
evaluated.
Syntax:
if(condition)
{
statement(s);/ *condition is true*/
}
else
{
statement(s);/ *condition is false*/
}
Problem
Write a program that will
determine the user if its
qualified to vote or not
package ScannerInput;
import java.util.Scanner;
public class ElseIF1 {
if (num>=18)
{
System.out.println("Qualified To Vote");
}
else
{
System.out.println("Not Qualified to Vote");
}
}
◦There are cases that need to
do multiple choices or certain
limitations before reading to
do any action. Those
Nested if instances could be solved
statement using the nested if
statements. It is just like
saying “A box inside a box”. In
programming if statements
within if statements.
Syntax:
If(condition)
{
if(condition)
{
statement(s);/ *condition is true*/
}
else
{
statement(s);/ *condition is false*/
}
else
{
Statement(s)
}
Problem
Write a program that will let the user
to enter his/her age, if 18 to 30 years
old are qualified to join the army, if
lower than 18 too young else too old.
package ConditionalStatement;
import java.util.Scanner;
public class NestedIF {
int age;
System.out.println("Enter your age");
age = in.nextInt();
if(age<=30)
{
if(age>=18)
{
System.out.println("Qualified to Join");
}
else
{
System.out.println("Too Young");
}
}
else
{
System.out.println("Too Old");
}
}
}
Problem
Create a program that will let the
user to enter a number, and then
output the number is ODD or
EVEN.
package ConditionalStatement;
import java.util.Scanner;
public class OddEven {
int num,mod;
System.out.println("Enter a number");
num = in.nextInt();
mod=num%2;
if (mod==0)
{
System.out.println("Even Number");
}
else
{
System.out.println("Odd Number");
}
}
◦Ladderized if-else-if-else
statements, the computer
can decide to choose one
Ladderized and only one of the given
if-else if-else choices/condition. Only the
statement first condition that was
proven true was chosen and
its associated statement(s)
will be executed.
Syntax:
if(condition)
statement1;
else if
statement2;
else if
statement3;
else if
statement4;
else
statement;
Problem
Write a program that will read in a number,
then display the days that correspond the
input number.
Sample output:
1 - Monday
Problem
Write a program that will read in a number,
then display the months that correspond the
input number.
Sample output:
1 - January
◦ The if – else – if ladder can perform
multiple test but the code could be
very hard if the program becomes too
long. Because of this reason, C has
another multiple branch decision-
Switch making mechanism. This is called
Statement switch statement ().this decision-
making statement is mostly used in
menu driven/based programs. Where
in choices had to be made. The
general form of switch () statement is.
Switch(variable)
{
case constant1:
statement(s); break;
case constant2:
statement(s); break;
case constant3:
statement(s); break;
case constant4:
statement(s); break;
default:
statement(s);
}
Problem
Write a program that will read in a number,
then display the months that correspond the
input number.
Sample output:
1 - January
Problem
◦Create a calculator that
input the operator sign.