Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditionals – if - else Dr. Sajib Datta

Similar presentations


Presentation on theme: "Conditionals – if - else Dr. Sajib Datta"— Presentation transcript:

1 Conditionals – if - else Dr. Sajib Datta CSE@UTA

2 Fundamental Operators A new operator used in C is modulus operator: %. % only used for integers, not floating-point Return the integer remainder from integer division. ▫Example: int topnum = 7, bottomnum = 3; int answer; answer = topnum % bottomnum; //answer has a value of 1.

3 Assignment Operators The following assignment operators are available in C: ▫+= addition ▫-= subtraction ▫*= multiplication ▫/= division num = num + 2 OR num+=2; num = num - 1 OR num-=1;

4 Relational Operators < : is less than <= : is less than or equal to == : is equal to >= : is greater than or equal to > : is greater than != : is not equal to Example: ◦Relational expression: a > 10 If the relation is true, the value of the expression is 1, otherwise 0.

5 Expressions A combination of operators and operands, where operands can be constants, variables, or combinations of the two ▫4 ▫4+21 ▫a = (b+c)/2 ▫q>4 Every expression has a value in C “=” – the value of the left side ▫the value of a = 2 is 2 “>” – relational operator ▫has value 1if true, and 0 if false

6 Difference between Expressions and Statements A statement is a complete instruction to the computer In C, it’s indicated by semicolon A complete instruction is not necessarily a statement ▫E.g., x = 5 + (y = 4); Different from statements ▫y = 4 // expression ▫y = 4; // statement (assignment statement)

7 Increment and Decrement Operators ++ and -- The operand must be a variable Two varieties – prefix and postfix a ++, b -- ++ a, --b This operation increases(decreases) the value of its operand by just one! Case1: ▫a = 3; ▫b = 2* ++a;// the value of the expression ++a is the new value of a, therefore b = 8 Case2: ▫a = 3; ▫b = 2* a++;// the value of the expression a++ is the old value of a, therefore, b = 6

8 If Statements if statement gives you choice of either executing a statement or skipping it The basic format of the if statement is if (condition_is_true) do_something; // condition_is_true should be an expression //need the parenthesis for the expression // If expression evaluates to true (nonzero), do something. Otherwise, it is skipped. Normally, expression is relational expression. But in general, any expression will work since it has a value, which can be mapped to true or false. ▫Examples int x; int y = 6; scanf (“%d”, &x); if (x >= 2) y = 10; printf(“y is %d.\n”, y);

9 Indent Style in C Programming indent style - a convention governing the indentation of blocks of code to convey the program's structure Visual Studio manages indent automatically by default Manually, “Tab” key on the keyboard will help

10 If Statements cont. Handle more than one statements when the condition is true Create a block of statements by using braces. ▫Example if (x >= 2) { y = 10; printf("y is now %d", y); } next statement; // with one statement, you can use “{}”, but it’s not necessary. // without the braces what’s going to happen here?

11 If and else Statements To do one thing if a condition is true but another if the condition is false - if-else statement: Basic format of if-else statement if (expression_is_true) do_something (if statement); else do_something_else (else statement); Example: int x = 10; if(x>=2) printf("x>=2\n"); else printf("x<2\n"); your next statement;


Download ppt "Conditionals – if - else Dr. Sajib Datta"

Similar presentations


Ads by Google