Upload
ian-kevin
View
54
Download
3
Embed Size (px)
Citation preview
Logika PemrogramanLogika PemrogramanLogika PemrogramanLogika Pemrograman
Programming in C - Programming in C - 22Dr. Heru Santoso BRDr. Heru Santoso BR
Hello, World1. /* Hello, world program */2.#include <stdio.h>3. 4. int main()5. {6. printf("hello, ");7. printf("world\n");8. return 0;9. }
Basic Programming Concepts
• Comment To explain what the program is for and why it is the way it is.
• Preprocessing Done before the program is compiled.• To include header files containing
information about C libraries• Statement A line of code that tells
computer to perform some action. Always ended with a semicolon(;).• Invoking functions
• Function A module often consisting of a set of statements.• Two functions in the hello world
program: main, printf
1. /* Hello, world program */
2. #include <stdio.h>3. 4. int main()5. {6. printf("hello, ");7. printf("world\n");8. return 0;9. }
Programming Concepts
• /* Hello, world program */
• #include <stdio.h>• • int main()• {• printf("hello, ");• printf("world\n");• return 0;• }
Statements : • calling function printf(“hello,”);
printf(“world\n”);
Nama fungsi
argument
Should be ended with
“;”
Nama fungsi
argument
Escape sequence
In Class Exercise 1-1
• Escape Sequences are used to control printf to do something other than printing characters.
• Modify the hello world program to try out various escape sequences like:• \n: Newline. Position cursor at the
beginning of the next line.• \t: Tab. Move cursor to the next tab stop.• \a: Alert. Sound the system bell.
Arithmetic Operators• To form expressions
• Many statements are merely expressions.
• Normal order of operations (+, - ; *,/) is followed. Parentheses can be used.
• 4+3*2 = 4+9=13• (4+3)*2=14
+ Addition
- Subtraction
* Multiplication
/ Division
%Modulus (remainder)
++ Increment
-- Decrement
Arithmetic Operators: An Example
1. /* Arithmetic operators */2. #include <stdio.h>3. 4. int main()5. {6. printf("7+3=%d\n",7+3);7. printf("7-3=%d\n",7-3);8. printf("7*3=%d\n",7*3);9. printf("7/3=%d\n",7/3);10. printf("7%%3=%d\n",7%3);11. printf("7.0/3.0=%f\n",7.0/3.0);12. return 0;13.}
Variables
• Variable Name for a memory object. Variable names must start with letters and contain letters, digits, and underscores. • a, b, i, j, counter, number_of_points, c1234, …
• Type What the variable represents. Could be of integer, floating point number, character, string, and many others.• int, float, double, char, …
• Declaration Tells compiler about variables and their type. • int i,j;• float sum;
Numeric Typesint integer
float floating point
charcharater (a very shot integer)
short or short int
short integer
long or long int
long integer
double long floating point
long doublevery long floating point
Reading Inputs1. #include <stdio.h>2. int main()3. {4. float a,b;5. printf("Please input the first number:\n");6. scanf("%f",&a);7. printf("Please input the second number:\n");8. scanf("%f",&b);
9. printf("a+b=%f\n",a+b);10. printf("a-b=%f\n",a-b);11. printf("a*b=%f\n",a*b);12. printf("a/b=%f\n",a/b);13.}
Assignment1. /* Arithmetic operators */2. #include <stdio.h>3. 4. int main()5. {6. int a,c;7. int b=4;8. a=3;9. c=a+b;10. printf(“Sum: %d + %d -> %d\n”,a,b,c);11. a++;b--;12. prinf(“Now a=%d and b=%d\n”,a,b);13. return 0;14.}
In-Class Exercise 1-2
• Write a program to calculate the average of three floating point numbers, namely 2.3, 3.4, 4.5.• Using scanf function• Using assignment