Course Introduction to Programming
Command: doWhile
In this lesson, we will learn a new repetition structure, doWhile. We will follow the topics:
- Task;
- doWhile structure;
- Coding.
Task
Make a program that reads an integer
- Use the while structure.
- Use the for structure.
- Use the doWhile structure.
doWhile Structure.
The doWhile structure is a repetition structure that performs a code snippet while a certain condition is true. The difference between doWhile and the while structure is that doWhile does not test the condition before the first loop.
Syntax
do{
//Code
} while( condition );
We will now use the structure to solve the problem.
#include<stdio.h>
int main(){
int n;
do{
scanf("%d", &n);
} while(n!=2018);
}
First, we ask the program to reserve space in memory for an integer variable
Now you know how to solve repetition problems using doWhile and its similarities and differences when compared to the while structure.