Programming Fundamental Instructor Name: Muhammad Safyan Lecture-10
Loop Order of movement of control in for loop: First time: Expression 1-> Expression 2->Loop body -> Expression 3 Second time and onward: Expression 2->Loop body -> Expression 3
Properties of Expression-1 Expression1 can initialize the more than one variable. For example:. main(){ int i,j,k; for(i=0,j=2,k=1;i<=4;i++) { cout<<i+j+k; } Getch(); } Output: 3 4 5 6 7
Properties of Expression-1 Expression1 is optional. For example: main(){ int i=1; for(;i<=4;i++){ cout<<i; } getch(); } Output: 1 2 3 4
Properties of Expression-2 Expression2 is also optional. For example: main(){ int j; for(j=0; ;j++){ cout<<j; if(j>2) break; } Getch(); } Output: 0 1 2
Properties of Expression-2 If expression2 is zero means condition is false and any non zero number means condition is true. For example main(){ int i; for(i=0;-5 ;i++){ cout<<i; if(i==3) break; } Getch(); } Output: 0 1 2 3
Properties of Expression-2 If expression2 is zero means condition is false and any non zero number means condition is true. For example main(){ int i; for(i=5;0 ;i++){ cout<<i; } Getch(); } Output:
Properties of Expression-3 It is called as instrumentation expression. Task of this expression is to increment the variable. Properties: We can increment more than one variable at the same time in the expression3. For example main(){ int i,j,k; for(i=0,j=0,k=0;k<=3;i++,++j,k+=2)) { cout<<i+j+k; } Getch(); } Output: 0 4
Properties of Expression-3 It is called as instrumentation expression. Task of this expression is to increment the variable. Properties: We can increment more than one variable at the same time in the expression3. For example main(){ int i,j=0; for(i=0;i<=3;++i,i++,++j ){ cout<<i<<”,”<<j;; } Getch(); } Output: 0 0 2 1
Properties of Expression-3 Expression 3 is also optional. For example main(){ int i; for(i=0;i<=3; ){ cout<<i++; } Getch(); } Output: 0 1 2 3
main(){ Loop body: int i,j=0; for(i=0;i<=3;++i,i++,++j ) Loop body contains the part of code which we have to execute multiple numbers of times. Properties of loop body: If loop body contain only one statement then brace is optional. For example: main(){ int i,j=0; for(i=0;i<=3;++i,i++,++j ) cout<<i<<j; Getch(); } Output: 0 0 2 1
main(){ Loop body: int i; for(i=0;i<=10;i++); cout<<i; Loop without body is possible. For example main(){ int i; for(i=0;i<=10;i++); cout<<i; Getch(); } Output: 11
for ( int row = 1; row <= 10; ++row ) { for ( int col = 1; col <= row; ++col ) cout << '*'; cout << '\n'; }
for (int row = 10; row >= 1; --row ) { for ( int col = 1; col <= row; ++col ) cout << '*'; cout << '\n'; }
for ( int row = 1; row <= 10; ++row ) { for ( int space = 1; space <= 10 - row; ++space ) cout << ' '; for ( int col = 1; col <= row; ++col ) cout << '*'; cout << '\n'; }
for ( int row = 10; row >= 1; --row ) { for ( int space = 1; space <= 10 - row; ++space ) cout << ' '; for ( int col = 1; col <= row; ++col ) cout << '*'; cout << '\n'; }
Nested LOOP main() { int i,j; i=1; j=1; while(i++<=100) while(j++<=200) if(j==150) break; else cout<<i<<"----"<<j<<"\n"; } getch();
A while loop nested in for loop
A while loop nested in for loop long sum; int i, j, count=10; for(i=1; i<=count; i++) { sum=0; j=1; while(j<=i) //++j; sum=sum+j; cout<<j<<"+"; ++j; } cout<<"="<<sum; cout<<endl;