2 - Loops

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Errors

• Syntax Errors – Compilation Errors


– Errors in programming language rules.
– You can use the compiler or interpreter to uncover syntax errors.
– You must have a good working knowledge of error messages to
discover the cause of the error.

• Logic or Meaning Errors


– Errors that indicate the logic used when coding the program failed
to solve the problem.
– You do not get error messages with logic errors.
– Your only clue to the existence of logic errors is the production of
wrong solutions.

• Run-time Errors (Exceptions)


– Code does something illegal when it is run (hence runtime)
E.g., divide by zero
Loops
• If we want to print “Hello world” 10 times.

– “Hello world “
– “Hello world “
– “Hello world “
– “Hello world “
– “Hello world “
– “Hello world “
– “Hello world “
– “Hello world “
– “Hello world “
– “Hello world “
Loops
• If we want to print “Hello world“ 1000 times ?
It's too boring, right ?

For this, we will make a Loop


So, What is the Loop ??
Loops
• Loops : repeat a statement a certain
number of times, or while a condition
is Satisfied.

They are introduced by the keywords


while, do-while, and for.
Loops
• for loop : The for loop is designed to iterate a
number of times.

Its syntax is:


for (initialization; condition; increase) {
// statements
}
Loops
A simple for loop code
#include <iostream>
using namespace std;
int main() {
for(int i = 0; i < 100; i++){

cout << "Hello world" << endl;

}
return 0;
}
Loops
While loops: While loops have only a condition in its syntax
“while the condition is true, go in the loop”.

Syntax :
while( condition ) {
// Statement
}
int main() {
int i = 0;
while(i < 100){
cout << "Hello world" << endl;
i++;
}
return 0;
}
Loops
• do-while loop : The do-while loop is a very
similar loop,
whose syntax is:
do {

// Statements

} while (condition);
Loops
A simple do-while loop code
int main() {
int i = 0;
do {
cout << "Hello world" << endl;
i++;
}while(i < 100);
return 0;
}
Problems
1. Write a program to print numbers from 1 to 10

2. Write a program to count numbers of odd and


even between 1 and 10

3. Write a program to take a number from user


and print its factorial.

4. Write a program to take a 10 numbers from


user and print largest and the smallest one .
Loops
● Break statement for(int i = 0; i < 10; i++){
int a;
when the compiler read this statement, it cin >> a;
exit from the loop and ignore the if(a % 2 == 0){
break;
remainder statements
}
}
In this code : when a is even, the loop is
break

int sum = 0;
● Continue statement for(int i = 0; i < 10; i++){
when the compiler read this int a;
statement, it ignore the cin >> a;
remainder statements and go to if(a % 2 == 0){
the next time in the loop continue;
}
sum = sum + i;
In this code, sum will have the
}
summation of odd numbers
Problems
1. Write a program take a numbers from user
until enter 0.

2. Write a program to take number from user and


determine it`s prime or not
Nested Loops
Loop inside Loop
Ex :
int main() {
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
cout << "*" << " " ;
}
cout<< "\n" ;
}
}
Problems
• Write a program to print
#
##
###

• Write a program to print


####
###
##
#
Scopes, Global, Local
#include <iostream>
using namespace std;
int main() {
for(int i = 0; i < 10; i++){
cout << i << endl;
}
cout << i << endl;
}
Notes
• Test Cases.
• Infinity loop.
• break in nested loops
• loop counter can increase by 1 or more and decrease
• fast code , ‘\n’

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}

You might also like