notes4 - Control Statements:- If statement (same as Java) o

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Control Statements:- If statement (same as Java) o if(..expression..){ ..... ..... } o Expression in if statement is converted to int. o if(expression) != 0 then expression is true and the body of the statement is executed. o if(expression) == 0 then it is false and the body is not executed. Then continuous to check against else if or else statements are used to then be executed. o Example: int i; if(i != 0){ // do something } equivalent to: if(i){ // do something }- While statement (same as Java) o while(..expression..){ ..... ..... } o Expression in while statement is converted to an int. o while(expression) != 0 then the program continuous to loop until expression does equal 0. o Example: int i, j; while(i > j){ .... // code body that will continue to executed // until j > i }- For statement o "for" statement is typically used in situation where you know in advance the number of iterations. o Syntax: for(expression1; expression2; expression3){ .... } o Example: //Assuming the variable i has been declared above. for(i = 0; i < 10; i++){ ......... // code to be executed goes here // this specific for statement will loop until // !(i < 10) for a total of 10 iterations } o However you could use the for statement where a while statement is also used. Example: expr1; while(expr2){ // body of statement to be executed expr3; } expr1 and expr3 are usually 'coma expressions' expr1 expr2 expr3 for(i = 0, j = 0; i < 10; i++){...
View Full Document

This note was uploaded on 02/18/2012 for the course CS 240 taught by Professor Rego during the Fall '08 term at Purdue University.

Page1 / 5

notes4 - Control Statements:- If statement (same as Java) o

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online