1 / 10

CS 101

CS 101. Lecture 4 Repetition. pre and post increment and decrement.

dillan
Download Presentation

CS 101

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. CS 101 Lecture 4 Repetition

  2. pre and post increment and decrement • If i=i+1; we may write i++ to mean the same thing, and is called the postincrement operator. ++i also means i=i+1 and is called the preincrement operator. --i and i-- are called the preincrement and postincrement operators and each means i=i-1. • The difference shows up in expressions and commands. If i = 4 then cout<<++x<<‘ ‘<<x;will print out 5 5 butcout<<x++<<‘ ‘<<x; will print out 4 5. • Similarly, if x=5 and y=3 thencout<<x<<‘ ‘<<y<‘ ‘<<++x*y--<<‘ ‘<<x<<‘ ‘<<y will print5 3 18 6 2 to the screen

  3. Shortcut assignment and arithmetic operators We can write x+=3; to mean x=x+3, similarly we can write x*=4; x/=5; x-=2; x%=3;if x,y are integer variables, x/y is truncated, i.e., 7/3 is 2. If at least the numerator or denominator of x/y is a double or a float, then the result is an untruncated double or float. 7.0/3 is 2.333 We can also writedouble(7)/3 or 7/double(3) to get the same result, but not double(7/3), since this gives 2.0. If x,y are integer variables, x%y gives the remainder when x is divided by y. 7%3 is 1. 13%5 is 3. -13%5 is –3. In math, -13%5 is 2.

  4. The while loop • If i is an integer variable containing 0, then the statementwhile(i<5) {cout<<i*i<<‘ ‘;i++;}will print 0 1 4 9 to the screen. The pair of statements cout<<i*i<<‘ ‘; i++; gets repeated 5 times, each time with a different value of i. The five values of i are 0,1,2,3,4. Before the pair of statements is executed, the test i<5 is performed. If it is true, then both statements are executed. If not(as when 5<5 is false), then the next statement after the while loop is executed.

  5. while loop program • While loops are usually used when it is not known in advance how many times the loop will be executed. The following program #include<iostream.h>main( ){double sum=0;int i=1;while(1.0/(2*i*i+2*i+1)>.0001){sum=sum+1.0/(2*i*i+2*i+1);i++}cout<<sum<<endl;}will execute 70 times and print .433617 to the screen. We have avoided solving the equation 2i2 + 2i +1 = 10000 to find out in advance how many times we must loop.

  6. for loops There are three parts to the for loop statement.for(int i=1;i<5;i++)cout<<i<<‘ ‘;int i=1; is called the initialization command, and is done first and only once. If there are several commands before the first ; then they must be separated by commas. i<5 is called the test expression. If it is true then the body of the for loop, in this case, the statement cout<<i<<‘ ‘; is performed. If the body is performed then the final statement(s) is(are) carried out after the body. Then the test expression is evaluated again. The above for statement will print 1 2 3 4 to the screen. The above for statement is equivalent tofor(int i=1;i<5;cout<<i<<‘ ‘,i++){} and also tofor(int i=1;i<5;){cout<<i<<‘ ‘;i++;} and also to for(int i=1; ;){if(i<5)cout<<i<<‘ ‘;else break;}

  7. break and continue If a break statement occurs in the body of a loop, the loop is exited. If a continue statement occurs in the body of a loop, the next iteration is performed. If the loop is a for loop then after the continue, the update portion of the for loop is executed.

  8. Quiz 2 and solution Write a program which will ask the user to input 50 numbers(doubles), and print the average to a file called average.txt#include<fstream.h>main(){cout<<“Please enter 50 numbers.\n”;double x, sum=0;for(int i=0;i<50;i++){cin>>x;sum=sum+x;}ofstream fout(“average.txt”);fout<<sum/50;fout.close();

  9. loops with a conditional Suppose we want to read 50 integers from the file input.txt and write the even integers to the file oute.txt and the odd integers to the file outo.txt.#include<fstream.h>main(){ifstream fin(“input.txt”);ofstream foute(“oute.txt”),fouto(“outo.txt”);int x;for(int i=0;i<50;i++){fin>>x;if(x%2==1)fouto<<x<<‘ ‘;else foute<<x<<‘ ‘;}fin.close();fouto.close();foute.close();}

  10. Quiz 3 and solution Write a program which will ask the user to input 50 numbers(doubles), and print the numbers to three different files. If the number is divisible by 3, the number is printed to the file three0.txt; if dividing the number by 3 yields a remainder of 1, the number is printed to the file three1.txt; if dividing the number by 3 yields a remainder of 2, the number is printed to the file three2.txt #include<fstream.h> main(){int x; ofstream out0(“three0.txt”),fout1(“three1.txt”),fout2(“three2.txt”); cout<<“Please enter 50 integrs.\n”; for(int i=0;i<50;i++){cin>>x; if(x%3==0)fout0<<x<<‘ ‘; else if(x%3==1)fout1<<x<<‘ ‘; else fout2<<x<<‘ ‘;} fout1.close(); fout1.close(); fout2.close();}

More Related