Saturday, June 17, 2023

C++ Control Statement (or) Programming Construct

 

C++  CONTROL STATEMENT (or) Programming Construct

 

1.Decision Making statements

o    if statements

o    if else statement

o    switch statement

    

o    do while loop

o    while loop

o    for loop

    

o    break statement

o    continue statement

If statement

syntax

if(condition)

{

statement;

}

if(condition)

{

statement;

}

 

Program

#include<iostream.h>

#include<conio.h>

void main()

{

int  age;

clrscr();

cout<<"enter your age"

cin>>age;

if(age>=18)

{

cout<<"eligible for vote";

}

if(age<18)

{

cout<<"Not eligible for vote";

}

getch();

}

 

output

enter your age 16

Not eligible for vote

enter your age 18

eligible for vote

If else statement

syntax

if(condition)

{

statement;

}

else

{

statement;

}

 

Program

#include<iostream.h>

#include<conio.h>

void main()

{

int  age;

clrscr();

cout<<"enter your age"

cin>>age;

if(age>=18)

{

cout<<"eligible for vote";

}

else

{

cout<<"Not eligible for vote";

}

getch();

}

 

output

enter your age 16

Not eligible for vote

enter your age 18

eligible for vote

 

Switch case

switch (condition)  {

    case constant1:

        // statement;

        break;

 

    case constant2:

        // statement;

        break;

        .

        .

        .

    default:

        // code to be executed if

        // failure will be executed in default case

}

// Program to build a simple calculator using switch Statement

#include <iostream.h>

#include<conio.h>

void main() {

    char oper;

    float a, b;

    cout << "Enter an operator (+, -, *, /): ";

    cin >> oper;

    cout << "Enter two numbers: " << endl;

    cin >> a >> b;

 switch (oper) {

 case '+':

 cout << a << " + " << b << " = " << a + b;

break;

 case '-':

cout << a << " - " << b << " = " << a - b;

break;

case '*':

cout << a << " * " << b << " = " << a * b;

break;

 case '/':

cout << a << " / " << b << " = " << a / b;

break;

default:

cout << "Error! The operator is not correct";

 break;

    }

 

getch();

}

 

Output

Enter an operator (+, -, *, /): +
Enter two numbers: 
2.3
4.5
2.3 + 4.5 = 6.8
 
For Loop
 
syntax
for(variableinitialization;conditionchecking; increment/decrement)
 {
 //loop statements;
 }
 
#include <iostream.h>
#include<conio.h>
 void main()
 {
 int a;
 for (a=1; a<=10; a++)
 {
 cout << "The value of a is: " << a << '\n';
 }
getch();
 }
output

 The value of a is:1

The value of a is:2

The value of a is:3

The value of a is:4

The value of a is:5

The value of a is:6

The value of a is:7

The value of a is:8

 The value of a is:9

The value of a is:10

 

 

 

While Loop

Syntax
while (condition)
 {
 //loop statements;
 }
 
 
#include <iostream.h>
#include<conio.h>
 void main()
 {
 int a=1;
 while (a<=10)
 {
 cout << "The value of a is: " << a << '\n';
 a++; 
 }
 } 
output

The value of a is:1

The value of a is:2

The value of a is:3

The value of a is:4

The value of a is:5

The value of a is:6

The value of a is:7

The value of a is:8

 The value of a is:9

The value of a is:10

 

Do - while

#include <iostream.h>  

#include<conio.h>

void main() {  

     int i = 1;    

          do{    

              cout<<i<<"\n";    

              i++;    

          } while (i <= 10) ;    

getch();

}  

Output:

1

2

3

4

5

6

7

8

9

10

C++ Break

#include <iostream.h>  

#include <conio.h>  

void main() {  

      for (int i = 1; i <= 10; i++)    

          {    

              if (i == 5)    

              {    

                  break;    

              }    

        cout<<i<<"\n";    

          }    

getch();

}  

 

Output:

1

2

3

4

C++ continue

#include <iostream.h>  

#include <conio.h>  

void main() {  

      for (int i = 1; i <= 10; i++)    

          {    

              if (i == 5)    

              {    

                  continue;    

              }    

        cout<<i<<"\n";    

          }    

getch();

}  

 

 

Output:

1

2

3

4

6

7

8

9

10

 

 

 

 

 

0 comments:

Post a Comment