0% found this document useful (0 votes)
14 views

Presentation 7

Uploaded by

asmat yasin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Presentation 7

Uploaded by

asmat yasin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

C++

Lecture: Asmat Yasin


Email: asmat.yasin123@gmail.com
Phone: 0777 990 542
WhatsApp: 0777 990 542
What is control Statements in C++

 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.

Types of control statements in C++


• Decision-Making Statements
• Looping or iterative statements
• Jump Statements
What is iterative statement or Loop in C++
 In programming , sometimes there is a need to perform some operation more than once or (say)
n number of times.
 Loop come into use when we need to repeatedly execute a block of statements.
For example: Suppose we want to print “Hello World” 10 times. This can be done in two ways as
shown below:
1: Manual Method 2: Iterative Method
 1: Manually we have to write cout for the C++ statement 10 times.
 2: In Loop, the statement needs to be written only once and the loop will be executed 10 times.
There are mainly two types of loop:

 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.

Syntax: Flow Diagram of for


loop

for (initialization ; condition ; increment /


decrement) {
// Code to be executed in the loop
}
Example:

#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++; }
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy