Download presentation
Presentation is loading. Please wait.
1
1 Arithmetic in C
2
2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15; int b = 4; float half; half = a / b; // 3 will be stored in half Q: How to do real division?
3
3 Increment / Decrement Operators: ++ Increment operator: int a = 5; a = a + 1; can be written as: a++; or ++a; -- Decrement Operator: a = a -1; can be written as: a--; or --a;
4
4 What is a difference? ++a; a++; Makes no difference if they stand alone! Makes a difference if used in an expression! Ex: Given int a, b; a = b = 5; a = 3 * b++; Is equivalent to: a = 3 * b; b++; Outcome: /
5
5 Cont… Ex2: a = b = 5; a = 3 * ++b; Is equivalent to: b++; a = 3 * b; Outcome:
6
6 Compound Assignment Operators: Assume: int a = 6; Increment a by 2: a = a + 2; Using combined assignment operator: +=: a += 2; Multiply a by 6: a = a * 6; Using combined assignment operator: *=: a *= 6;
7
7 Cont… -= a -= b; means: a = a – b; *= a *= 5; means: a = a * 5; /= b /= a; means: b = b / a; %= a %= 2; means: a = a % 2; a *= (b + c); means: a = a * (b + c);
8
8 3 major code blocks: There are 3 main structures / blocks in high level languages: Sequential What we had so far Selection Making decisions Repetition Loops
9
9 Selection: Program execution branches to different lines of code based on some condition. Selection is implemented in C by: if statement switch statement
10
10 If Statement: One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition? true Statement(s) Rest of the program false
11
11 Forming conditions in C: Conditions are formed by comparing 2 things. You should compare things of similar data type.
12
12 Relational Operators: Relational operators are binary operators, i.e. they operate on 2 things. < less than 5 < 9 <= less than or equal a <= 70 > greater than >= greater than or equal == equal to score == 100 != not equal to
13
13 Relational Expressions (Conditions): Are any meaningful combination of literals, variables, and relational operators, and arithmetic operators. Ex: int a = 3, b = 5; 1) a >= 3 2) b == a 3) 12 > 9 4) a >= b 5) a + b * 2 > 10 // true
14
14 Precedence Rule: (..)highest Arithmetic operators Relational Operatorslowest
15
15 Compound statement: Ex1: int n = … if (n > 10) printf(“………..”); Ex2: if (n < 10) { printf (“ ……….”); printf (“………...”); } Single statement Compound statement
16
16 Ex1: check the score, and display a message … int score; printf (“ Enter test score:”); scanf(“%d”, &score); if (score >= 60) { printf (“ You passed the test \n”); printf (“ Good Job! “); } printf (“ Your score is: %d”, score); Q1: What is the output if input is 55? Q2: What is the output for score = 88
17
17 If / else statement: Is referred to as “Closed Branch” in the book. Syntax:Flowchart: if (condition) statement(s); else statement(s); Condition? Statement(s) Rest of the program falsetrue
18
18 Ex1: Get the user’s age and tell the user if s/he can vote or not. Voting age is 18. int age; printf (“Enter your age: “); scanf (“%d”, &age); if (age >= 18) printf (“ You can vote ”); else printf (“You are too young to vote.”);
19
19 Exercise: Write a segment of code that checks the test score. If it is >= 60, display you passed, otherwise, display you failed. int grade; printf(“ Enter the grade: “); scanf(.. )
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.