Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

06-Break and continue



.Introduction:-
Sometimes, it is desirable to skip the execution of a loop for a certain test condition or terminate it immediately without checking the condition.

In scenarios like these, continue; or a break; statement is used.

.Break Statement:-

The break; statement terminates for loop/ while loop / do..while loop)







Example 1--C++ Program to demonstrate working of break statement


#include <iostream>
using namespace std;
int main( )

{
    float n, sum = 0.0;

  
    for(;;)
    {
        cout << "Enter a number: ";
        cin >> n;

        if (n!=0.0)
            {
            sum=sum+n;
             }

        else

            {
            // terminates the loop if number equals 0.0
            break;
             }

    }

    cout << "Sum = " << sum;

    return 0;
}




An infinite for loop has been used here.

for (; ;)

To terminate this infinite loop, we are using break statement .


same result can also be achieved using while(true)





.Break Statement:-





Example 1- Program to write all the natural numbers from 1 to 100 except multiples of 4.

#include<iostream>
using namespace std;
int main () 

{
int i;
for(i=1;i<=100;i++)
{   
if (i%4!=0)
{cout<<i<<endl;}
else
{continue;}
}
return 0;
}


05-Logic operators- c++


Logic operators in c++

The basic logic operators are :-"and,or,not" operators.

&& - and operator

||    - or operator
!     - not operator


04- loops in c++

c++ programming


in programming languages, loops are used to execute a set of statements repeatedly, until a particular condition is satisfied.


There are 3 kind of loops in c++

1. While loop
2.For loop
3.Do-While loop


03.conditional statements in c++



c++ programming

1.If Statement

it is a conditional statement.. The if statement is used to execute the code, only when a condition is true.

if (test condition) 



block statement

}


02.variables in c++

Variables in c++

Variable are used in C++, where we need storage for any value, which will change in program. Variable can be declared in multiple ways...

c++ programming


01.Computer programming

Computer programming is the process of writing instructions that get executed by computers.


c++ programming




There are various kinds of programming languages out there designed for various purposes.some of the popular programming languages are

c/c++
c # (known as c sharp)
Python
javascript
java
perl
swift
php
visual basic
vb.net