• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Node.js Tutorial
  • C Tutorial
  • Privacy Policy
  • Contact Us
  • About Us
Tech Stack Journal

Tech Stack Journal

  • Java
  • Node.js
  • Docker
  • Algorithms
  • Spring Core

if else statement

Contents

  • 1 if condition
  • 2 else statement
  • 3 Multiple statements in if-else
  • 4 Nested if conditions

C language provides a decision control mechanism to execute your code based on a condition.

if condition

if(condition)
    code;

A condition is an expression usually written using 2 operands separated by 1 relational operator. The result of a condition will be either 1 or 0. If the expression is true the condition returns 1 else it returns 0.

An if condition starts with if keyword, followed by condition surrounded by round brackets.

An example,

#include <stdio.h>

void main()
{
    int marks = 80;
    if(marks >= 35)
        printf("Passed");
}

else statement

You can add an else statement to perform something else when the condition in if statement is not success.

if(condition)
  true-code;
else
  false-code;

For example,

#include <stdio.h>

void main()
{
    int marks = 80;
    if(marks>=35)
        printf("Passed");
    else
        printf("Failed");
}

Multiple statements in if-else

If you want to execute multiple statements conditionally, you can enclose all those statements within curly brackets.

if(condition)
{
    statement1;
    statement2;
}
else 
{
    statement1;
    statement2;
}

For example, in the below program we are printing multiple statements when the condition is either success or failure, using curly parenthesis.

#include <stdio.h>

void main()
{
    int marks = 80;
    if(marks>=35)
    {
        printf("Passed");
        printf("\n Marks: %d",marks);
    }

    else
    {
        printf("Failed");
        printf("\n Marks: %d",marks);
    }
}

Nested if conditions

An if condition within another if condition is called the nested if condition.

if(condition 1)
{
    if(condition 2)
    {
         statements;
    }
    statements;
}

Similarly, we can write a nested if condition inside else block too.

Previous
Next

Primary Sidebar

  • Facebook
  • Pinterest
  • YouTube

More to See

How to Show Last Updated Date in Posts with Genesis Framework

December 12, 2020 By Admin

Get Environment Variables and System Properties in Java

September 18, 2020 By Admin

Tags

access modifiers in java Access Specifiers in Java array in class java ArrayIndexOutOfBoundsException Arrays in Java Constructor Definition in Java Constructor in Java Constructor in Java with Example Definition of Constructor in Java example of a loop Example of Constructor in Java final method final variable First Hello World Java Program for loop in java with example for loop java example for loops in java examples for loops java example How to run Java Hello World from command prompt How to use Constructor in Java How to write Hello World Java program in Eclipse Inheritance in Java Installing Java Installing Java in Windows Introduction to Java Introduction to Java Programming Iterating over an array Java Comments Java Do While Loop Java For Loop Java Hello World Command Line Java Hello World Example Java Hello World in Eclipse Java Hello World Program Java Hello World Tutorial Java Inheritance Java Installation Java Intro Java Introduction Java Statements Java While Loop Static Methods in Java Static Variables in Java Types of Comments What is a Constructor in Java

Secondary Sidebar

  • C Tutorial
    • First C Program
    • Variables
    • Data Types
    • Arithmetic Operators
    • Relational Operators
    • if else statement

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Node.js
  • Spring Core

Archives

  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Node.js Tutorial
  • C Tutorial
  • Privacy Policy
  • Contact Us
  • About Us

My Other Sites

Spec Watchers

Speak New Language

Recent

  • [Solved] How to fix java.lang.InstantiationException
  • How to Show Last Updated Date in Posts with Genesis Framework
  • Get Environment Variables and System Properties in Java
  • Autowire using Java Based Spring Configuration
  • Java based Spring Configuration

Search

Tags

access modifiers in java Access Specifiers in Java array in class java ArrayIndexOutOfBoundsException Arrays in Java Constructor Definition in Java Constructor in Java Constructor in Java with Example Definition of Constructor in Java example of a loop Example of Constructor in Java final method final variable First Hello World Java Program for loop in java with example for loop java example for loops in java examples for loops java example How to run Java Hello World from command prompt How to use Constructor in Java How to write Hello World Java program in Eclipse Inheritance in Java Installing Java Installing Java in Windows Introduction to Java Introduction to Java Programming Iterating over an array Java Comments Java Do While Loop Java For Loop Java Hello World Command Line Java Hello World Example Java Hello World in Eclipse Java Hello World Program Java Hello World Tutorial Java Inheritance Java Installation Java Intro Java Introduction Java Statements Java While Loop Static Methods in Java Static Variables in Java Types of Comments What is a Constructor in Java

Copyright © 2021 · Tech Stack Journal · Log in