C++ For Loop
C++ For Loop
• Ouput: 1 2 3 4 5 6 7 8 9
10
• C++ Nested While Loop Example
• In C++, we can use while loop inside another
while loop, it is known as nested while loop.
The nested while loop is executed fully when
outer loop is executed once.
• #include <iostream>
• using namespace std;
• int main () {
• int i=1;
• while(i<=3)
• {
• int j = 1;
• while (j <= 3)
• {
• cout<<i<<" "<<j<<"\n";
• j++;
• }
• i++;
• }
• }
• Output: 1 1 1 2 1 3 2 1 2 2 2 3 3 1
3 2 3 3
• C++ Infinitive While Loop Example:
• We can also create infinite while loop by
passing true as the test condition.
• #include <iostream>
• using namespace std;
• int main () {
• while(true)
• {
• cout<<"Infinitive While Loop";
• }
• }
• Output:
• Infinitive While Loop
• Infinitive While Loop
• ctrl+c
• C++ Do-While Loop
• The C++ do-while loop is used to iterate a part of
the program several times. If the number of
iteration is not fixed and you must have to execute
the loop at least once, it is recommended to use
do-while loop.
• The C++ do-while loop is executed at least once
because condition is checked after loop body.
• do{
• //code to be executed
• }while(condition);
• #include <iostream>
• using namespace std;
• int main() {
• int i = 1;
• do{
• cout<<i<<"\n";
• i++;
• } while (i <= 10) ;
• }
• Output: 1 2 3 4 5 6 7 8 9
10
• C++ Nested do-while Loop
• In C++, if you use do-while loop inside another
do-while loop, it is known as nested do-while
loop. The nested do-while loop is executed
fully for each outer do-while loop.
• #include <iostream>
• using namespace std;
• int main() {
• int i = 1;
• do{
• int j = 1;
• do{
• cout<<i<<"\n";
• j++;
• } while (j <= 3) ;
• i++;
• } while (i <= 3) ;
• }
• Output: 1 1 1 2 1 3 2 1 2 2 2 3
3 1 3 2 3 3
• C++ Infinitive do-while Loop
• In C++, if you pass true in the do-while loop, it
will be infinitive do-while loop.
• do{
• //code to be executed
• }while(true);
• C++ Infinitive do-while Loop Example
• #include <iostream>
• using namespace std;
• int main() {
• do{
• cout<<"Infinitive do-while Loop";
• } while(true);
• }
• Output:
• Infinitive do-while Loop
• Infinitive do-while Loop
• Infinitive do-while Loop
• Infinitive do-while Loop
• Infinitive do-while Loop
• ctrl+c
•
• C++ Break Statement
• The C++ break is used to break loop or switch
statement. It breaks the current flow of the
program at the given condition. In case of
inner loop, it breaks only inner loop.
• jump-statement;
• break;
• #include <iostream>
• using namespace std;
• int main() {
• for (int i = 1; i <= 10; i++)
• {
• if (i == 5)
• {
• break;
• }
• cout<<i<<"\n";
• }
• }
C++ Break Statement with Inner Loop
• #include <iostream>
• using namespace std;
• int main()
• {
• for(int i=1;i<=3;i++){
• for(int j=1;j<=3;j++){
• if(i==2&&j==2){
• break;
• }
• cout<<i<<" "<<j<<"\n";
• }
• }
• }
C++ Continue Statement
• #include <iostream>
• using namespace std;
• int main()
• {
• for(int i=1;i<=3;i++){
• for(int j=1;j<=3;j++){
• if(i==2&&j==2){
• continue;
• }
• cout<<i<<" "<<j<<"\n";
• }
• }
• }
C++ Goto Statement
int main() {
myFunction(); // call the function
return 0;
}
Advantage of functions in C++