Presentation 7
Presentation 7
In C++, control statements are constructs that allow you to control the flow of
execution in a program.
They help in making decisions, repeating code, or altering the normal sequence of
program execution based on certain condition.
1:Entry Controlled Loops: In this type of loop, the test condition is tested
before entering the loop body.
2: Exit Controlled Loops: In this type of loop the test condition is tested or
evaluated at the end of the loop body. Therefore, the loop body will execute at
least once, irrespective of whether the test condition is true or false. The do-
while loop is exit controlled loop.
What is For Loop?
In C++, a for Loop is a control statement used to repeatedly execute a block of code for a
specific number of time.
A For Loop is a repetition control structure that allows us to write a loop that is executed a
specific number of times.
The loop enables us to perform n number of steps together in one line.
#include <iostream>
using namespace std;
for ( i = 0 ; i < 10 ; i+
main(){
+){
int number;
cout<<i<<endl;
cout<<"enter a number
}
"<<endl;
cin>>number;
for (int i = 1; i <= 10 ;
for (int i = 1; i <=
i++){
20 ; i++){
cout<<i<<" x
if (i % 2 == 0){
"<<number<<" = "<<i *
cout<<i
number<<endl;
<<endl;
}
}
}
}
for loop examples:
#include <iostream>
using namespace std;
main(){
int score = 0;
char answer;
for (int i = 1; i <= 10 ; i++){
switch (i){
case 1:
cout<<"Who is the father of computer? "<<endl;
cout<<"a)Charles babbage\nb)Alan Turin\nc)Jonhn von
Neumanam\nd) Bill Gates"<<endl;
cout<<"Enter you'r answer (a/b/c/d): ";
cin>>answer;
if (answer == 'a'){
score++;
}
break;
case 2:
cout<<"\nQuestion 2: CPU stand for? "<<endl;
cout<<"a)Central process Unit\nb)Computer Processing
Unit\nc)Central Porcessing Unit\nd) Genral Processing Unit"<<endl;
cout<<"Enter you'r answer (a/b/c/d): ";
cin>>answer;
if (answer == 'b'){
score++;
}
break;
case 3:
cout<<"\nQuestion 3: Whic of the following is the brain of
the computer? "<<endl;
cout<<"a)CPU\nb)Memory\nc)ALU\nd)Hard Disk"<<endl;
cout<<"Enter you'r answer (a/b/c/d): ";
cin>>answer;
if (answer == 'a'){
score++;
}
break;
}
}
What is While Loop in C++?
In C++, the while loop is an entry controlled loop that repeatedly executes a block of
code as long as the given condition remains true.
Wile loop unlike the for loop, it is used in situations where we do not know the exact
number of iterations of the loop beforehand as the loop execution is terminated on the
basis of the test condition.
Syntax Flow chart Working of
while loop
Initialization expression;
While (test_expression)
{
// statements
Update_expression;
}
Example of while loop
#include <iostream>
using namespace std;
#include <iostream> #include <iostream> main(){
using namespace std; using namespace std; int number;
main(){ main(){ int sum = 0;
int n, factorial = 1, i = 1;cout<<"Enter numbers to sum them up (Enter a negative number to stop):
// intialization "<<endl;
cout<<"Enter a positive // While loop to sum numbers until a negative number is entered
expression integer: "; while (true){
int i = 1; cin>>n; cin >>number; // User input
// test expression if (number < 0){
while (i <= n ){
break; // Exite the loop if a negative number is entered
while (i < 6){ factorial *= i; }
cout<<"Hello i++; sum +=number;
} }
World\n"; cout<<"The total sum is: "<<sum <<endl;
cout<<"Factorial of
// Update expression "<<n <<" is: "<<factorial<<endl;
}
i++; }
}
}