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 statement

if statement

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

1

2

3

4

5

Basic syntax:

if(condition)

Statement;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Asimple ifstatement program.

#include <iostream>

using namespacestd;

intmain()

{

intnum;

cout<<”Enter anynumber”;cin>>num;

if(num>200)

cout<<”your entered number isgreater than200”;

return0;

}

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 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 .

1

2

3

4

5

6

7

8

9

Basic syntax:

if(condition)

Statement;

else

statement;

Simple else program

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#include <iostream>

using namespacestd;

intmain()

{

intnum;

cout<<”Enter anynumber”;cin>>num;

if(num>200)

cout<<”your entered number isgreater than200”;

else

<<”Your entered number isnotgreater than200”

return0;

}

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

#include <iostream>

using namespacestd;

intmain()

{

intspeed;

cout<<”Enterer40,50,60,70”;

cin>>speed;

switch(speed){

case40:

cout<<”Your speed is40”;

break;

case50:

cout<<”Your speed is50”;

break;

case60:

cout<<”Your speed is60”;

break;

case70:

cout<<”Your speed is70”;

break;

default:

cout<<”Your entered wrong speed”;

return0;

}

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 .