[Explained in Detail] if else nested Programming Example in C, Java and Python

[Explained in Detail] if else nested Programming Example in C, Java and Python

In every programming language, almost each and every programming code we use an if-else statement. Whether if its Python, C/C++, Java or any advanced programming code, this is the most useful concept you will see.

In this tutorial, you will learn, detail about if else statements and if else nested programming example in C, Java and Python.

Not just programming language, we use the if-else statement in our daily lives.

How?

For examples:

  • If it is not Sunday, I go to school. Else, I go to play Cricket.
  • If it is 10 pm, I go to bed. Else if sunrises, I wake up.

There are so many scenarios. Basically, we keep our-self disciplined with if else statement.

Understanding the if-else statement in Programming

Now it’s programming where we are gonna use them.

We can also discipline our code to run certain lines of code based on the condition. If the condition is true, run these lines of code. Or, if the condition is false run those lines of code, blah blah…

In simple words, based on the condition, our program will decide which code snippet to execute.

I  decided to write a single if-else article for all the mentioned Programming languages. As…

Semantically, this holds true for all the programming languages. There is SLIGHTLY difference in code syntax for each programming language.

To explain,  we will check out some basic programming languages.

If you are into multiple programming languages, it will be easy for you to understand and compare if-else statement in each language you learn.

The if-else statement is also called as conditional branching in programming terminology.

Generic problem statement for the if-else condition:  Take a variable (says nNum). Save the user input value in the variable. Based on the value, we will use the if-else statement.

You can jump to any of your interested programmings to understand if-else example. If you are new to the programming, I would recommend going with a complete tutorial.

The if-else example in different programming:

So let’s deep dive…

if-else Statement in C/C++

Prerequisite: Compiling and running C program.

#include<stdio.h>
void main()
{
    int nNum == 0;
    scanf("Enter the value: %d", &nNum);
    if(nNum = 0)
    {
        printf("Number is Zero.");
    }
}

Output:

In the above code, we have just directed to execute code if the condition is true.

If the user input is 0, it will print.

Number is Zero.

What if the condition is false?

Here comes the if-else statement.

#include<stdio.h>
void main()
{
    int nNum = 0;
    scanf("Enter the value: %d", &nNum);
    if(nNum == 0)
    {
        printf("Number is Zero.");
    }
    else
    {
        printf("Number is non-zero.");
    }
}

Output:

If the above condition in the if statement is false (if the nNum value is not zero.), it executes lines of code in else block as,

Number is non-zero.

In the above program, we are identifying if the number is zero or not.

If it is not zero, how to find if the number is bigger (positive number) or smaller (negative number) than zero?

To address this, you can use nested if else statement.

Not sure?

Check the code given below.

#include<stdio.h>
void main()
{
    int nNum = 0;
    scanf("Enter the value: %d", &nData);
    if(nNum == 0)
    {
      printf("Number is Zero.");
    }
    else if(nNum > 0)
    {
      printf("Number is positive and greater than zero.");
    }
    else
    {
      printf("Number is negative and smaller than zero.");
    }
}

It is also called as a nested if-else statement in C programming.

Output:

If your input is a positive number, if-condition (nNum == 0) will be false. Condition (nNum > 0) will be true and it prints the output as,

Number is positive and greater than zero.

If your input is a negative number, the first two conditions will be false and it will execute the line of code in the else statement as,

Number is negative and smaller than zero.

Moving to Python.

if-else Statement in Python

If you compare with other programming languages, the line of code in Python is less and it is more readable. And this what the Python is known for.

Prerequisite: If you are new to the Python programming- Executing Your First Python program

We will keep the same problem statement.

Problem statement: Write a Python code for taking user input from the keyboard and print if the given value is equal to zero or smaller/greater than zero.

nNum = input("Enter the value: ")
if nNum == 0:
    print("Number is Zero.")
elif nNum > 0:
    print("Number is positive and greater than zero.")
else:
    print("Number is negative and smaller than zero.")

We are using input function from Python to take the user input from the keyboard.

This is an example of if-else nested programming in Python.

Simple. Right?

if-else Statement in Java

Again with the same problem statement.

Problem statement: Write a Java Program to print if the given value is equal to zero or smaller/greater than zero.

public class ifElseExample { 
  public static void main(String[] args) { 
    int nNum = 20; 
    if(nNum == 0){ 
      System.out.print("Number is zero."); 
    }
    else if(nNum > 0) {
      System.out.print("Number is positive and greater than zero.");
    }
    else {
      System.out.print("Number is negative and smaller than zero.");
    }
  } 
}

The syntax in Java is almost the same as C programming.

Output:

Number is positive and greater than zero.

We can also write the same program by taking a number as a user input.

Interested in learning more about Java Programming? Check out complete Java tutorial.

Final Word…

Trying if-else statement in a various programming language. You can see, semantic and use of the if-else statement in each of the programming language is the same. They are only different from the code syntax. If you are familiar with coding syntax, it is just catwalk.

Now,

Problem statement for Practicing if-else statement [Exercise]

  • Take the user input in the range of (0 to 6) and based on the user input print the day of the week.
  • You can also practice if-else statement by solving Interview Coding questions.

All the programming languages have the same meaning (semantic) for if-else statements. They are only different by their coding syntax.

Any doubt? Any question related to if else nested Programming Example in C, Java and Python? Let’s share your thought in the comment section.

Happy Programming!

Leave a Reply

Your email address will not be published. Required fields are marked *