The use of Statements, Operators & Control Structures
in C programming
Size: 62.04 KB
Language: en
Added: Oct 19, 2025
Slides: 15 pages
Slide Content
LECTURE 3: Statements, Operators & Control Structures
Statements A statement is a complete direction instructing the computer to carry out some task e.g. x =2+3 ; Differentiate between a Single Statement and a Compound Statements { block of 2 or more statements} . Expressions: In C, an expression is anything that evaluates to a numeric value. Expressions can range from simple to complex .
The Fundamental Operators in C Operators in C language can be grouped as follows: Assignment Operator . Arithmetic Operators Unary Mathematical operators . Relational operators . Logical operators .
Arithmetic Operators Examples; / (Division) e.g. z=x/2 ; - (Subtraction) e.g. z=x-2 ; + (Addition) e.g. z=x+2 ; % (Modulus) e.g. z = x%2; (Multiplication) e.g. z = x*2 ; Assignment Operator (=)
Unary Operators Post- Increment/Pre - Increment Decremental Operator (--) e.g. a-- = a-1 ; ( Post ) --a= a-1 ; ( Pre ) Incremental Operator (++) e.g. a++ = a+1 ; ( Post ) ++a = a+1 ; ( Pre )
Relational Operators Examples; >, ( Greater Than ) e.g. x>y <, ( Less Than ) e.g. x<y >= , ( Greater or Equal To ) e.g. x>=y <= , ( Less or Equal To ) e.g. x<=y == , ( Equality Operator ) e.g . x==y !=, ( Not Equal To ) e.g. x!=y
Logical Operators Examples; Logical AND (&&) e.g. {(a<b) && (b<c)} Logical OR (||) e.g. {( a<b) || (b<c )} Logical NOT (!) e.g. {!(a>b)}
Control Structures If condition If ….else Multiple Case Selection Structures: If …… else if…… Switch * ( Read! )
The if statement Syntax: if (condition) { statement (s); } Example: if (a>=b) { x =b-a; }
If….else Syntax: if (condition ) { statement1 (s); } else { statement2 (s); } Example : if (a>=b) { x=b-a; } else { y=a-b; }
Multi Selection Case Syntax: if (Condition1) { statement1; } else if (condition2) { statement2 } else if (condition3) { statement3 } else …… …. Main() { char ch; if (ch= =‘m’){ printf (“Monday”); } else if (ch = =“t”){ printf (“Tuesday”);} else if (ch = =‘w’) { printf (“Wednesday”); } else if (ch= =“h”) { printf (“Thursday”); } else if (ch= =‘f’ ) { printf (“Friday”); } else if(ch= =‘a’) { printf (“Saturday”); } Else if (ch= =‘s’) { printf(“Sunday”); } Else printf (“Not a day of the Week”); } }