Skip to main content

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.


What are conditional statements?

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.

  1. 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");

}


  1. 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

  1. 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.

  1. 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 :)

When it comes to programming, conditional statements play a vital role in controlling the flow of code execution based on certain conditions. In this article, we will discuss the conditional statements in programming languages such as Java, C, C++, and Python, and how they can be used effectively.

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:

Java
int x = 10
if (x > 5) { 
 System.out.println("x is greater than 5"); 
}

In Java, the if-else conditional statement has the following syntax:

Java
if (condition) {
// code to execute if condition is true 
else {
// code to execute if condition is false
}

For Example:-

Java
int 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");
 }

"if" and "if else" conditional statements in Java

Java
public 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:

csharp
The number is greater than 5. 
The number is not greater than 15.

2. C:

C provides two types of conditional statements: if and switch statements.

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"); 
}

    In C, the if-else conditional statement has the following syntax:

    C
    if (condition) {
    // code to execute if condition is true
    }
    else {
    // code to execute if condition is false
    }

    For Example:-

    C
    int x = 5
    if (x > 10) {
    printf("x is greater than 10\n");
     } 
    else {
    printf("x is less than or equal to 10\n"); 
    }

      "if" and "if else" conditional statements in C.

    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:

    C
    The number is greater than 5.
    The number is not greater than 15.

    3. C++:

    C++ provides the same types of conditional statements as C: if and switch statements.

    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.
    "if" and "if else" conditional statements in C++.

    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.


    4.Python:

    Python provides only one type of conditional statement: if-else statement.

    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:
    Python
    x = 10 
    if x > 5: 
    print("x is greater than 5"
    else
    print("x is less than or equal to 5")

    In Python, the if-else conditional statement has the following syntax:

    Python
    if condition
     # code to execute if condition is true else
     # code to execute if condition is false

    For Example:-
    Python
    x = 5 
    if x > 10: 
    print("x is greater than 10"
    else:
    print("x is less than or equal to 10")

    Note that Python uses indentation to denote code blocks, so it's important to make sure your indentation is consistent.

    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:

    Python
    value_if_true if condition else value_if_false

    For example:
    Python
    x = 5 print("x is greater than 10" if x > 10 else "x is less than or equal to 10").

    "if" and "if else" conditional statements in Python.

    Python
    num = 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:

    Pyhon
    The number is greater than 5. 
    The number is not greater than 15.


    Conclusion:


    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:-

    My Products:-





    Comments

    Popular posts from this blog

    Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.

    Enter 3 numbers from the user & make a function to print their average. To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average: JAVA import java . util .*; public class Solutions {     public static void main ( String args []) {        Scanner sc = new Scanner ( System . in );        int a = sc . nextInt ();        int b = sc . nextInt ();        int c = sc . nextInt ();          int average = ( a + b + c ) / 3 ;        System . out . println ( average );    }    }   C++: // c++ #include <iostream> using namespace std ;...

    2D Arrays | Java Complete Placement Course | Lecture 11

    Java - Introduction to Programming Lecture 11 2D Arrays In Java It is similar to 2D matrices that we studied in 11th and 12th class. Creating a 2D Array - with new keyword int [][] marks = new int [ 3 ][ 3 ] ; Taking a matrix as an input and printing its elements. import java . util .*;   public class TwoDArrays {     public static void main ( String args []) {         Scanner sc = new Scanner ( System . in );         int rows = sc . nextInt ();         int cols = sc . nextInt ();           int [][] numbers = new int [ rows ][ cols ];           //input         //rows         for ( int i = 0 ; i < rows ; i ++) {             //columns  ...

    Merge Sort | How do I learn merge sort?

           Merge Sort | For Beginners | Java Placement Course >> Merge sort is a popular sorting algorithm that utilizes a divide-and-conquer approach to sort elements in an array. The basic idea behind merge sort is to divide an unsorted array into two halves, recursively sort each half, and then merge the two sorted halves into a single sorted array. >>  Merge sort algorithm works by recursively dividing an array into two halves, sorting each half, and then merging them to obtain the sorted array. The basic steps of the merge sort algorithm are as follows: Divide the unsorted array into two halves, mid and left. Sort the left half recursively using merge sort algorithm. Sort the right half recursively using merge sort algorithm. Merge the two sorted halves to obtain the final sorted array. JAVA Implementation: // Merge sort public class MergeSort {     //time complexity=nlogn     public static void conquer ( int arr [], int ...