Download presentation
Presentation is loading. Please wait.
Published byCecil Banks Modified over 8 years ago
2
Real numbers representation - Floating Point Notation First C Program Variables Declaration Data Types in C ◦ char, short, int, long, float, double, unsigned int, etc Magic sizeof() operator prinft/scanf conversion codes Char is a number
4
int k = 5; int *p = NULL; p = &k; *p = 4; scanf(“%d”, &k);
5
Selects statements to execute based on the value of an expression ◦ The expression is sometimes called the controlling expression Selection statements: ◦ if statement ◦ switch statement
6
used to execute conditionally a statement or block of code. if (expression) statement If expression is true, statement is executed (what is true?). statement can be replaced by a block of statements, enclosed in curly braces.
7
/* This program displays the absolute value of a number given by the user */ #include int main() { double num; printf("Please enter a real number: "); scanf("%lf", &num); if (num<0) num = -num; printf("The absolute value is %g\n", num); return 0; }
8
if (expression) statement 1 else statement 2 if expression is true, statement 1 is executed. if expression is false, statement 2 is executed both statements can be (and very often are) replaced by blocks of statements (“compound statements”)
9
int first, second, min; /* … */ if (first < second) { min = first; printf ("The first number is smaller than the second.\n"); } else { min = second; printf ("The second number is smaller than the first\n"); } printf("The smaller number is equal to %d\n", min);
10
In C, every expression has a numeric value An expression is ‘true’ when its value is non-zero If it is zero, it is false Therefore, in the following – if (expression) statement statement is executed if expression is non zero.
11
A < B evaluates to zero if A is larger than or equal to B, and some non-zero value if A is smaller than B Value of A+B is the sum of A and B Assignment Operator A = B
12
#include int main() { int i = 3; printf("i = %d\n", i); printf("(i==7) = %d\n", i==7); printf("i = %d\n", i); printf("(i=7) = %d\n", i=7); printf("i = %d\n", i); }
13
They are: ◦ A == B (Note the difference from A = B!!!!!) ◦ A != B ◦ A < B ◦ A > B ◦ A <= B ◦ A >= B The value of the expression is non-zero if it’s true, zero if it’s false
14
int a, b; printf("Enter two numbers\n"); scanf("%d%d", &a, &b); if (a == b) { printf("The numbers equal %d\n", a); printf("The expression a == b is %d\n", a == b); } else { printf("The numbers are not equal\n"); printf("The expression a == b is %d\n", a == b); }
15
Allows to evaluate two or more expressions ◦ !A – ‘not’ - True when A is not, and vice versa. ◦ A && B – ‘and’ - True when both A and B are true ◦ A || B – ‘or’ (inclusive or) - True when either A or B (or both) are true
16
#include int main() { int grade; printf("Please enter your grade: "); scanf("%d", &grade); if (grade 100) { printf(“Invalid grade!\n"); return 1; } else printf("This is a grade.\n"); return 0; }
17
if (expression) statement else if (expression) statement else if (expression) statement else statement
18
if (grade >= 90) printf ("A\n"); else if (grade >= 80) printf ("B\n"); else if (grade >= 70) printf ("C\n"); else if (grade >= 60) printf ("D\n"); else printf ("F\n");
19
Input ◦ Two integers, A and B Output ◦ Their relation (i.e. displays A==B, A B)
20
#include int main() { int A, B; printf("Enter two Numbers\n"); scanf("%d%d", &A, &B); if (A == B) printf("A==B\n"); else if (A > B) printf("A>B\n"); else printf("A<B\n"); return 0; }
21
(expr1)? expr2 : expr3 If expr1 is true (non-zero), expr2 is evaluated. Otherwise, expr3 is evaluated
22
#include int main() { int i, j, min; printf("Please enter two numbers: "); scanf("%d%d", &i, &j); min = (i < j)? i : j; printf("The minimum between %d and %d is %d\n", i, j, min); return 0; }
23
switch (expression) { case const-expr: statements case const-expr: statements … default: statements }
24
expression must have an integer value when the switch statement is executed: ◦ the expression is evaluated ◦ if a case matches the value of the expression, the program jumps to the first statement after that case label ◦ otherwise, the default case is selected ◦ the default is optional
25
switch (grade/10) { case 10: case 9: printf ("A\n"); break; case 8: printf ("B\n"); break; case 7: printf ("C\n"); break; case 6: printf ("D\n"); break; default: printf ("F\n"); }
26
#include int main( ) { double n1, n2, res; char op; printf("Please enter two numbers: "); scanf("%lf%lf", &n1, &n2); printf("Please enter an arithmetical operator (+, -, * or /): "); scanf(" %c", &op);
27
switch(op) { case '+': res = n1+n2; break; case '-': res = n1-n2; break; case '*': res = n1*n2; break; case '/': /* We're not checking for division by zero for clarity... */ res = n1/n2; break; default: printf("%c is an invalid arithmetical operator!\n", op); return 1; } /* Display the expression and its result */ printf("%g %c %g = %g\n", n1, op, n2, res); return 0; }
28
Write a program that accepts a number between 1 and 100 from the user. If there is a coin of that value in cents, it should display its name. Otherwise, it should report that there is no such coin 1 = cent, 5 = nickel, 10 = dime, 25 = quarter, 100 = dollar Remember to check for the validity of the input!
29
/#include int main() { int num; printf("Please enter a number from 1 to 100: "); scanf("%d", &num); /* Make sure the input is valid */ if (num 100) { printf("Invalid input!\n"); return 1; } /* Display the correct coin name, or a default message if there's no such coin */ switch (num) { case 1: printf("It's a cent!\n"); break; case 5: printf("It's a nickel!\n"); break; case 10: printf("It's a dime!\n"); break; case 25: printf("It's a quarter!\n"); break; case 100: printf("It's a whole dollar!\n"); break; default: printf("It's not a coin!\n"); } return 0; }
30
i++ or ++I== i = i + 1 i-- or --i == i = i – 1 i += a == i = i + a i -= a == i = i - a i *= a == i = i * a i /= a == i = i / a
31
for, while, do-while loops.
32
while ( condition ) { statement(s); }
33
#include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); i=1; /* i is the counter */ while (i<=n) { fact = fact*i; i++; /* increment i */ } printf("the factorial is %d\n", fact); return 0; }
34
fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; }
35
for (c = begin; c <= end; c += inc) { loop body }
36
#include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i=1; i<=n; ++i) { fact *= i; } printf("the factorial is %d\n", fact); return 0; }
37
Depends on nature of the application for – repeat operation certain number of times while – repeat operation until some condition is true
38
/* Print a rectangle of #. The height and width are defined by the user */ #include int main( ) { int i, j; int height, width; printf("Please enter the two box dimensions: \n"); scanf("%d%d", &height, &width); for (i = 1; i <= height; i++) { for (j = 1; j <= width; j++) printf(“#"); printf("\n"); }
39
Write a program that prints an upside-down half triangle of *. The height of the pyramid is the input. ***** *** ** **** *
40
#include int main() { int i, j, size; printf(“Please enter a size:\n”); scanf(“%d”,&size); for (i = 1; i <= size; i++) { for (j = i; j <= size; j++) printf("*"); printf("\n"); } return 0; }
41
#include int main() { int i, j, last; printf("enter a number\n"); scanf("%d", &last); for (i = 2; i <= last; i++) { for (j = 2 ; j < i; j++) { if (i % j == 0) break; } if (j == i) printf("the number %d is prime\n", i); } return 0; }
42
#include int main() { int i; printf("Please enter a positive number.\n"); do { scanf("%d", &i); if (i <= 0) printf("Try again.\n"); } while (i<=0); /* The program continues.... */ return 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.