Conditional Statements | if, If-else, "if" and "if else", Switch Break | Complete Java Placement Course | Lecture 3
Java - Introduction to Programming
Lecture 3
Conditional Statements
Conditional statements are a fundamental concept in programming. They are used to make decisions based on certain conditions, allowing the program to perform different actions depending on the input or data it receives. In this article, we'll take a closer look at conditional statements in programming and how they can be used effectively.
Conditional statements, also known as "if-then" statements, are used in programming to execute certain code if a condition is met. In other words, the program will perform a particular action if a particular condition is true. For example, a program may check if a user has entered the correct password before granting access to a secure area.
Conditional Statements ‘if-else’
The if block is used to specify the code to be executed if the condition specified in if is true, the else block is executed otherwise.
int age = 30;
if(age > 18) {
System.out.println("This is an adult");
} else {
System.out.println("This is not an adult");
}
Conditional Statements ‘switch’
Switch case statements are a substitute for long if statements that compare a
variable to multiple values. After a match is found, it executes the
corresponding code of that value case.
The following example is to print days of the week:
int n = 1;
switch(n) {
case 1 :
System.out.println("Monday");
break;
case 2 :
System.out.println("Tuesday");
break;
case 3 :
System.out.println("Wednesday");
break;
case 4 :
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6 :
System.out.println("Saturday");
break;
default :
System.out.println("Sunday");
}
Homework Problems
Make a Calculator. Take 2 numbers (a & b) from the user and an operation as follows :
1 : + (Addition) a + b
2 : - (Subtraction) a - b
3 : * (Multiplication) a * b
4 : / (Division) a / b
5 : % (Modulo or remainder) a % b
Calculate the result according to the operation given and display it to the user.
Ask the user to enter the number of the month & print the name of the month. For eg - For ‘1’ print ‘January’, ‘2’ print ‘February’ & so on.
KEEP LEARNING & KEEP PRACTICING :)
1. Java:
Java provides three types of conditional statements: if, if-else, and switch statements.
The "if" statement in Java is used to test a condition, and if the condition is true, it executes a block of code. If the condition is false, it skips the block of code.
The "if-else" statement is used when we want to execute one block of code if the condition is true, and another block of code if the condition is false.
The switch statement is used when we want to execute different blocks of code based on different possible values of a variable.
Here's an example of an if statement in Java:
Javaint x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
}
Javaif (condition) {
// code to execute if condition is true
}
else {
// code to execute if condition is false
}
Javaint x = 5;
if (x > 10)
{
System.out.println("x is greater than 10");
}
else {
System.out.println("x is less than or equal to 10");
}
Javapublic class Main {
public static void main(String[] args) {
int num = 10;
if (num > 5) {
System.out.println("The number is greater than 5.");
}
if (num > 15) {
System.out.println("The number is greater than 15.");
}
else {
System.out.println("The number is not greater than 15.");
}
}
}
Output:
csharpThe number is greater than 5.
The number is not greater than 15.
The "if" statement in C is similar to Java and is used to test a condition and execute a block of code if the condition is true. If the condition is false, it skips the block of code.
The switch statement in C is used when we want to execute different blocks of code based on different possible values of a variable.
Here's an example of an if statement in C:
C int x = 10;
if (x > 5) {
printf("x is greater than 5");
}
Cif (condition) {
// code to execute if condition is true
}
else {
// code to execute if condition is false
}
Cint x = 5;
if (x > 10) {
printf("x is greater than 10\n");
}
else {
printf("x is less than or equal to 10\n");
}
C#include <stdio.h>
int main() {
int num = 10;
if (num > 5) {
printf("The number is greater than 5.\n");
}
if (num > 15) {
printf("The number is greater than 15.\n");
}
else {
printf("The number is not greater than 15.\n");
}
return 0;
}
Output:
CThe number is greater than 5.
The number is not greater than 15.
The "if" statement in C++ is used to test a condition and execute a block of code if the condition is true. If the condition is false, it skips the block of code.
The switch statement in C++ is also used when we want to execute different blocks of code based on different possible values of a variable.
Here's an example of an if statement in C++:
C++int x = 10;
if (x > 5) {
cout << "x is greater than 5";
}
C++ if...else
The if
statement can have an optional else
clause. Its syntax is:
if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}
Example : C++ if...else Statement
// Program to check whether an integer is positive or negative
// This program considers 0 as a positive number
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed.";
return 0;
}Output 1
Enter an integer: 4 You entered a positive integer: 4. This line is always printed.
C++#include <iostream>
using namespace std;
int main() {
int num = 10;
if (num > 5) {
cout << "The number is greater than 5." << endl;
}
if (num > 15) {
cout << "The number is greater than 15." << endl;
}else {
cout << "The number is not greater than 15." << endl;
}
r
eturn 0;
}
Output:
C++The number is greater than 5.
The number is not greater than 15.
The "if" statement in Python is used to test a condition and execute a block of code if the condition is true. If the condition is false, it executes another block of code.
Here's an example of an if-else statement in Python:
Pythonx = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
Pythonif condition:
# code to execute if condition is true
else:
# code to execute if condition is false
Pythonx = 5
if x > 10:
print("x is greater than 10")
else:
print("x is less than or equal to 10")
Python ternary operator:
In addition to the if-else statement, Python also has a ternary operator, which is a shorthand way to write a simple if-else statement in a single line of code. The syntax is as follows:
Pythonvalue_if_true if condition else value_if_false
Pythonx = 5
print("x is greater than 10" if x > 10 else "x is less than or equal to 10")
.
Pythonnum = 10
if num > 5:
print("The number is greater than 5.")
if num > 15:
print("The number is greater than 15.")
else:
print("The number is not greater than 15.")
Output:
PyhonThe number is greater than 5.
The number is not greater than 15.
Conditional statements are an essential part of programming, allowing programs to make decisions based on certain conditions. By using if-then statements, if-else statements, nested if statements, and switch statements, programmers can create complex programs that respond to user input and perform a wide range of tasks.
Others blogs:-
- Insertion Sort | Insertion sort algorithm | insertion sort time complexity
- Inheritance | types of inheritance | inheritance in python
- Variables & Data Types (Lecture 2) | Primitive Data Types| Non-Primitive Data types
- Package in Java| what is the case used for a package in java| creating packages in java
- Functions in Java | Practice Questions | Complete Placement Course | Lecture 7
Comments
Post a Comment