Skip to main content

Conditional structure: if and else

                                                 Conditional structure: if and else

  • The if statement executes based test expression inside the braces.
  • If statement expression is to true, If body statements are executed and Else body statements are skipped.
  • If statement expression is to false If body statements are skipped and Else body statements are executed.
  • Simply, Block will execute based on If the condition is true or not.
  • IF conditional statement is a feature of this programming language which performs different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Apart from the case of branch prediction, this is always achieved by selectively altering the control flow based on some condition.

if and else Syntax

if (expression) // Body will execute if expression is true or non-zero
{
	//If Body statements
}else
{
	//Else Body statements
}

if and else Syntax Example

for example, In c
	if (i == 3) {
		doSomething();
	}
	else	{
		doSomethingElse();
	}

Syntax Explanation

Consider above example syntax,if (i == 3) 
  • which means the variable i contains a number that is equal to 3, the statements following the doSomething() block will be executed.
  • Otherwise variable contains a number that is not equal to 3, else block doSomethingElse() will be executed.

Example Program For If..else

/*  Example Program For If..else In C++ Programming Language */

// Header Files
#include<iostream> 
using namespace std;

//Main Function
int main()
{
	// Variable Declaration
	int a;

	//Get Input Value
	cout<<"Enter the Number :";
	cin>>a;

	//If Condition Check
	if(a > 10)
	{
	   // Block For Condition Success
	   cout<<a <<" Is Greater than 10";
	}
	else
	{
	   // Block For Condition Fail
	   cout<<a<<" Is Less than/Equal to 10";
	}
	//Main Function return Statement
	return 0;
}

Sample Output:

Enter the Number :8
8 Is Less than/Equal to 10

Enter the Number :10
10 Is Less than/Equal to 

Comments

Popular posts from this blog

Java Basic Principles

This Blog is Specially Designed for those who can learn the theory concepts of Java (Object Oriented Language)                                                             Object Oriented programming is a programming style which is associated with the concepts like class, object, Inheritance, Encapsulation, Abstraction, Polymorphism. Most popular programming languages like Java, C++, C#, Ruby, etc. follow an object oriented programming paradigm.  As JAVA  being  the most sought-after skill, we will talk about object-oriented programming concepts in Java.   An object-based application in Java is based on declaring classes, creating objects from them and interacting between these objects.  In this blog,  we will understand the below  core concepts of  Object oriented Programming in the ...

Introduction To C++

       Lets learn The C++ Programming Language C++ ranks 4th in popularity according to 2016 IEEE spectrum Top Programming Language ranking. Learning C++ is a wise investment for all programmers.                    What is C++? “C++ is a statically-typed, free-form, (usually) compiled, multi-paradigm, intermediate-level general-purpose middle-level programming language.” In simple terms, C++ is a sophisticated, efficient and a general-purpose programming language based on C. It was developed by Bjarne Srroutrup in 1979. Many of today’s operating systems, system drivers, browsers and games use C++ as their core language. This makes C++ one of the most popular languages today. Since it is an enhanced/extended version of C programming language, C and C++ are often denoted together as C/C++.                             ...

Difference Between Static and non-Static Variable in Java

Difference Between Static and non-Static Variable in Java The variable of any class are classified into two types; Static or class variable Non-static or instance variable Static variable in Java Memory for static variable is created only one in the program at the time of loading of class. These variables are preceded by static keyword. tatic variable can access with class reference. Non-static variable in Java Memory for non-static variable is created at the time of create an object of class. These variable should not be preceded by any static keyword Example: These variables can access with object reference. Difference between non-static and static variable Non-static variable Static variable 1 These variable should not be preceded by any static keyword Example: class A { int a ; } These variables are preceded by static keyword. Example class A { static int b ; } 2 Memory is allocated for these variable whenever an object is created Memory is a...