Skip to main content

Python type() Function

When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...

JavaScript Conditional Statements – Making Decisions in Programs

In the previous article, we learned about operators and expressions. Operators help us compare values and create conditions. However, a program also needs the ability to make decisions based on those conditions.

For example:

  • If marks are greater than 40, display "Pass".

  • If age is 18 or above, allow voting.

  • If username and password are correct, allow login.

These decisions are made using Conditional Statements.

In this article, we will learn:

  • What conditional statements are

  • if statement

  • if-else statement

  • else-if ladder

  • Nested if

  • switch statement

  • Ternary operator

  • Real-world examples

  • Dry runs


What are Conditional Statements?

Conditional statements allow programs to execute different blocks of code depending on whether a condition is true or false.

Basic idea:

Condition
    ↓
True → Execute Code A

False → Execute Code B

Why Conditional Statements are Needed

Without conditions:

document.write("You Passed");

Every student would pass.

Using conditions:

if(marks >= 40)
{
    document.write("You Passed");
}

Only eligible students pass.


Types of Conditional Statements

JavaScript provides:

StatementPurpose
ifOne condition
if-elseTwo alternatives
else-if ladderMultiple conditions
nested ifConditions inside conditions
switchMultiple choices
ternary operatorShort if-else

The if Statement

The if statement executes code only when a condition is true.


Syntax

if(condition)
{
    statements;
}

Example

let age = 20;

if(age >= 18)
{
    document.write("Eligible to Vote");
}

Dry Run

age = 20

Condition:

20 >= 18

true

Execute block

Output:

Eligible to Vote

Example 2

let marks = 80;

if(marks > 40)
{
    document.write("Pass");
}

Output:

Pass

False Condition Example

let marks = 30;

if(marks > 40)
{
    document.write("Pass");
}

Output:

Nothing displayed

Because condition is false.


The if-else Statement

The if-else statement provides two alternatives.


Syntax

if(condition)
{
    statements;
}
else
{
    statements;
}

Example

let age = 16;

if(age >= 18)
{
    document.write("Eligible to Vote");
}
else
{
    document.write("Not Eligible");
}

Dry Run

age = 16

16 >= 18

false

else executes

Output:

Not Eligible

Pass-Fail Example

let marks = 75;

if(marks >= 40)
{
    document.write("Pass");
}
else
{
    document.write("Fail");
}

Output:

Pass

Even-Odd Program

let num = 10;

if(num % 2 == 0)
{
    document.write("Even Number");
}
else
{
    document.write("Odd Number");
}

Dry Run

10 % 2

Remainder = 0

Condition true

Output:

Even Number

else-if Ladder

Used when multiple conditions must be checked.


Syntax

if(condition1)
{
}
else if(condition2)
{
}
else if(condition3)
{
}
else
{
}

Grade Calculation Example

let marks = 85;

if(marks >= 90)
{
    document.write("Grade A+");
}
else if(marks >= 80)
{
    document.write("Grade A");
}
else if(marks >= 70)
{
    document.write("Grade B");
}
else
{
    document.write("Grade C");
}

Dry Run

marks = 85

85 >= 90 → false

85 >= 80 → true

Grade A displayed

Output:

Grade A

Student Result Example

let marks = 65;

if(marks >= 80)
{
    document.write("Distinction");
}
else if(marks >= 60)
{
    document.write("First Division");
}
else if(marks >= 40)
{
    document.write("Pass");
}
else
{
    document.write("Fail");
}

Nested if Statement

An if statement inside another if statement.


Syntax

if(condition1)
{
    if(condition2)
    {
    }
}

Example

let age = 20;

let citizenship = true;

if(age >= 18)
{
    if(citizenship == true)
    {
        document.write("Eligible to Vote");
    }
}

Dry Run

age >= 18

true

citizenship == true

true

Eligible

Output:

Eligible to Vote

Login System Example

let username = "admin";

let password = "1234";

if(username == "admin")
{
    if(password == "1234")
    {
        document.write("Login Successful");
    }
}

Switch Statement

Used when many choices exist.

Better than long else-if ladders in certain situations.


Syntax

switch(expression)
{
    case value1:
        statements;
        break;

    case value2:
        statements;
        break;

    default:
        statements;
}

Day Example

let day = 3;

switch(day)
{
    case 1:
        document.write("Monday");
        break;

    case 2:
        document.write("Tuesday");
        break;

    case 3:
        document.write("Wednesday");
        break;

    default:
        document.write("Invalid Day");
}

Output

Wednesday

Month Example

let month = 2;

switch(month)
{
    case 1:
        document.write("January");
        break;

    case 2:
        document.write("February");
        break;

    case 3:
        document.write("March");
        break;

    default:
        document.write("Invalid");
}

Output:

February

Importance of break

Without break, execution continues.

Wrong:

case 1:
    document.write("One");

case 2:
    document.write("Two");

Output:

OneTwo

Default Case

Executed when no case matches.

Example:

default:
    document.write("Invalid Choice");

Ternary Operator

A short form of if-else.


Syntax

condition
?
statement1
:
statement2;

Example

let age = 20;

let result =
(age >= 18)
?
"Eligible"
:
"Not Eligible";

document.write(result);

Output

Eligible

Dry Run

20 >= 18

true

First value selected

Largest Number Program

let a = 20;

let b = 10;

if(a > b)
{
    document.write("A is Larger");
}
else
{
    document.write("B is Larger");
}

Output:

A is Larger

Positive-Negative-Zero Program

let num = -5;

if(num > 0)
{
    document.write("Positive");
}
else if(num < 0)
{
    document.write("Negative");
}
else
{
    document.write("Zero");
}

Output:

Negative

Complete Practical Program

<!DOCTYPE html>

<html>

<body>

<script>

let marks = 72;

if(marks >= 80)
{
    document.write("Distinction");
}
else if(marks >= 60)
{
    document.write("First Division");
}
else if(marks >= 40)
{
    document.write("Pass");
}
else
{
    document.write("Fail");
}

</script>

</body>

</html>

Output

First Division

Flowchart of if-else

        Condition
             │
      ┌──────┴──────┐
      │             │
    True         False
      │             │
 Execute A     Execute B

Real-Life Applications

Conditional statements are used in:

  • ATM systems

  • Online shopping

  • Banking applications

  • Login systems

  • Student result processing

  • Ticket booking systems

Companies like Amazon, Google, and Microsoft use billions of conditional checks every day.


Common Mistakes Beginners Make

Using = Instead of ==

Wrong:

if(age = 18)

Correct:

if(age == 18)

Forgetting Curly Braces

Wrong:

if(age >= 18)

document.write("Eligible");

Better:

if(age >= 18)
{
    document.write("Eligible");
}

Missing break in switch

Can cause unexpected results.


Advantages of Conditional Statements

  • Enable decision making

  • Create intelligent programs

  • Improve flexibility

  • Handle different situations

  • Essential for application logic


Conclusion

Conditional statements allow JavaScript programs to think and make decisions. They form the basis of almost every real-world application.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? ๐Ÿ“Œ Readability – Comments make code easier to understand for you and others. ๐Ÿ“Œ Maintenance – When you revisit your code after months, comments remind you what you were doing. ๐Ÿ“Œ Collaboration – In team projects, comments explain code to fellow developers. ๐Ÿ“Œ Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . ๐Ÿ’ป Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } ๐Ÿง  Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

๐Ÿ’ก Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. ๐Ÿง‘‍๐Ÿ’ป Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...