Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views6 pages

List of Programs - Control Structures

The document provides examples of C++ selection statements including if, if-else, and switch statements, along with their syntax and functionality. It also demonstrates loops in C++ with examples of for, while, and do-while loops, showing how to increment and print a number multiple times. Each code snippet includes user input and output to illustrate the concepts.

Uploaded by

Jitendra Dangra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

List of Programs - Control Structures

The document provides examples of C++ selection statements including if, if-else, and switch statements, along with their syntax and functionality. It also demonstrates loops in C++ with examples of for, while, and do-while loops, showing how to increment and print a number multiple times. Each code snippet includes user input and output to illustrate the concepts.

Uploaded by

Jitendra Dangra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C++ if Statement

/* C++ Selection Statements - C++ if Statement */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;
if(num%2==0)
{
cout<<"You entered an even number";
}
getch();
}

C++ if-else Statement

/* C++ Selection Statements - C++ if-else Statement */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;
if(num%2==0)
{
cout<<"You entered an even number";
}
else
{
cout<<"You entered an odd number";
}
getch();
}

C++ switch Statement


/* C++ Selection Statement - C++ switch Statement */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int dow;
cout<<"Enter number of week's day (1-7): ";
cin>>dow;
switch(dow)
{
case 1 : cout<<"\nSunday";
break;
case 2 : cout<<"\nMonday";
break;
case 3 : cout<<"\nTuesday";
break;
case 4 : cout<<"\nWednesday";
break;
case 5 : cout<<"\nThursday";
break;
case 6 : cout<<"\nFriday";
break;
case 7 : cout<<"\nSaturday";
break;
default : cout<<"\nWrong number of day";
break;
}
getch();
}

C++ Loops
/* C++ Loops Example */

Let's start with for loop program


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, l;
cout<<"Enter a number: ";
cin>>num;
cout<<"\nIncrementing & Printing the number, 10 times:\n";
for(l=0; l<10; l++)
{
num++;
cout<<num<<"\n";
}
getch();
}

Program demonstrating while loop in C++

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, l=0;
cout<<"Enter a number: ";
cin>>num;
cout<<"\nIncrementing & Printing the number, 10 times:\n";
while(l<10)
{
num++;
cout<<num<<"\n";
l++;
}
getch();
}

Program, demonstrating do-while loop in C++

/* C++ Loops Example */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, l=0;
cout<<"Enter a number: ";
cin>>num;
cout<<"\nIncrementing & Printing the number, 10 times:\n";
do
{
num++;
cout<<num<<"\n";
l++;
}while(l<10);
getch();
}

You might also like