Sunteți pe pagina 1din 30

Conditional

Statement
Arithmetic
Operators
Operator Name Description Example

+ Addition Adds together two values x+y

- Subtraction Subtracts one value from another x-y

* Multiplication Multiplies two values x*y

/ Division Divides one value from another x/y

% Modulus Returns the division remainder x%y

++ Increment Increases the value of a variable by 1 ++x

-- Decrement Decreases the value of a variable by 1 --x


Java
Comparison
Operators
Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


Java Logical
Operators
Operator Name Description Example

Returns true if both x < 5 && x <


&& Logical and
statements are true 10

Returns true if one of


|| Logical or x < 5 || x < 4
the statements is true

Reverse the result,


!(x < 5 && x <
! Logical not returns false if the
10)
result is true
◦One of the most widely
used keyword in
programming is the if
keyword. If statement is
The if used when the program is
statement made to choose one of two
paths. This is the
foundation of decision
making in programs.
Syntax:
if(condition)
{
statement(s);
}
Problem
Write a program that will determine
the user if its qualified to vote.
package ScannerInput;
import java.util.Scanner;
public class ElseIF1 {

public static void main(String args[])


{

Scanner in = new Scanner (System.in);


int num;

System.out.println("Enter your age");


num = in.nextInt();

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 {

public static void main(String args[])


{
Scanner in = new Scanner (System.in);
int num;

System.out.println("Enter your age");


num = in.nextInt();

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 {

public static void main(String args[])


{
Scanner in = new Scanner (System.in);

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 {

public static void main(String args[])


{
Scanner in = new Scanner (System.in);

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.

S-ar putea să vă placă și