Control statements
Control statements
Control statements :-
A control statement works as a determiner for deciding the next task of the other statements whether to execute or not.if Statement :
Simple if statement is the basic of decision-making statements in Java. It decides if certain amount of code should be executed based on the condition.
Syntax:
if (condition) {
Statemen 1; //if condition becomes true then this will be executed
}
Statement 2; //this will be executed irrespective of condition becomes true or false
example :
class IfStatement {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("Number is positive.");
}
System.out.println("This statement is always executed.");
}
}
if…else Statement :
In if…else statement, if condition is true then statements in if block will be executed but if it comes out as false then else block will be executed.
Syntax:
if (condition) {
Statemen 1; //if condition becomes true then this will be executed
}
else
{
Statemen 1; //if condition becomes false then this will be executed
}
Example :
class IfElse {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("Number is positive.");
}
else {
System.out.println("Number is not positive.");
}
System.out.println("This statement is always executed.");
}
}
Assignments :-
If:-
1. Write a program that accepts two integer values from the user and return the larger values.
2. Write a program to compare two numbers use operators like <, >, <=,>=, ==, !=
Input Data:
Input first integer: 25
Input second integer: 39
Expected Output
25 != 39
25 < 39
25 <= 39
3. Check number is even or odd
4. Program to find Largest of three numbers
5. Write a program to calculate the sum of two integers and return true if the sum is equal to a third integer.
Input the first number : 5
Input the second number: 10
Input the third number : 15
Sample Output:
The result is: true
6. Write a program that accepts three integers from the user and return true if two or more of them (integers ) have the same rightmost digit. The integers are non-negative.
Input the first number : 5
Input the second number: 10
Input the third number : 15
Sample Output:
The result is: true
7. Write a program to check whether an given integer is a power of 4 or not.
Given num = 64, return true. Given num = 6, return false.
8. Check leap year
9. Input character is vowel or consonant
10. Given number is positive or negative
11. H- Program to swap two numbers using bitwise operator
12. H- Program to find smallest of three numbers using ternary operator
13. H- Program to find largest of three numbers using ternary operator
14. display Student Grades
Comments
Post a Comment