notes6 - Control Statements if statement(same as Java...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Control Statements if statement (same as Java) if(expr){ .... }else{ //else body } (expr) in the if statement is converted to int. If (expr) != 0, then (expr) is true and the body of the if is executed. Otherwise, if (expr) == 0, the (expr) is false and the body is not executed. If "else" is used then the else body is executed. int i; ... if(i != 0){ ... } equivalent to if(i){ ... } while statement while (expr){ ...//body } execute body while (expr) is true Example while ( i < J ){ ...//body }execute body while i < J for statement "for" statement is typically used in situations when you know in advance the number of iterations for( expr1; expr2; expr3 ){ //body } Example int i;
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
for( i = 0; i < 10; i++) { //body } However you can use the for where a while is also used. We can write a for loop as a while in the following way. expr1; while(expr2){ //body expr3; } Expr1 and expr3 are usully "coma expressions" for( i = 0; J = 0; i < 10; j++, J--){ //body } Forever Loops while(1){ //body → runs forever } for( ; ; ){ //body → runs forever } switch statement switch(expr){ case const 1; . . . break; case const 2; . . . break; . . default; . . break;
Image of page 2
} expr is evaluated and converted to an int if "expr" is equal to any of the const 1, .. const n then it will jump to that section.
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern