0% found this document useful (0 votes)
5 views10 pages

Unit 2 Control Structures

The document provides an overview of control structures in programming, including if-else statements, switch statements, while loops, do-while loops, and for loops. Each section includes syntax and example code demonstrating how to implement these structures in C++. Additionally, it covers break and continue statements for controlling loop execution.

Uploaded by

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

Unit 2 Control Structures

The document provides an overview of control structures in programming, including if-else statements, switch statements, while loops, do-while loops, and for loops. Each section includes syntax and example code demonstrating how to implement these structures in C++. Additionally, it covers break and continue statements for controlling loop execution.

Uploaded by

Mihir Kulkarni
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CONTROL STRUCTURE

1. If-else statements

● If statement:
Syntax:
if(condition)

//code to be executed

Program Code :

Program on even odd:


#include <iostream>

using namespace std;

int main () {

int num = 10;

if (num % 2 == 0)

cout<<"It is even number" ;

return 0;

● If -else statement

Syntax:
if(condition)
{

//code if condition is true

}else{

//code if condition is false

Program code:

Program on voting:
#include <iostream>

using namespace std;

int main ()

int age;

cout<<"Enter your age : ";

cin>>age;

if (age>18)

cout<<"You can vote!"<<endl;

else

cout<<"You cannot Vote"<<endl;

return 0;}

● If else if ladder statement


Syntax:
if(condition1){

//code to be executed if condition1 is true

}else if(condition2){

//code to be executed if condition2 is true

else if(condition3){

//code to be executed if condition3 is true

...

else{

//code to be executed if all the conditions are false

Program code:
#include <iostream>

using namespace std;

int main () {

int num;

cout<<"Enter a number to check grade:";

cin>>num;

if (num <0 || num >100)

cout<<"wrong number";

else if(num >= 0 && num < 50){


cout<<"Fail";

else if (num >= 50 && num < 60)

cout<<"D Grade";

else if (num >= 60 && num < 70)

cout<<"C Grade";

else if (num >= 70 && num < 80)

cout<<"B Grade";

else if (num >= 80 && num < 90)

cout<<"A Grade";

else if (num >= 90 && num <= 100)

cout<<"A+ Grade";

return 0; }

[Link] statement:

Syntax:
switch(expression){

case value1:

//code to be executed;

break;

case value2:

//code to be executed;

break;

......

default:

//code to be executed if all cases are not matched;

break;

Program code:
#include <iostream>

using namespace std;

int main () {

int num;

cout<<"Enter a number to check grade:";

cin>>num;

switch (num)

case 10: cout<<"It is 10"; break;

case 20: cout<<"It is 20"; break;


case 30: cout<<"It is 30"; break;

default: cout<<"entered no. is wrong"; break;

3. While statement:

Syntax:
while(condition){

//code to be executed

Program code:

Program to get 1 to 10 number display.


#include <iostream>

using namespace std;

int main() {

int i=1;

while(i<=10)

cout<<i <<"\n";

i++;

} }

4. Do while statement

Syntax:
do{
//code to be executed

while(condition);

Program code:
#include <iostream>

using namespace std;

int main() {

int i = 1;

do{

cout<<i<<"\n";

i++;

} while (i <= 10) ;

5. for statement:

Syntax:
for(initialization; condition; incr/decr){

//code to be executed

Program code :

#include <iostream>

using namespace std;


int main()

int n;

cout<<"enter any positive integer";

cin>>n;

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

cout<<n<< "*"<<i <<"="<<n*i <<"\n";

6. Break statement:
Syntax:
jump-statement;

break;

Program code:

#include <iostream>

using namespace std;


int main() {

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

if (i == 5)

break;

cout<<i<<"\n";

[Link] statement:
Syntax:

jump-statement;

continue;

Program code:
#include <iostream>

using namespace std;

int main()

for(int i=1;i<=10;i++){
if(i==5){

continue;

cout<<i<<"\n";

You might also like