First program in C++ is to prints “Hello World!” message on the screen of the
computer. During the processing phase, we don’t need any parameter (variables)
for this problem.
Size: 209.31 KB
Language: en
Added: Oct 15, 2024
Slides: 13 pages
Slide Content
COMPUTING FOR ENGINEERING
MS :HNADI ALI
Control Statements
Example : Write Program in C++ that Display Numbers
from 1 to 5 ?
#include <iostream>
using namespace std;
int main() {
int counter = 1;
while (counter <= 5)
{
cout<< counter ;
++ counter;
}
}
The do-while Loop
1- loop counter is any numeric variable ( int , float ,….).
2- The initial value of the loop counter is up to you, but you have to increment it to avoid
endless loops.
3- The Loop Body is Executed at least one Time.
The do-while Loop
The do-while Loop
The body of the loop is executed at first. Then the condition is evaluated.
If the condition evaluates to true, the body of the loop inside the do statement is
executed again.
The condition is evaluated once at least.
This process continues until the condition evaluates to false. Then the loop stops.
The do-while Loop
The do...while loop is a variant of the while loop with one important difference:
the body of do...while loop is executed once before the condition is checked.
The do-while Loop
Example : Write a program that calculates and prints out the Average
grade for 6 students .
The do-while Loop
int counter = 1;
int grade=0 , sum=0;
do
{
cout<<"Enter grade for student no " << counter <<"\n"
cin>>grade;
sum = Sum + grade;
counter ++;
}
while (counter >=6) ;
cout<<"Average Grade is " << sum/6 <<"\n";
The do-while Loop
Example : Write Program in C++ that Display Numbers
from 1 to 5 ?
The do-while Loop
Example : Write a C++ program that print phrase hello
10 times?