Repetition Structure in C programming

Outcomes

In here you can learn while repetition statement, for repetition statement, do…while repetition statement. Also you can learn break and continue statements, counter-controlled and sentinel-controlled repetitions. So let’s start repetition structure in C programming.

Repetition Structure in C programming

Repetition Structure in C programming

In conclusion known as iteration. If the user want to repeat some action while the condition remains true, user can use this structure.

  • Counter -Controlled Repetition

In short we can say in here the number of repetitions is know before the loop begins execution. Because a control variable is used to count the number of repetitions. After that the repetition terminates when the counter exceeds number of repetitions.

  • Sentinel-Controlled repetition

When no indication is given of how many times the loop should execute, a sentinel value is used to terminate the loop. For example type -1 to terminate entering of marks. A loop should have a statement to obtain data each time the loop is performed.


Repetition Structure : While Statement

In the below shows the format of a while statement.

while( condition ) {

      statements

In the body of the while statement may contain single or many statements. For example,

Repetition Structure : While Statement example
Repetition Structure : While Statement example

Let’s see the answer of the above code.

output of the above code
output of the above code

Okay let’s see another example.

Quiz 01 – Get the marks of 10 students from the keyboard and calculate the total and the average. (You can use repl.it to do your program )

Answer for the quiz 01
Answer for the quiz 01

Do you have any problems in variables and other parts of this code. You can refer Selection Control Structure and C programming beginning posts. Let’s see the output of the above quiz.

output of the above quiz
output of the above quiz

Repetition Structure : for Statement

Let’s see the format of the for statement.

for( expression 1 ; expression  2; expression  3 ){

              statements 

}

Expression in the for statement’s header are optional. So let’s see the general format .

format of the for statement.
format of the for statement.

Let’s see an example. for( i = 0; i < 5; i++ ) , In here integer value “i” started from 0 and it repeated 5 times. Increment expression acts like a standalone statement. because it is in the statement’s header.

In the statement’s header, first you have to declare a variable. if you don’t need that leave a blank. like for( ; i < 5; i++ ). In the expression 2 you have to give the condition you want. If you don’t have this also, leave a blank. like for( ; ; i++ ). 

In the expression 3, you have to give the increment statement. If don’t have this also, you can leave a blank. like for(  ;  ;  ).  It means for(  ;  ;  ) also the correct header. But this is not correct in coding. You must give the expressions into for statement’s header. If you try to leave blanks in the code, it will give you an warning messages.

output with error
output with error compiler

try to do this quiz, use a c online compiler.

Quiz 02 – Write a program that will print the following sequence of value( hint use a for loop ) sequence – 3, 8, 13, 18, 23


Repetition Structure : do…while Statement

In this statement loop continuation condition is checked after the loop body is performed. Therefore the loop body will be executed at least once. Let’s see the format,

do{

     statements

}while( condition );

see the example in the below, you can get an idea of do…while statement. get the output with your own in the compiler.

do...while statement
do…while statement

Repetition Structure in C programming : Break Statement

The break statement is executed in while, for, do…while, statements to exit from that statement. After that program execution continues with the next statement. See the example given below.

break Statement
break Statement

Repetition Structure in C programming : Continue Statement

This also executed in while, for and do…while statements. In this statement skips the remaining statements in the body of that control statement and do the next iteration. see the example given below.

Continue Statement
Continue Statement

Repetition Structure : Nested Iteration Statement

Now you know the iterations. In this statement we are going to use that iterations as nested. See the example given below,

Nested Iteration Statement
Nested Iteration Statement

Let’s see the output,

Output for the above code
Output for the above code

So this is the end of this post. If you have any problems Contact us.

Thank you. Make sure to leave a comment.

There are more articles from BuildITMasters. Follow the below link to refer.

Other articles from different categories.

guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x