620 likes | 767 Views
2.0 INTRODUCTORY CONCEPT. CLO 1, CLO 2 1.Explain various programming problem using design tools (C2) 2. Apply knowledge of basic concepts of fundamental programming to solving a given problem(C3). 2.0 INTRODUCTORY CONCEPT. At the end of this chapter, student will be able to :
E N D
2.0 INTRODUCTORY CONCEPT CLO 1, CLO 2 1.Explain various programming problem using design tools (C2) 2. Apply knowledge of basic concepts of fundamental programming to solving a given problem(C3)
2.0 INTRODUCTORY CONCEPT At the end of this chapter, student will be able to : • Define a variable and a constant • Identify the rules for naming variables and constant, • Declare constants and variables • Build constant and variables • Explain keywords and operators in programming • Use keywords and operators
2.0 INTRODUCTORY CONCEPT At the end of this chapter, student will be able to : 7. Convert formula into C expression
2.1 Constant And Variables(Identifier) 2.1.1 Variable And Constant Variable • Refer to the memory location where the data is stored • This value keeps changing during the program execution
2.1.1 Constant • a location in the memory that stores data that never changes during the execution of the program • constant can either be: • A numbers, like 15 or 10.5 • A single character, like 'x' or '#‘ • A group of characters (string), like “Beautiful Malaysia”
2.1.2 Rules For Naming Constants And Variables • Can be a combination of alphabets and numbers but must start with an alphabet • Comprise maximum of 40 characters • No commas or blank space is allowed • No special characters can be used except underscore (_)
2.1.3 Declare Constants And Variables • Constant and variable used in a program must be declared in the beginning • To specify the variable and constant data type to the compiler
Declare Constant const data_type name=value ; e. g : const float pie=3.14 ; const intDays_of_Week=7; const char Colour=“RED”;
Declare Variable data_type name ; e. g : float mark_1 ; int Quantity; char StudentID;
2.0 INTRODUCTORY CONCEPT 2.1 Understand Constant And Variables(Identifier) 2.1.5 Build Constants and variables in Programmes Total Pie Payment
2.1.6 Identify and Explain Keywords in C Programmes Are reserved words for which the meaning is already defined to the compiler. an identifier cannot have the same spelling and case as a C keyword
2.0 INTRODUCTORY CONCEPT 2.2 Understand Data Types Data Types Data types are used to store various types of data that is processed by program. Data type attaches with variable to determine the number of bytes to be allocate to variable and valid operations which can be performed on that variable
2.2.1 Basic Data Types in C The C language provides a lot of basic types. Most of them are formed from one of the four basic arithmetic type identifiers in C (char, int, float and double), and optional specifiers (signed, unsigned, short, long). All available basic arithmetic types are listed below:
Char signed long Short signed long int Short int long long Signed short long long int Signed short int signed long long Int signed long longint Signed int float Long double long int
2.2.2 Basic Data Types Numeric
Numeric Keyword Variable Type Range char Character (or string) -128 to 127 int integer -32,768 to 32,767 short Short integer -32,768 to 32,767 short intShort integer -32,768 to 32,767 long Long integer 2,147,483,648 to 2,147,483,647 unsigned char Unsigned character 0 to 255 unsigned intUnsigned integer 0 to 65,535 unsigned short Unsigned short integer 0 to 65,535 unsigned long Unsigned long integer 0 to 4,294,967,295 float Single-precision +/-3.4E10^38 to +/-3.4E10^38 floating-point (accurate to 7 digits) double Double-precision +/-1.7E10^308 to +/1.7E10^308 floating-point (accurate to 15 digits)
Characters C stores character type internally as an integer. Each character has 8 bits so we can have 256 different characters values (0- 255).Character set is used to map between an integer value and a character. The most common character set is ASCII. The char type store only one symbol of data (letter, digit, space, tab and so on).
String The string type is able to keep virtually data of any length. Also string type is based on arrays and uses reference type working with memory.
2.2.3 Explain The Basic Data Types in C a. int int is used to define integer numbers. { int Count; Count = 5; }
b. Char chardefinescharacters. { char Letter; Letter = 'x'; }
c. float float is used to define floating point numbers. { float Miles; Miles = 5.6; }
d. Double double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes. { double Atoms; Atoms = 2500000; }
2.3 Understand Operators And Expression Operator • Operators are symbols which take one or more operands or expressions and perform arithmetic or logical computations.
2.3.1 Types Of Operator Types of operators available in C are as follows: Arithmetic Mathematical operators are used for performing simple mathematical calculations such as addition, subtraction, multiplication and division.
Relational Relational operators are used to compare two operands.
Logical • Logical operators are used to combine two simple statements into a compound statement. • Using logical operators, you can simulate Boolean algebra in C.
Boolean Boolean operators define the relationships between words or groups of words. True=1 False=0
2.3.2 Operators used in programming 3 types : a. Mathematic operators b. Relational operators c. Logical operators
Operator precedence : i)bracket () ii) *,/,% (left to right) iii)+,- (left to right)
The AND (&&) operator will evaluate to true only if all the conditions in the expression returns a true value. Example You will pass the exam only when you get more than 50 in subject 1, subject 2 and subject3.
The OR (||) operator will evaluate to true even if one of the conditions is true. Example To get a medal, you should either be 1st or 2nd in the race.
The NOT (!) operator will evaluate to true if the condition fails and vice versa. Example If your sex is not M, you are girl