0% found this document useful (0 votes)
2 views9 pages

c++Example Part4

The document provides an overview of control flow statements in C++, including switch statements, while loops, do/while loops, for loops, and nested loops. It explains the syntax and usage of each loop type, along with examples demonstrating their functionality. Additionally, it covers the use of break and continue statements within loops to control execution flow.
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)
2 views9 pages

c++Example Part4

The document provides an overview of control flow statements in C++, including switch statements, while loops, do/while loops, for loops, and nested loops. It explains the syntax and usage of each loop type, along with examples demonstrating their functionality. Additionally, it covers the use of break and continue statements within loops to control execution flow.
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

C++ example: part4

Switch Statements
Use the switch statement to select one of many code blocks to be
executed.

 The switch expression is evaluated once.


 The value of the expression is compared with the values of
each case
 If there is a match, the associated block of code is executed.
 The break and default keywords are optional, and will be
described later in this chapter.

Syntax:

switch(expression){
case x:
//code block
break;
case y:
//code block
break;
default:
// code block
}

101 int main() {


int day = 4;
switch (day){
int day = 4;
switch (day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
case 3: cout << "Wednesday"; break;
case 4: cout << "Thursday"; break;
case 5: cout << "Friday"; break;
case 6: cout << "Saturday"; break;
case 7: cout << "Sunday"; break;
}
return 0;
} // Thursday

The default Keyword.


102 int main() {
int day = 3;
switch (day) {
case 6:
cout << "Today is Saturday";
break;
case 7:
cout << "Today is Sunday";
break;
default:
cout << "Looking forward to the Weekend";
}
return 0;
} // Outputs "Looking forward to the Weekend"

While Loop
The while loop loops through a block of code as long as a specified
condition is true:

Syntax:
while (condition) {
// code block to be executed
}

103 int main() {


int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
return 0;
}

104 Write a sample C+ program to print numbers from 1 to 100.


int main()
{
int number = 1;

while(number <= 100)


{
cout << number << " ";
number = number + 1;
}

return 0;
}
104 int main() {
int i = 0;
for (;i < 5;) {
cout << i << "\n";
i++;
}
return 0;
}

Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block
once, before checking if the condition is true, then it will repeat the loop as long as the
condition is true.

Syntax:
do {
// code block to be executed
}
while (condition);

105 write a program that displays numbers 0 to 4.


int main() {
int i = 0;
do {
cout << i << " ";
i++;
}
while (i < 5);
return 0;
}
Output: 0 1 2 3 4

106 Write a program to find the sum of positive numbers using do..while.

int main() {
int number = 0, sum = 0;

do {
sum += number;

// take input from the user


cout << "Enter a number: ";
cin >> number;
}
while (number >= 0);

// display the sum


cout << "\nThe sum is " << sum << endl;

return 0;
}

For Loop
When you know exactly how many times you want to loop through a
block of code, use the for loop instead of a while loop:

Syntax:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
107 write a program that displays numbers 0 to 4.
int main() {
for (int i = 0; i < 5; i++) {
cout << i << "\n";
}
return 0;
}
108 print even values between 0 and 10:
int main() {
for (int i = 0; i <= 10; i = i + 2) {
cout << i << "\n";
}
return 0;
}

Nested Loops
It is also possible to place a loop inside another loop. This is called a nested loop.
The "inner loop" will be executed one time for each iteration of the "outer loop":
109 int main() {
// Outer loop
for (int i = 1; i <= 2; ++i) {
cout << "Outer: " << i << "\n"; // Executes 2 times

// Inner loop
for (int j = 1; j <= 3; ++j) {
cout << " Inner: " << j << "\n"; // Executes 6
times(2 * 3)
}
}
return 0;
}
Outer: 1
Inner: 1
Inner: 2
Inner: 3
Outer: 2
Inner: 1
Inner: 2
Inner: 3
110
Write a program in C++ to find the multiplication table using nested for loop.
int main() {
// Outer loop
for (int i = 1; i <= 3; ++i) {
for (int j = 1; j <= 10; ++j) {
cout << i <<"*" <<j<<"=" <<i*j<< "\n";
}
}
return 0;
}

Break and continue

Break
The break statement can be used to jump out of a loop.
111 int main() {
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
cout << i << " ";
}
return 0;
} // 0 1 2 3

Continue
The continue statement breaks one iteration (in the loop), if a specified condition occurs,
and continues with the next iteration in the loop.

112 int main() {


for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
cout << i << "\n";
}
return 0;
}

Break and Continue in While Loop


113 int main() {
int i = 0;
while (i < 10) {
cout << i << "\n";
i++;
if (i == 4) {
break;
}
}
return 0;
}
114 int main() {
int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
}
cout << i << "\n";
i++;
}
return 0;
}

Break and continue in do---while Loop


115 int main() {
int i = 0;
do {
if (i == 4) {
break;
}
cout << i << '\n';
i++;
} while (i < 10);

return 0;
}

116 int main() {


for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
cout<<i<<"\n";
}
}

117 int main()


{
for(int i=1;i<=10;i++){
if(i==5){
continue;
}
cout<<i<<"\n";
}
}
118 int main()
{
for(int i=0;i<=10;i+=2){
if(i==6){
continue;
}
cout<<i<<"\n";
}
}
Break and Continue in While Loop
119 int main() {
int i = 0;
do {
if (i == 4) {
break;
}
cout << i << '\n';
i++;
} while (i < 10);

return 0;
}

120 // program to find the sum of positive numbers


// if the user enters a negative number, break ends the
loop
// the negative number entered is not added to sum

int main() {
int number;
int sum = 0;
while (true) {
// take input from the user
cout << "Enter a number: ";
cin >> number;

// break condition
if (number < 0) {
break;
}

// add all positive numbers


sum += number;
}

// display the sum


cout << "The sum is " << sum << endl;

return 0;
}
continue with while loop
121 / program to calculate positive numbers till 50 only
// if the user enters a negative number,
// that number is skipped from the calculation

// negative number -> loop terminate


// numbers above 50 -> skip iteration

int main() {
int sum = 0;
int number = 0;

while (number >= 0) {


// add all positive numbers
sum += number;

// take input from the user


cout << "Enter a number: ";
cin >> number;

// continue condition
if (number > 50) {
cout << "The number is greater than 50 and
won't be calculated." << endl;
number = 0; // the value of number is made 0
again
continue;
}
}

// display the sum


cout << "The sum is " << sum << endl;

return 0;
}
Enter a number: 5
Enter a number: 6
Enter a number: 8
Enter a number: 3
Enter a number: -9
The sum is 22

You might also like