Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop

Similar presentations


Presentation on theme: "CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop"— Presentation transcript:

1 CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Dr. Sajib Datta

2 Modulus A new operator used in C is modulus operator: %
% only used for integers, not floating-point Gives the integer remainder from integer division. Example: int a = 7, b = 3; int answer; answer = a % b;

3 More… The following assignment operators are available in C:  += addition  -= subtraction  *= multiplication  /= division  num = num + 2 same as num+=2;  num = num - 1 same as num-=1;

4 What is the output of ‘x’?
int x; x = 10; x += x; printf(“%d”, x);

5 Operator Precedence What is the value of x?
A precedence for each operator Multiplication and division have a higher precedence than addition and subtraction For operators with the same precedence, if they share an operand, they are executed according to the order they occur in the statement. (In most cases, from the left to the right, except assignment operation) Examples: int num, x; num = 2; x = 1 + 3*num/4; What is the value of x?

6 Operator Precedence Want an addition operation to take place before division? int num, x; num = 2; x = (1 + 3)*num/4; What is x now? int a = 1, b = 4; b += a+2; is equivalent to b = b + (a + 2)

7 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.

8 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

9 Statement vs. Expression
A statement is a complete instruction to the computer In C, it is indicated by semicolon An statement is consists of expressions

10 Increment and Decrement Operator
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! In the prefix form, the increment or decrement takes place before the value is used in expression evaluation so the value of the expression is different from the value of the operand. In the postfix form, the increment or decrement takes place after the value is used in expression evaluation so the value of the expression is the same as the value of the operand

11 Increment and Decrement Operator
Case1: a = 3; b = 2* ++a;// the value of the expression ++a is the new value of a, therefore b = ? Case2: b = 2* a++;// the value of the expression a++ is the old value of a, therefore, b = ?

12 Conditionals

13 if condition 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);

14 if… Examples int x; int y = 6; scanf (“%d”, &x); if (x >= 2)
printf(“y is %d.\n”, y);

15 if… 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?

16 if…else… 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);

17 if…else… Example: int x = 10; if(x>=2) printf("x>=2\n"); else
your next statement;

18 if…else… else statement must have an if statement to match
Not allowed to have any statement between if statement and else statement (except the cases of nested if-else) Example: (a wrong program!) int x = 4; int y; if (x >= 2) y = 10; printf("y is now 10.\n"); else printf("y is not assigned.\n");

19 if…else… else statement can also be a block of statements, but remember to give “{ }” Example: if(x >= 2) { y = 10; printf("y is now 10.\n"); } else y = 20; printf("y is now 20.\n");

20 Nested if… Have a second conditional statement in when the first
condition is true if statements can be nested Example : if ( x < 10 ) if( y < 3 ) z = x + y; your next statement comes here; To assign a value of (x+y) to z only if x > 10 and y < 3

21 Nested if and else /* indentation is used to show correct logic; the else goes with the nearest unmatched f if statement within a block , where block is defined by using braces.*/ Examples: (nested in if statement) (1) if (x == 13) if (y == 52) printf("Test1.\n"); else printf("Test2.\n"); printf("Test3.\n"); (2) if (x == 13) if (y== 52) printf("Test3.\n"); Let’s say x=13, y=53…what are the outputs in 3 cases (3) if (x == 13)

22 Nested if and else (4) if (x == 13) { if (y == 52) printf("Test1.\n");
} printf("Test3.\n"); (5) if (x == 13) if (y== 52) (1)(2)(3)(4) are the same, but not (5) (2) and (3) are not good program styles, you should use indent to convey your code block, like what (1) does, or use the braces like the (4) example.

23 Nested if and else Examples: (nested in else statement)
(1) if (x == 13) printf(“x == 13.\n”, x); else if (x < 13) printf(“x < 13.\n”); else printf(“x > 13.\n”); (2) if (x == 13) if (x < 13) (1) and (2) are the same.

24 if/else Any expression that evaluates to a nonzero value is considered true. Examples: if (-3.5) printf("non-zero values are true\n"); // this will be printed. else printf("this never prints\n"); if (0) printf("zero is false\n"); printf("this is always false\n"); // this will be printed. WARNING: “=” and “==” ◦ Example:  if (a = 2) printf("a is equal to 2 forever.\n"); // this will be printed. printf("This statement will never be executed.\n");

25 Logical operator && ---------- and
Exp1 && exp2 is true only if both exp1 and exp2 are true || or Exp1 || exp2 is true if either exp1 or exp2 is true or if both are true ! not ! Exp1 is true if exp1 is false, and it’s false if exp1 is true

26 Logical operator Examples: Operator precedence:
4<3 && 2< true/false? 5!=5 || 4< true/false? !(x<9) assume x = 8, true/false? Operator precedence: relational operation have higher precedence over logical operation, except the logical operation “!” parentheses have the highest precedence

27 Logical operator Example: if ( x < 10 ) if( y < 3 ) z = x + y;
your next statement comes here; the same as if (x < 10 && y < 3)

28 Logical operator To test two condition expressions, you have to use a logical operator to connect them. #include <stdio.h> int main(void) { int x; scanf("%d",&x); if(3<x<6) // wrong!!!, to correct ( 3<x && x<6) printf("if statement\n"); else printf("else statment\n"); return 0; } Test with x = 7

29 Switch statement An if-else statement is used for binary decisions–those with two choices, while switch statement is intended for more than two choices. switch (expression) { case label1: do statements1// there is a space between case and label case label2: do statements2 case labeln: do statementsn default: do default statements (optional) } expression should be an integer value (including type char). Labels must be constants (integer constants or char constants).

30 Switch statement The program scans the list of labels until it finds one matching that value. If there is no matching, while there is “default” key word, the statements associated with “default” will be executed. If there is no matching, and there is no “default” either, the program will jump out of switch statement. The statement after switch statement will be executed.

31 break in Switch statement
If a matching is found, the corresponding statement, and all following labels are executed until flow reaches end of switch int x; scanf("%d", &x); switch(x) { case 1: printf("freshman\n"); case 2: printf("sophomore\n"); case 3: printf("junior\n"); case 4: printf("senior\n"); default: printf("graduates\n"); } printf("out of switch now.\n"); What happens when x=1? All statements will be executed If x=4, it will print senior graduates

32 break in Switch statement
When you see “break”, the program will jump out of the switch statement when reaching the break. int x; scanf("%d", &x); switch(x) { case 1: printf("freshman\n"); break; case 2: printf("sophomore\n"); case 3: printf("junior\n"); case 4: printf("senior\n"); default: printf("graduates\n"); } printf("out of switch now.\n"); What happens with x=1? x=4?

33 Empty Case (Switch) int x; scanf("%d", &x); switch(x) { case 1:
case 2: printf("sophomore\n"); case 3: printf("junior\n"); break; case 4: printf("senior\n"); default: printf("graduates\n"); } printf("out of switch now.\n"); What happens with x=1? x=4?

34 Case with multiple statements
int x; scanf("%d", &x); switch(x) { case 1: printf("freshman\n"); printf(“redundant freshman\n"); case 2: printf("sophomore\n"); case 3: printf("junior\n"); break; case 4: printf("senior\n"); default: printf("graduates\n"); } printf("out of switch now.\n"); What happens with x=1? x=4?

35 If/else <-> switch
if(a == 1) printf(“a = 1”); else { if(b == 3) printf(“b = 3”); }

36 If/else <-> switch
if(a == 3 || b == 1) printf(“if is true”); else printf(“else is true”);

37 If/else <-> switch
switch(n) { case 1: case -1: printf(“UTA”); break; case 50: case 75: case 100: printf(“CSE”); case 120: switch(m) case 50: printf(“1320”); default: break; } default: printf(“sec 001”);

38 char data type char type technically is an integer type
Computer uses numeric codes to represent characters, and store characters as integers The mostly commonly used code in the U.S. is the ASCII code

39 char data type A char variable takes 8-bit unit of memory (1 byte), which can be verified by sizeof() C character constant: a single letter contained between single quotes Example: char mych = 'a'; printf("%d", sizeof(char));

40

41 Compare char values char ch; scanf(“%c”, &ch);
printf(“%c”, ch); // will print the character printf(“%d”, ch); //will print?

42 Compare char values

43 Input/output Char char letter1 = 'A'; char letter2 = 65;
printf("print the ASCII for 'A' - %d", letter1);// 65 will be printed printf("print the char value for ‘A' - %c", letter2); // A will be printed scanf("%c", &letter);// must read a character, even the input is a digit, it will be regarded as a character scanf("%d", &letter);// fail – type must match Not good programming to mix integer and char value, because it needs remembering ASCII for characters.

44 Non-printing characters
Characters which can not be printed directly Rather, some represent some actions such as backspacing or going to the next line or making the terminal bell ring.

45 Non-printing characters

46 while loop The basic form of the while loop is
while(test_condition) do_something; As long as test is true, the loop will repeat. test_condition should be an expression. If it is nonzero, it’s a true condition, and do something. If it is zero, the while statement will be skipped. Give the test_condition a chance to change in do_something, otherise, the loop will run for ever In general, (1) define a variable outside while loop and (2) use it to compose the test_condition, and then (3) change the variable in do_something

47 Example Input: 17 Output: 0 2 4 6 8 10 12 14 16
#include <stdio.h> int main(void) { int upbound; int i = 0; scanf("%d",&upbound); while(i <= upbound) if (i % 2 == 0) printf("%d ", i); i++; } printf("\n"); return 0;

48 Example Print out a triangle. * ** *** **** How do you solve this?


Download ppt "CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop"

Similar presentations


Ads by Google