Download presentation
Presentation is loading. Please wait.
Published byJohnathan Watts Modified over 9 years ago
1
CH Programming An introduction to programming concepts
2
Outline Programming Languages Variables Assignment Statements Arithmetic Expressions Input Statements Output Statements Conditional Statements Loops Command vs Program mode
3
Programming Languages A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. Programming languages, like human languages, are defined through the use of syntactic, grammatical and semantic rules, to determine structure and meaning respectively. Function A programming language is a language used to write computer programs, which involve a computer performing some kind of computation or algorithm and possibly control external devices such as printers, robots, and so on. Compiled vs. Interpreted
4
Outline Programming Languages Variables Assignment Statements Arithmetic Expressions Input Statements Output Statements Conditional Statements Loops Command vs Program mode
5
Variables The Tupperware of Programming Variables are like saved documents on your computer, each one storing information. When you name a variable you are actually declaring or defining it. type varName1; -> int age; type varName1, varName2; -> int age, weight; type varName1 = value; -> float ratio = 0.87; In CH, for example, variables can hold letters (string_t), and numbers (int, double, and float) values. If you want to access the information that a variable has stored in it, all you have to do is call the variable's name. cout << ratio; 0.87 Make sure the names are descriptive.
6
Outline Programming Languages Variables Assignment Statements Arithmetic Expressions Input Statements Output Statements Conditional Statements Loops Command vs Program mode
7
Assignment Statements Assignment statements are used to assign a value to a variable. variable-name = expression; Imagine we have defined the following variables: string_t name, lastName, letterGrade; int average, test1, test2, finalExam; Lets assign them some values: name = "Mary"; lastName = "Smith"; test1 = 98; test2 = 78; finalExam = 90; average = (test1 + test2 + finalExam)/3; /* (98+78+90)/3 = 88.66 */ letterGrade = "B+";
8
Outline Programming Languages Variables Assignment Statements Arithmetic Expressions Input Statements Output Statements Conditional Statements Loops Command vs Program mode
9
Arithmetic Expressions Just as in algebra, arithmetic expressions are evaluated from left to right with multiplication (*) and division (/) being evaluated before addition (+) and subtraction (-). The operator modulo (%) returns the reminder of a division, e.i. 5 % 2 = 1 Use parentheses to overrule this order or to clarify the expression. For instance, grade = (98 + test2) *.50 + finalExam *.50; Practice arithmetic expressions and other CH topics on problets.orgproblets.org
10
Outline Programming Languages Variables Assignment Statements Arithmetic Expressions Input Statements Output Statements Conditional Statements Loops Command vs Program mode
11
Input Statements Input statements are used to input values and assign the values to variables. cin >> variable-name; If you declared a variable called test1, int test1; the following statement: cin >> test1; average = (test1 + test2 + finalExam)/3; If you want to enter two or more values using one input statement, use >> before each variable name. For example the statement cin >> name >> test1; would allow you to enter your name, press return and then enter a value for test1, and press return again.
12
Input Statements (Strings) If you want to input a value into a string variable, the actual text entered would not contain quotation marks as we have seen in the assignment statement. For the statement below that reads in the nickname, the user would enter Bobby, NOT “Bobby”. cin >> name; If your program is to be used by others, you should output a line to tell the users what they are to enter. cout << "Input your name: "; cin >> name; Note: In CH, when cin is used to read in a string and an int or float variable was the last value read in the execution, the statement getchar(); Is necessary to remove the rest of the input line that was entered when the number value was read.
13
Outline Programming Languages Variables Assignment Statements Arithmetic Expressions Input Statements Output Statements Conditional Statements Loops Command vs Program mode
14
Output Statements To write to the screen. To output the value of any type of variable use the form: cout << variable-name; cout << test1 You can also use the the forms cout << “literal string”; or cout << expression; cout<< “text to be written to the screen”; text to be written to the screen cout <<“Congratulations! You got an “ << letterGrade <<“ !!!” << endl; Congratulations! You got an B+ !!! cout << “Your average or numeric grade is “ << average; Your average or numeric grade is 88.66 Another example: cout << “Please enter your age”; cin>> yourAge; When cin or cout statements are used in program mode, the following include statement must be inserted after the comment statements at the beginning of the program to allow the program to accept input and output using the cin and cout instructions. #include
15
Outline Programming Languages Variables Assignment Statements Arithmetic Expressions Input Statements Output Statements Conditional Statements Loops Command vs Program mode
16
Conditional Statements The if-then statement allows the program to choose which statements to execute depending on a condition. The if statement has the form if (condition) { statement list } When the if statement is executed, the condition is evaluated. If it is true, the CH code in the statement list (the then portion) is executed. If the condition is false, the statements in the list are skipped. The brackets can be omitted if there is only one statement in the list.
17
Conditional Statements The condition for floating point numbers and integers is an expression that uses at least one of the relational operators below. The condition must be in parentheses. Equal == Note: Two equal signs are used for the relational operator, equal. Not equal != Less than < Greater than > Less than or equal <= Greater than or equal >= And && Or || Note: logical ANDs and ORs are used to create composite conditions, i.e. conditions with multiple parts. In the case of AND, all sub-conditions of the composite condition must be true in order for the composite condition to evaluate to true. In the case of OR, it suffices that one sub-condition is true for the composite condition to be true.
18
Conditional Statements if (average >= 90){ letterGrade = “A”; cout << “Congratulations! You got an A!”<<endl; } if (average = 85) cout << “Congratulations! You got a B+!”<<endl; if (average >= 70){ cout << “Congratulations! You passed!”<<endl; } else { cout << “I’m sorry. You failed!”<<endl; }
19
Outline Programming Languages Variables Assignment Statements Arithmetic Expressions Input Statements Output Statements Conditional Statements Loops Command vs Program mode
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.