for, while and do-while+infinite loops in c++
#include<iostream>
using namespace std;
int main() {
/*loops in c++:
there are 3 types of loops in c++:
1)For loop
2)while loop
3)Do-while loop*/
//For loop in c++:
// let me say i wanna print numbers upto 50 so i can do the following things
// #printing numbers just by cout
// cout<<1;
// cout<<2;
// cout<<3;
// and soo on
// #or
// int i=1;
// cout<<i;
// i++;
// cout<<i;
// i++;
// and soo on
// #or we can use loops for it, for very simplification
// for (int i = 0; i <= 50; i++)
// {
// code
// cout<<i<<endl;
// i++; yaha i++ nhi lgana vrna 2 baar updation hojaega
// }
// syntax for "for" loop:
// for (initialization; condition; updation)
// {
// loop body (c++ code);
// }
// sbse pehle initialization hoga (sirf ek baar) uske baad conditon check hogi
// then compiler loop body ke and jaega and then updation krega and so on
//hum infinite loop bi bana skte hain, conditon ko hmesha true rkhein to
//for example
// for (int i = 0; 40 <= 50; i++)
// {
// /* code */
// cout<<i<<endl;
// // i++; yaha i++ nhi lgana vrna 2 baar updation hojaega
// }
//ye infinite loop hai coz 40 is always less than 50
/*******WHILE LOOP IN C++*******/
// int i=1;
// while (i<=50){
// cout<<i<<endl;
// i++;
// }
// syntax for while loop:
// while (condition){
// c++statements;
// }
//we can also make infinite loop using while loop
//for example:
// while(true){
// cout<<i<<endl;
// i++;
// }
// it'll print numbers upto inifnity
/**************DO WHILE LOOP IN C++*************** */
// int i=1;
// do{
// cout<<i<<endl;
// i++;
// } while (i<=50);
// syntax for do while loop:
// do{
// c++ statements;
// } while (codition);
//do while loop atleast ek baar to execute hota hi hai
//fpr example
int i=1;
do{
cout<<i<<endl;
i++;
} while(false);
//sirf 1 print hoga then baki sbme condition false hojaegi
//we can also make infinite while loop by using "true" as condition
return 0;
}
Comments
Post a Comment