if else statement

 

if else statement

if else statement

In this article you are going to learn about three Condition if else statement in C++

if statement

else statement

switch statement

 

So let start

often Program also need to make decision on certain condition , given in program. Decision can be made in C++ in numerous methods . if and else are most commonly use as decision making, which selects between two alternative .if statement can be used without else statement , depending on your condition used in program. Switch statement is another decision making statement , which creates branches for multiple alternative sections of code , depending on value of single variable .

 

if

if statement

if statement

so let start from if statement which is simple decision making statement.

Basic syntax :

if (condition )

Statement ;

 

A simple if statement program.

#include <iostream>

using namespace std;

int main()

{

int num;

cout<<”Enter any  number”;cin>>num;

if(num > 200)

cout<<”your entered number is greater than 200”;

return 0;

}

 

Explanation of code

The if keyword is followed by a test expression in parentheses (num > 200) . The syntax of

if statement is very much like that of while. The difference is that the if statement following the if are executed only once if the test expression  is true , the statement wile are executed repeatedly until test expression becomes false .

in program output ,If your entered num is greater than 200 , this message will be printed on screen

your entered number is greater than 200.

If number entered is not greater than 200 , program will terminate without printing second line

else

else statement in c++

else

If statement is lets you do something if given state is true . suppose if it isn’t true ? , then?, nothing will be happen , but assume we want to do on thing if a condition is true , and do something else if it’s false . that is where else statement comes . it  consists of an if statement , followed by a statement or block of statement and followed by keyword else .

Basic syntax:

if (condition )

Statement ;

else

statement;

 

 

Simple else program

 

#include <iostream>

using namespace std;

int main()

{

int num;

cout<<”Enter any  number”;cin>>num;

if(num > 200)

cout<<”your entered number is greater than 200”;

else

<<”Your entered number is not greater than 200”

return 0;

}

in above given program , if condition is true then if part will executed and if not true then else part will execute.

 

 

switch statement

switch statement

 

switch statement

If your program have large decisions and all decisions depend on value of similar variable,
in that kind of situation switch statement is better than hierarchy of if or else structure.

simple program of switch statement

#include <iostream>

using namespace std;

int main()

{

int speed;

cout<<”Enterer 40,50,60,70”;

cin>>speed;

switch(speed){

case 40:

cout<<”Your speed is 40”;

break;

case 50:

cout<<”Your speed is 50”;

break;

case 60:

cout<<”Your speed is 60”;

break;

case 70:

cout<<”Your speed is 70”;

break;

default :

cout<<”Your entered wrong speed”;

return 0;

}

 

This simple program will print one of four possible messages , depending on whether user inputs the number 40,50,60 and 70.

The keyword switch is followed by a switch variable in parentheses .

Switch(speed)

Braces then delimit a number of case statements. Each case keyword is followed by a constant, which is not in parentheses but is followed by a colon.

case 40:

The data type of case constants should match that of switch variable .

Before entering the switch , program should assign a value to switch  variable  . This value will typically match a constant in one of the case statements. When this is case, statement immediately following keyword case will be executed , until  control is reached at  break statement .

 

break statement

A break statement at end of each case section . The break keyword causes the entire switch statement to exit. Control goes to first statement following end of switch construction . Don’t forget the break keyword after every case.

 

default keyword

At last case at bottom of switch construction , you instead see keyword default . this keyword gives switch construction a way to take an action if value of loop variable doesn’t match any of case constant .

Summary :

In this article you have learned about if   else statement and switch statement .

if statement is used where you have one condition , else where you have alternative condition and switch statement where you have multiple same condition  .

please share your experience  after reading article & share it with your friends .

Leave a Reply

Your email address will not be published. Required fields are marked *