TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Wednesday 19 August 2020

Conditional Statement In C++ | Control Statement In C++ | Conditional Statement In C++ In Hindi

 

Conditional  Statement In C++

A conditional statement is used in programming language  to control the flow of the program. They are nothing but a keyword or statements that are used in a program to transfer the flow of control to another statement based on the conditions. Based on the given condition, it evaluates the result and executes the corresponding statements.

Conditional statements in programming languages decides the direction of flow of program execution. Decision making statements available in  C++ are:

      1 .    If statement

     2.    If else statement

    3.    If elseif statement 

    4.    Nested if statement  

    5.    Switch statement

 

image of conditional statement in c++

Fig: Conditional Statement In C++

1.   If statement

if statement is  a conditional statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Syntax:

if(condition)

{

statement;

}

Flow Chart:


image of if statement in c++

Fig: if statement in c++

Program:

#include<iostream.h>

#include<conio.h>

void main()

{

int a=10;

if(a>15)

{

cout<<”the number you enter is 10”;

}

cout<<”the number you enter is greater than”;

getch();

}

 

Output:

The number you enter is 10

 

2.   If  else statement

The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. We can use the else statement with if statement to execute a block of code when the condition is false.

Syntax:

If(condition)

{

//statement;

}

else

{

//statement;

}

 

Flow Chart:

image of if else statement in c++

Fig: if else statement in c++

Program:

#include<iostream.h>

#include<conio.h>

void main()

{

int a;

cout<<”enter a number”;

if(a%==2)

{

cout<<”the number enter is even”;

}

cout<<”the number enter is odd”;

getch();

}

 

Output:

Enter a number

22

The number is even

 

3.   If else if statement

The C++ if-else-if ladder declaration executes from multiple statements in one condition.

 

Syntax:

if(condition1)

{

//statement;

}

else if(condition1)

{

//statement;

}

else if(condition2)

{

//statement;

}

else if(condition3)

{

//statement;

}

else

{

//statement;

}


Flow Chart:

image of if else if statement in c++

Fig: if else if statement in c++

 

Program:

#include<iostream.h>

#include<conio.h>

void main()

{

int i=30;

if(i==10)

{

cout<<”i is 10”;

}

else if(i==20)

{

Cout<<”i is 20”;

}

else if(i==25)

{

cout<<”i is 25”;

}

else

{

cout<<”i is not presented”;

}

getch();

}

Output:

I is not presented

 

4.   Nested statement

A nested if in C++is an if statement that is the target of another if statement. Nested if statements means an if statement inside another if statement. 

Synatx:

if(condition)

{

if(condition)

{

//statement;

}

else

{

//statement;

}

Flow Chart:

image of nested statement in c++
Fig: nested statement in c++

Program:

#include<iostream.h>

#include<conio.h>

void main()

{

int a,b,c;

cout<<”enter three number”;

cin>>a>>b>>c;

if(a>b)

{

if(a>c)

{

cout<<a;

}

else

{

cout<<c;

}

}

else

{

if(b>c)

{

cout<<b;

}

else

{

cout<<c;

}

}

getch();

}

 

Output:

Enter three number

99

87

67

99

 

5.   Switch statement

Switch case statement is used when we have multiple conditions and we need to perform different action based on the condition. When we have multiple conditions and we need to execute a block of statements when a particular condition is satisfied. In such case either we can use lengthy if else if  statement or switch case.

 

Syntax:

Switch( expression)

{

case value :

//statement;

break;

case value :

//statement;

break;

         case value :

//statement;

break;

default:

//statement;

}


Flow Chart:

                            
image of switch statement in c++
Fig: switch statement in c++


Program:

#include<iostream.h>

#include<conio.h>

void main()

{

int a;

cout<<”enter any no(1 to 7)”

cin>>a;

switch(a)

{

case 1:

cout<<”Monday”;

break;

case 2:

cout<<”Tuesday”;

break;

case 3:

cout<<”Wednesday”;

break;

case 4:

cout<<”Thursday”;

break;

case 5:

cout<<”Friday”;

break;

case 6:

cout<<”Saturday”;

break;

case 7:

cout<<”Sunday”;

break;

default:

cout<<”wrong enter”;

}

getch();

}

 

Output:

Enter any no(1 to 7)

6

Saturday

No comments:

Post a Comment