Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops different types: – for – while – etc.. For Loop for(initialValue; test-expression ; update- expression) { BODY }

Similar presentations


Presentation on theme: "Loops different types: – for – while – etc.. For Loop for(initialValue; test-expression ; update- expression) { BODY }"— Presentation transcript:

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

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

3 Example #include 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 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 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 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) 11111 12221 12321 12221 11111 1111111 1222221 1233321 1234321 1233321 1222221 1111111

22 1111 1221 1221 1111


Download ppt "Loops different types: – for – while – etc.. For Loop for(initialValue; test-expression ; update- expression) { BODY }"

Similar presentations


Ads by Google