• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

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

Aniruddha Chaudhari/11883/0
C / C++CodeJAVAPython

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:

  • C/C++ Programming with if-else Statement
  • Python Programming with if-else Statement
  • Java Programming with if-else Statement

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!

cppJavaPython
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard