1 / 22

Loops

Loops. different types: for while etc. For Loop. for(initialValue ; test-expression ; update-expression) { BODY }. Example. #include<iostream.h> int main() { int a; for (a = 0; a < 10; a++ ) { cout << a << endl; } return 0; }. While loop.

Download Presentation

Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Loops • different types: • for • while • etc.

  2. For Loop • for(initialValue; test-expression ; update-expression) { BODY }

  3. Example #include<iostream.h> int main() { int a; for (a = 0; a < 10; a++ ) { cout << a << endl; } return 0; }

  4. While loop • while(test-expression) { BODY }

  5. Example #include<iostream.h> int main() { int arSize = 20; char name[arSize]; cin >> name; int a = 0; while (name[a] != ‘\0’) { cout << name[a] << endl; a++; } return 0; }

  6. Conditional Statements • if statements • Syntax: if (condition) { IF_BODY } else { ELSE_BODY }

  7. Conditions: • Relational Expressions: • Comparisons: • e.g • == tests equality • > is greater?

  8. Example int number; cin >> number; if ( number < 0) { cout << “Negative Value” << endl; } else { cout << “Non-negative Value” << endl; }

  9. Comparing two strings #include<iostream.h> int main() { int arSize = 20; char name1[arSize]; char name2[arSize]; cout<<“String1”<<endl; cin>>name1; cout<<“String2”<<endl; cin>>name2;

  10. int len1,len2,a; len1=strlen(name1); len2=strlen(name2); bool flag=true; if (len1 != len2 ) cout<<“different”; else { for (a=0; (name1[a]==name2[a]) && a <len1 ;a++); if (a==len1) cout<<“The same”; else cout<<“different”; } }

  11. Sort an Array of Integer #include<iostream.h> int main() { int arSize = 20; int ArrayNumber[arSize]; int index; for(index=0; index <20; index++) { cout<<“enter an integer”<<endl; cin>>ArrayNumber[index]; }

  12. int i,j; int temp; for( i=1; i< 20;i++) for (j=0; j<20-i ; j++) if (ArrayNumber[ j] > ArrayNumber[ j+1]) { temp= ArrayNumber[ j ]; ArrayNumber[ j ]=ArrayNumber[ j+1]; ArrayNumber[ j+1]=temp; } }

  13. do while do { Body } while( test-expression); Body executes at least once!

  14. Write a program to compute converse of a positive integer number 1246 6421

  15. long num; long num_converse=0; do { num_converse = num_convers*10+ num % 10; num = num/10; } while(num>0); cout<<num_converse;

  16. More about I/O cin.get(), cin.getline() • Both cin.get() and cin.getline() read the entire input line( up to newline character) • getline() discards the newline char whereas get() leaves it in the input queue

  17. Example cin.get(name, Arsize); cin.get(dessert,Arsize); //there is a problem Should be : cin.get(name, Arsize); cin.get(); cin.get(dessert,Arsize); Or we can write cin.get(name, Arsize).get() // read string and newline cin.get(dessert,Arsize); cin.get(name,Arsize) //returns the cin object which can be used to invoke the get().

  18. We can write : cin.getline(nema1,Arsize).getline(name2,Arsize);

  19. char ch; cin.get(ch); we can write ch=cin.get();

  20. Write a program to find the greatest common divisor of 2 input numbers.

  21. Write a program to read n and print out the n lines input like (n=5,7)

More Related