Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS140: Intro to CS An Overview of Programming in C by Erin Chambers.

Similar presentations


Presentation on theme: "CS140: Intro to CS An Overview of Programming in C by Erin Chambers."— Presentation transcript:

1 CS140: Intro to CS An Overview of Programming in C by Erin Chambers

2 Getting started ● Please log in to the computer, click on the startup menu (located in bottom left corner) ● Select “Utilities” -> “Kate” to open a text editor

3 A First Program #include main(void) { printf(“Hello, World!\n”); return 0; }

4 First Program – Line by line <-- Loads standard input/output options #include main(void) { printf(“Hello, World!\n”) return 0; } <-- starts the program: void means that there is no return value or input parameters <-- prints a string, which is in between quotation marks <-- marks end of program

5 Running our program ● We need to compile our C program using the compiler called cc ● The compiler then outputs an executable file which will run our program, usually called a.out ● To run it, open up a Konsole (the little black screen icon at the bottom), type “cc filename.c”, then type “./a.out” at a command prompt

6 Remember data types? ● Numbers: int, float ● Strings: str ● Characters: char ● Booleans: bool

7 Other data types: a simple example #include main(void) { int x, y, divide, remainder; x = 10; y = 13; divide = x / y; remainder = x % y; printf(“The quotient is %d, and the remainder is %d \n”, divide, remainder); return 0; }

8 Printing the value of a variable ● %d – tells the compiler that we will be printing an integer in this spot, which will appear after the comma ● Other possibilities – %c for a character – %f for a floating point number

9 Floats #include main(void) { float x, y, divide; x = 10; y = 13; divide = x / y; printf(“%f divided by %f is %f”, x, y, divide); return 0; }

10 Input #include main(void) { int x, y, divide, remainder; printf(“Enter two integers: “) scanf(“%d%d”, &x, &y); divide = x / y; remainder = x % y; printf(“The quotient is %d, and the remainder is %d \n”, divide, remainder); return 0; }

11 If statements ● For more control, might want to specify some code be executed if some condition is true ● Use the if-else statement and boolean expressions ● Boolean expressions in C:, =, ==, != ● ! for 'not', && for 'and', || for 'or' ● Syntax: if (boolean expression) { statements } else { statements }

12 If statements – an example #include main(void) { int x, y, z; printf(“Enter two integers: \n”); scanf(“%d%d”, &x, &y); if (x > y) max = x; else max = y; printf(“The maximum is %d \n”, max); return 0; }

13 More complicated if statements if (x > y) if (x > z) max = x; else max = z; else if (y > z) max = y; else max = z;

14 Loops ● While loops are a way of performing the same code multiple times ● While the boolean expression evaluates to true, the code in the while loop will be performed; as soon as it is false, the while loop ends and the program continues

15 While loop - example #include main(void) { int i = 10; while (i >0) { printf(“T minus %d and counting\n”, i); i = i – 1; }

16 Another while loop #include main(void) { int number, sum; printf(“Enter a number:”); scanf(“%d”, &number); while (number != 0) { sum = sum + number; printf(“Enter another number, or 0 to stop:”); } printf(“The sum is %d\n”, sum); return 0; }

17 Comments in a program ● In C, any line beginning with // will be ignored by the compiler. ● Since code can be difficult to decipher, this provides a great way to comment your code, or describe what each line does.

18 A program with comments // Written by Erin Chambers #include main(void) { //print my first message printf(“Hello, World!\n”); return 0; }


Download ppt "CS140: Intro to CS An Overview of Programming in C by Erin Chambers."

Similar presentations


Ads by Google