Use an Outer Do While Loop That Will Ask the User if They Wish to Run the Program Again. C++
We are assuming that you are practicing a lot. If not, then go to the practice section before proceeding. First solve a good number of issues of all the topics covered and then far so come up back. In example of whatever doubt, ask your question in the discussion section.
while Loop
What if someone asks you lot to print 'Hullo World' 10 times?
One way is to write the printf
statement x times. Only that is definitely not a practiced choice if you accept to write it 50 times!
And so, here comes the while loop.
#include <stdio.h> int chief () { int a = 1 ; while ( a <= 10 ) { printf ( "Hi Earth \due north " ); a ++ ; } return 0 ; }
Hello World
Hi World
Hello World
Hello World
Howdy Earth
Howdy Globe
Hullo World
Hello World
Hello Earth
Hi World
Now, let'southward sympathise each line of the lawmaking.
First, take a await at the syntax of a while loop.
while(condition)
{
statement(s)
}
while
loop checks whether the condition written in '( )
' is true or not.
If the condition is constitute true, then statements written in the body of the while loop i.due east., inside the braces { }
are executed. Then, again the status is checked, and if establish true and then statements in the body of the while loop are executed again. This process continues until the condition becomes faux.
For ameliorate understanding, let's understand our example step by step.
In our case, firstly, we assigned a value of i to a variable 'a'.
while(a <= 10)
checks the condition 'a <= x
'. Since the value of a is 1 which is less than x, the statements of while
(within the braces { }) are executed.
'Hello World' is printed and a++
increases the value of 'a' by 1. And then, now the value of 'a' becomes 2.
Now, once more the condition is checked. This fourth dimension also a <= x
is true because the value of 'a' is ii. And then, again 'How-do-you-do Globe' is printed and the value of 'a' increased to three.
When the value of 'a' becomes 10, again the condition a <= x
is truthful and 'Hi World' is printed for the tenth time. At present, a++
increases the value of 'a' to 11.
This time, the condition a <= 10
becomes simulated and the while
loop terminates.
Wasn't that interesting?
The following animation volition as well help you to understand the while
loop.
Let's see one more instance of while
loop
#include <stdio.h> int main () { char choice = 'y' ; while ( pick == 'y' ) { int a ; printf ( "Enter a number to check odd or even \n " ); scanf ( "%d" , & a ); if ( a % 2 == 0 ) { printf ( "Your number is even \due north " ); } else { printf ( "Your number is odd \due north " ); } printf ( "Desire to check more y for yep n for no \north " ); scanf ( " %c" , & choice ); } return 0 ; }
Enter a number to check odd or fifty-fifty
2
Your number is fifty-fifty
Want to check more than y for yeah n for no
y
Enter a number to cheque odd or fifty-fifty
five
Your number is odd
Want to cheque more y for yes n for no
north
The loop volition run until the value of 'choice' becomes different than 'y'. And so, for the first time, it will run since 'choice' is 'y'. Then it will execute the codes inside the loop. At terminal, it volition ask the user whether he wants to bank check more than. And this tin can alter the value of the variable 'choice' and may terminate the loop.
Note that the variable 'a' is divers inside the while loop and will be invalid exterior that.
Another Loop - FOR
Let's go to our first example in while loop where nosotros accept to print 'Hello Globe' x times.
We can besides practice the aforementioned with for
loop. But before that, allow's expect at the syntax of for
loop.
for(initialization ; condition ; propagation)
{
statement(due south)
}
As stated, for
is also a loop which repeats the codes inside its body. The code is repeated until the status
is true. Annihilation written in the place of initialization
is executed just in one case and before starting the loop. The statement written at the place of propagation
is executed after every iteration of the loop. Let'south look at an example to sympathize it better.
#include <stdio.h> int main () { int a ; for ( a = i ; a <= 10 ; a ++ ) { printf ( "Hello World \due north " ) ; } return 0 ; }
Hi World
Hi World
Hello World
How-do-you-do World
Hello Globe
Hello World
Hello World
Hi Earth
How-do-you-do World
Howdy World
Now let'due south see how for
loop works.
for(a=ane; a<=10; a++)
a=1
→ This is the initialization of the loop and is executed once at the starting of the loop. By and large, it used to assign value to a variable. Here, 'a' is assigned a value 1.
a<=10
→ This is the condition which is evaluated. If the condition is truthful, the statements written in the trunk of the loop are executed. If it is imitation, the statement but later the for loop is executed. This is similar to the condition nosotros used in the while
loop which was existence checked once more and again.
a++
→ This is executed later on the execution of the code in the torso of for
loop. In this example, the value of 'a' increases by 1 every time the lawmaking in the torso of for loop executes. There tin be whatsoever expression hither which you want to run afterwards every iteration of the loop.
In the to a higher place example, firstly a=1
assigns the value of 1 to the variable a.
Then condition a<=10
is checked. Since the value of 'a' is 1, therefore the code in the body of for
loop is executed and 'Hello Earth' gets printed.
Once the codes in the body of for
loop are executed, a++
is executed which increases the value of 'a' by one. Now, the value of 'a' is two.
Again the status a<=ten
is checked which is true because the value of 'a' is ii. Once more codes in the torso of for loop are executed and 'Hello World' gets printed and then the value of 'a' is again increased by i (a++
).
When the value of 'a' becomes ten, the condition a <= 10
evaluates to true and 'Howdy World' gets printed. Now, when a++
increases the value of 'a' to 11, the status a<=10
becomes false and the loop terminates.
Don't you retrieve it's just a different course of while loop? Yes, it is actually.
We can too skip any of the initialization
, condition
or propagation
statements. Let'south await at the following examples of writing the same code in different ways.
{
int a = ane ;
for ( ; a <= ten ; a ++ )
{
printf ( " Hello Globe " ) ;
}
}
We tin also write information technology as:
{
int a ;
for ( a = 1 ; a <= 10 ; )
{
printf ( " Hello World " ) ;
a ++ ;
}
}
Try these once.
Now let'due south see some examples of for loop.
#include <stdio.h> int main () { int a ; printf ( "Tabular array of 12 \n " ); for ( a = 1 ; a <= 10 ; a ++ ) { printf ( "12*%d = %d" , a , 12 * a ) ; } render 0 ; }
Table of 12
12*one = 12
12*2 = 24
12*3 = 36
12*4 = 48
12*5 = sixty
12*6 = 72
12*seven = 84
12*8 = 96
12*9 = 108
12*10 = 120
Information technology is like shooting fish in a barrel to empathize. In the first iteration, the value of 'a is 1. And then, 12*a
is 12. In the 2d iteration, the value of 'a' is 2. So, 12*a
is 24. And at last, 'a' is 10. So, 12*a
is 120.
#include <stdio.h> int main () { int a , b , power , i ; power = one ; printf ( "To find a raised to the power b. \n give value of a" ); scanf ( "%d" , & a ); printf ( "Give value of b \n " ); scanf ( "%d" , & b ); for ( i = 1 ; i <= b ; i ++ ) { power = power * a ; } printf ( "Power of a'%d' raised to b'%d' is %d \north " , a , b , power ); return 0 ; }
To discover a raised to the power b.
give value of a
five
Requite value of b
4
Power of a'five' raised to b'4' is 625
To calculate ab, nosotros take taken a variable 'power' and its initial value is ane.
In the showtime iteration, ability = power*a
will exist power = 1*a. So, ability volition become 'a'.
In the second iteration, 'power' will be a*a
i.due east., a2
In the third iteration, 'ability' will be a2*a
i.e., a3.
So, in bth iteration, 'power' will exist ab
.
Nested loop
Like if/else
, we can also use ane loop inside another. This is chosen nesting of loops.
Run across this example to make it articulate.
#include <stdio.h> int main () { int i ; int j ; for ( i = 12 ; i <= 14 ; i ++ ) { /*outer loop*/ printf ( "Table of %d \n " , i ); for ( j = ane ; j <= ten ; j ++ ) { /*inner loop*/ printf ( "%d*%d \t = \t %d \n " , i , j , i * j ); } } return 0 ; }
Table of 12
12*one = 12
12*ii = 24
12*3 = 36
12*4 = 48
12*v = 60
12*6 = 72
12*7 = 84
12*8 = 96
12*9 = 108
12*10 = 120
Tabular array of xiii
13*one = 13
thirteen*ii = 26
13*three = 39
thirteen*four = 52
thirteen*5 = 65
13*6 = 78
thirteen*7 = 91
thirteen*8 = 104
13*nine = 117
13*x = 130
Table of 14
14*i = fourteen
fourteen*2 = 28
14*iii = 42
fourteen*four = 56
14*5 = 70
14*half dozen = 84
14*7 = 98
fourteen*8 = 112
xiv*9 = 126
xiv*10 = 140
At the initial execution of the start, 'i' is 1 and thus, 'Tabular array of 12' gets printed.
Now, the next statement is for(j = ane;j<=ten;j++)
. Then, executing this statement:
'j' is ane and 12*1 = 12
volition be printed.
Now, the second iteration of the inner loop will take place. 'i' is yet 12 simply 'j' will be increased to 2. So, 12*two = 24
will be printed.
Coming to the concluding iteration of the inner loop, 'i' is even so 12, and 'j' will be 10, so 12*10 = 120
volition be printed.
Now, coming out of the outer loop, there is no argument after the inner loop, so 'i' will exist increased to 13 (i++
) for the adjacent iteration of the outer loop and 'Table of 13' will exist printed and things volition exist repeated with the value of 'j' equal to xiii.
It's absurd to see this working, isn't information technology?
Nosotros tin also initialize multiple variables and apply multiple weather and propagation steps in for loop by just using a comma ' , '
Let's encounter this working.
Suppose nosotros have to change the values of a variable 'a' from 1 to 5 and the values of another variable 'b' from 5 to ane.
#include <stdio.h> int main () { int a ; int b ; for ( a = 1 , b = 5 ; a <= v , b >= 1 ; a ++ , b -- ) { printf ( "a = %d \t b = %d \n " , a , b ) ; } return 0 ; }
a = one b = 5
a = 2 b = 4
a = 3 b = iii
a = iv b = 2
a = v b = ane
'\t' is used to provide tabspace like '\n' gives a newline.
So, y'all must be having fun by now. You lot will have a lot more fun when you lot utilise your coding skills in practical situations like games, websites, robots, etc.
do...while loop
It is just like the while
and for
loop but the only deviation is that the code in its body is executed once
do
{
statement(due south)
}
while(status);
Now, let's see the same example of printing 'Howdy Earth' x times but this time with exercise...while
loop.
#include <stdio.h> int primary () { int a = 1 ; do { printf ( "Hello World \n " ); a ++ ; } while ( a <= 10 ); render 0 ; }
Hello World
Hello Globe
Hello Earth
How-do-you-do Earth
Hello Globe
Hello World
Hello World
Hi Earth
Hello Earth
Howdy World
Let's understand this.
At first, the codes inside the torso of loop (i.due east. inside the braces { } following do
) are executed without checking condition. This prints 'How-do-you-do World' and a++
increments the value of 'a' past 1. So, the value of 'a' becomes two.
In one case the code inside the braces ({ }) is executed, condition 'a <= 10'
is checked. Since the value of 'a' is 1, and then the status is satisfied.
Again the code within the body of loop is executed and the value of 'a' becomes ii.
When the value of 'a' is x and 'Howdy World' is printed for the tenth time, a++
increases the value of 'a' to eleven. After this, the condition becomes simulated and the loop terminates.
We just saw three different types of loops which are used to repeat a certain process some number of times and they were fun as well, I hope. Just you lot need to solve questions to make your base in coding concrete.
What if loop goes on and on - Space Loop
There may be some loops that can iterate or occur infinitely. These are chosen Infinite Loops. These loops occur infinitely because their condition is e'er true.
Nosotros tin can make an infinite loop past leaving its conditional expression empty. When the conditional expression is empty, it is assumed to be true.
Permit's encounter an example on how to make a for loop space.
#include <stdio.h> int master () { for (;;) { printf ( "This is not gonna end! \northward " ); } return 0 ; }
This is not gonna cease!
This is not gonna cease!
This is not gonna end!
This is not gonna stop!
This is non gonna end!
This is not gonna end!
This is not gonna end!
This is not gonna cease!
This is not gonna stop!
This is not gonna finish!
This is not gonna stop!
This is not gonna cease!
This is not gonna finish!
This is not gonna finish!
...
To finish an infinite loop, press Ctrl + C.
To learn from simple videos, you can always look at our C++ video class on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.
Don't spend so much time trying to choose the perfect opportunity that you lot miss the right opportunity.
- Michael Dell
robinsonatephy1951.blogspot.com
Source: https://www.codesdope.com/c-loop-and-loop/
0 Response to "Use an Outer Do While Loop That Will Ask the User if They Wish to Run the Program Again. C++"
Postar um comentário