Introduction to C
Data Types in C
Control Structure in C
Unit 2: C BASICS, OPERATORS, LOOPS
Course: Programming for Problem Solving
Dr. Vishwa Pratap Singh
NextGen Academy
March 11, 2024
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 1 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Contents
1 Introduction to C
Features of C
Basic C program structure
C tokens
2 Data Types in C
Data types
C Operators and precedence
Type conversion
3 Control Structure in C
Control Structures:
if
nested if
switch-case
while
do- while
for statements
Unconditional control statements
break
continue
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 2 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Introduction to C
C is a general-purpose, high-level language developed by Dennis M. Ritchie to
develop the Unix Operating systems.
C was originally first implemented on the DEC PDP-11 computer in 1972.
In 1978, Brian Kernighan and Dennis Ritchie introduced the first publicly
available description of C, now known as the K&R standard.
C is a widely used professional language for multiple reasons:
1 Easy to learn
2 Structured language
3 Efficient
4 Handles low-level activities
5 Compiled on a variety of computer platforms
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 3 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Features of C
1 Procedural Language:
Step-by-step execution of predefined instructions.
C allows multiple functions for tasks.
Object-oriented programming is another commonly used paradigm.
2 Fast and Efficient:
Newer languages like Java, Python offer more features but may be less efficient due
to additional processing.
C, a middle-level language, enables direct hardware manipulation.
3 Modularity:
Storing C code in libraries for future use.
C’s power resides in its libraries.
4 Statically Typed:
Variable types checked at compile time.
Each variable must have a declared type.
5 General-Purpose Language:
Used in various applications such as operating systems (Windows, Linux, iOS,
Android, macOS) and databases (PostgreSQL, Oracle, MySQL, MS SQL Server, etc.).
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 4 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Features of C
1 Rich set of Built-in Operators:
Diverse language with a rich set of operators for writing complex or simplified C
programs.
2 Libraries with Rich Functions:
Robust libraries and functions in C facilitate even beginner coders.
3 Middle-Level Language:
Combines capabilities of assembly language and high-level language features.
4 Portability:
C programs can run and compile on various systems with minimal changes.
5 Easy to Extend:
C programs can be extended with additional features and operations.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 5 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Writing a ’C’ Program
A typical ’C’ program consists of the following:
1 Preprocessor Commands
2 Functions
3 Variables
4 Statements and Expressions
5 Comments
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 6 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Understanding a ’C’ Program
Let’s analyze the following program:
1 #include <stdio.h> is a preprocessor command that includes the stdio.h
file before compilation.
2 int main() is the main function where program execution begins.
3 The comment /*...*/ guides the compiler and is not processed.
4 printf(...) is a function displaying ”Make in India!” on the screen.
5 The line return 0; terminates the main() function and returns the value 0.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 7 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
C Tokens
C Tokens are the smallest meaningful elements in the C programming language.
They can be classified into six types:
1 Identifiers
2 Keywords
3 Constants
4 String literals
5 Operators
6 Special symbols (such as parentheses, braces, and semicolons)
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 8 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Keywords in C
Keywords are pre-defined or reserved words in a programming language. In C:
Each keyword performs a specific function in a program.
Keywords cannot be used as variable names as they’re reserved for specific use.
You cannot redefine keywords in C.
C language supports 32 keywords, some of which include int, for, if, etc.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 9 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Identifiers in C
Identifiers are used for naming variables, functions, and arrays in C:
User-defined names consisting of letters and digits, starting with a letter or
underscore.
Must differ in spelling and case from keywords.
Follow specific rules:
Begin with a letter or underscore.
Consist of only letters, digits, or underscore. No special characters allowed.
Cannot be a keyword.
No white space allowed.
Up to 31 characters long; only the first 31 characters are significant.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 10 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Rules for Naming Identifiers in C
Certain rules should be followed while naming identifiers in C:
1 They must begin with a letter or underscore ( ).
2 They must consist of only letters, digits, or underscore. No other special
character is allowed.
3 It should not be a keyword.
4 It must not contain white space.
5 It should be up to 31 characters long; only the first 31 characters are significant.
Note: Identifiers are case-sensitive, so names like variable and Variable will be
treated as different.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 11 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Constants in C
Constants in C:
Variables with fixed values that cannot be modified once defined.
Can belong to any data type.
Examples:
const int c var = 20;
const int* const ptr = & var;
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 12 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Strings in C
Strings in C:
Arrays of characters ending with a null character (’\0’).
Indicate the end of the string.
Always enclosed in double quotes.
Examples:
char string[20] = ’g’, ’e’, ’e’, ’k’, ’s’, ’f’, ’o’, ’r’, ’g’,
’e’, ’e’, ’k’, ’s’, ’\0’;
char string[20] = "geeksforgeeks";
char string[] = "geeksforgeeks";
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 13 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Special Symbols in C
The following special symbols are used in C with specific meanings:
1 Brackets []: Used as array element references for single and multidimensional
subscripts.
2 Parentheses (): Indicate function calls and function parameters.
3 Braces : Mark the start and end of a block of code with multiple executable
statements.
4 Comma (,): Used to separate multiple statements or parameters in function
calls.
5 Colon (:): Invokes an initialization list.
6 Semicolon (;): Known as a statement terminator, ending a logical entity in C
code.
7 Asterisk (*): Creates pointer variables and performs multiplication.
8 Assignment operator (=): Assigns values and validates logical operations.
9 Preprocessor (): Automatically transforms the program before compilation.
10 Period (.): Accesses members of a structure or union.
11 Tilde (): Used as a destructor to free memory space.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 14 / 56
Introduction to C Features of C
Data Types in C Basic C program structure
Control Structure in C C tokens
Operators in C
Operators in C:
Symbols triggering actions when applied to C variables and objects.
Operands are the data items on which operators act.
Classification based on the number of operands they act upon:
1 Unary Operators: Act on a single operand, e.g., increment and decrement operators.
2 Binary Operators: Act on two operands, further classified into:
i. Arithmetic operators
ii. Relational Operators
iii. Logical Operators
iv. Assignment Operators
v. Bitwise Operator
3 Ternary Operator: Requires three operands, e.g., Conditional Operator (?).
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 15 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Contents
1 Introduction to C
Features of C
Basic C program structure
C tokens
2 Data Types in C
Data types
C Operators and precedence
Type conversion
3 Control Structure in C
Control Structures:
if
nested if
switch-case
while
do- while
for statements
Unconditional control statements
break
continue
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 16 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Data Types in C
Variables in C:
Essential part of any programming language.
A label assigned to a storage space manipulated by a program.
Every variable in C has a specific data type that:
Dictates the size and structure of allocated memory.
Specifies the permissible range of values.
Determines operations allowed on the variable.
For instance, to calculate the area of a rectangle, variables (e.g., length and width)
are used, each with its own data type, allocating specific memory space for the data.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 17 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Data Variables in C
Variables in C:
Names given to storage areas manipulated by programs.
Each variable has a specific type:
Determines memory size and layout.
Specifies the range of values stored.
Defines allowed operations on the variable.
Variable names:
Composed of letters, digits, and underscores.
Must start with a letter or underscore.
Case-sensitive in C.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 18 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Variable Declaration in C
General syntax for declaring a variable:
Two ways to declare variables:
1 Declaration without initializing:
data type variable name;
Example: char Final Grade;
2 Declaration with initialization:
data type variable name = val;
Example: int age = 22;
data type: Specifies the variable type (e.g., int, char).
variable name: Name of the variable.
val: Value assigned to the variable during initialization.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 19 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Variable Declaration Types in C
Types of Declaration of Variables in C:
1 Primary Type Declaration
Declares variables with primitive or built-in data types.
Common primary data types: int, float, char, boolean, double, long, etc.
Examples:
1 Single primary type declaration: char Grade = ’A’;
2 Multiple primary type declarations in the same line:
int Length = 12, Width = 13, Depth = 14;
3 Multiple primary type declarations in different lines:
int variable name1 = 12;
float variable name2 = 22.22;
char variable name3 = ’S’;
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 20 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
User-Defined Type Declaration in C
User-Defined Type Declaration
User-defined data types defined by the programmer.
Common types: struct, union, enum, typedef, etc.
Structure
Groups different data types into a single user-defined type.
Union
Members share a common memory location, allowing access to one member at
a time.
Typedef
Defines new data types using typedef.
Example:
typedef char person name;
typedef int person age;
typedef float salary;
Advantages
Enables creation of custom data types like person info.
Enhances program readability.
Difference
In primary type declaration, any variable name can be used for any data type,
while in user-defined type declaration, any identifier can be used for any data
type.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 21 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Rules to Declare Variables in C
Rules for Variable Declaration in C:
1 Variables should not share the same name within the same scope.
2 Variable names can start with letters or underscores but not with a number.
3 Variable names must not be reserved keywords in C.
4 Variable names can contain a mix of alphabets, numbers, and underscores.
5 All declaration statements must end with a semicolon (;).
6 Suggested practice: Declare variables of the same data type in the same line.
7 Use meaningful variable names that clearly describe their purpose.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 22 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Data Types and Rules for Variable Declaration in C
Data Types in C:
Data types define the type of data a variable can hold.
Types include: integer, float (decimal), character, boolean (true/false), etc.
Data types specify the kind of data a variable will store.
Rules for Variable Declaration in C:
1 Variables should not share the same name within the same scope.
2 Variable names can start with letters or underscores but not with a number.
3 Variable names must not be reserved keywords in C.
4 Variable names can contain a mix of alphabets, numbers, and underscores.
5 All declaration statements must end with a semicolon (;).
6 Suggested practice: Declare variables of the same data type in the same line.
7 Use meaningful variable names that clearly describe their purpose.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 23 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Primary Data Types in C
Primary Data Types or Basic Data types:
These are the fundamental data types from which all other types are derived.
Consist of integer, floating point, character, and void.
Four main types of primary/basic data types are:
1 int Data Type in C:
Represents integers, both positive and negative whole numbers.
Example: {int num = 10;}
2 Char Data Type:
Represents single characters enclosed in single quotes.
Example: {char letter = ’A’;}
3 Float Data Type:
Represents floating-point numbers (decimals).
Example: {float value = 3.14;}
4 Void Data Type:
Represents absence of type or an empty data type.
Used mostly in function definitions and pointers.
Example: {void function();}
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 24 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Secondary Data Types in C
Secondary Data Types:
Secondary data types are formed by combining two or more primary data types
in C.
They consist mainly of user-defined, derived, and constant data types.
Two main types of secondary data types are:
User-Defined Data Type in C:
label=()
Created by users to suit their specific needs.
Examples include structs, unions, enums, and typedefs.
lbbel=() Derived Data Types:
Derived from primary data types to form new types.
Examples include arrays, pointers, functions, and structures.
lcbel=() Constant Data Types:
Represent fixed values that cannot be altered during program execution.
Examples include const variables or literal constants.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 25 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
C Operators and Precedence
C Operators:
Operators in C are used to perform operations on variables and values.
Example: int myNum = 100 + 50;
Operators can perform addition, subtraction, multiplication, division, etc.
Operator Usage Examples:
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 26 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Arithmetic Operators
Arithmetic Operators:
Arithmetic operators perform mathematical operations.
Examples: + (addition), - (subtraction), * (multiplication), / (division), %
(modulus)
Usage examples:
Addition: int sum = 10 + 5;
Subtraction: int difference = 20 - 8;
Multiplication: int product = 6 * 4;
Division: int quotient = 15 / 3;
Modulus: int remainder = 20 % 7;
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 27 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Relational Operators
Relational Operators:
Relational operators compare two operands.
Examples: ¡ (less than), ¿ (greater than), ¡= (less than or equal to), ¿= (greater
than or equal to), == (equal to), != (not equal to)
Usage examples:
Less than: bool result = (5 < 10);
Greater than or equal to: bool result = (20 >= 15);
Equal to: bool result = (x == y);
Not equal to: bool result = (a != b);
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 28 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Logical Operators
Logical Operators:
Logical operators perform logical AND, OR, and NOT operations.
Examples: && (logical AND), —— (logical OR), ! (logical NOT)
Usage examples:
Logical AND: bool result = (true && false);
Logical OR: bool result = (true || false);
Logical NOT: bool result = !(x > y);
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 29 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Assignment Operators
Assignment Operators:
Assignment operators assign values to variables.
Examples: = (assignment), +=, -=, *=, /=, %= (compound assignment)
Usage examples:
Simple assignment: x = 10;
Compound assignment: y += 5; (equivalent to y = y + 5;)
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 30 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Bitwise Operators
Bitwise Operators:
Bitwise operators perform operations on bits of integer operands.
Examples: &, —, ˆ (bitwise AND, OR, XOR), ¡¡, ¿¿ (left shift, right shift), ∼
(bitwise NOT)
Usage examples:
Bitwise AND: int result = 5 & 3;
Bitwise OR: int result = 5 | 3;
Bitwise XOR: int result = 5 ˆ 3;
Bitwise left shift: int result = 8 << 2;
Bitwise right shift: int result = 8 >> 2;
Bitwise NOT: int result = ∼5;
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 31 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Precedence of Operators
Precedence of Operators:
The precedence of operators determines the order of execution in an expression.
Example: int x = 5 - 17 * 6;
In C, the precedence of * is higher than - and =.
Hence, 17 * 6 is evaluated first, then the expression involving - is evaluated.
Result: x holds the value of the expression 5 - (17 * 6).
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 32 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Arithmetic Operators Precedence
Operator Description Associativity
+ Addition Left to Right
- Subtraction Left to Right
* Multiplication Left to Right
/ Division Left to Right
% Modulus Left to Right
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 33 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Relational Operators Precedence
Operator Description Associativity
== Equal to Left to Right
!= Not equal to Left to Right
¿ Greater than Left to Right
¡ Less than Left to Right
¿= Greater than or equal to Left to Right
¡= Less than or equal to Left to Right
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 34 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Logical Operators Precedence
Operator Description Associativity
&& Logical AND Left to Right
—— Logical OR Left to Right
! Logical NOT Right to Left
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 35 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Assignment Operators Precedence
Operator Description Associativity
= Assign Right to Left
+= Add and assign Right to Left
-= Subtract and assign Right to Left
*= Multiply and assign Right to Left
/= Divide and assign Right to Left
%= Modulus and assign Right to Left
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 36 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Bitwise Operators Precedence
Operator Description Associativity
& Bitwise AND Left to Right
— Bitwise OR Left to Right
ˆ Bitwise XOR Left to Right
˜ Bitwise NOT Right to Left
¡¡ Left shift Left to Right
¿¿ Right shift Left to Right
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 37 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Associativity of Operators
The associativity of operators determines the evaluation direction in an
expression.
Example: b = a; - Here, the = operator’s associativity is right to left. Thus, a is
assigned to b.
In case of same precedence operators, associativity determines execution order.
Example: 1 == 2 != 3 - Operators == and != have the same precedence and
left-to-right associativity.
Thus, 1 == 2 is executed first, resulting in (1 == 2) != 3.
Note: To enhance readability in expressions with multiple operators, use
parentheses.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 38 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
C Type Conversions
Type conversion refers to changing data types before evaluation in C.
Two types of type conversions:
Implicit Conversion (Type Coercion):
Compiler automatically converts one data type value into another.
Occurs during assignment or using operators.
Example: int a = 17.35; - 17.35 (R-value) automatically converted to int (L-value).
In arithmetic, relational, and logical operators, lower size data types are converted to
higher size before evaluation.
Conversion order: bool -¿ char -¿ short int -¿ int -¿ unsigned int -¿ long -¿ unsigned long -¿
long long -¿ float -¿ double -¿ long double.
Explicit Conversion (Type Casting):
Programmer explicitly instructs the compiler to convert one type to another using typecast
operator.
Syntax: (data type) expression.
Example: (float) 1 / 3 evaluated as 1.0 / 3 yields 0.333333; whereas 1 / 3 yields
0.
Using typecast operator, as in ((int)num)
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 39 / 56
Introduction to C Data types
Data Types in C C Operators and precedence
Control Structure in C Type conversion
Code Example: Type Conversion in C
#include <stdio.h>
void main () {
float a, b;
a = 35 / 17;
b = (float) 35 / 17;
printf("a = %f, b = %f\n", a, b);
}
Output:
a = 2.000000, b = 2.058824
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 40 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
Contents
1 Introduction to C
Features of C
Basic C program structure
C tokens
2 Data Types in C
Data types
C Operators and precedence
Type conversion
3 Control Structure in C
Control Structures:
if
nested if
switch-case
while
do- while
for statements
Unconditional control statements
break
continue
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 41 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
Control Structures in C
Control statements manage the execution flow of a program in C.
Various types of control statements:
1 Sequential Control:
Program statements execute sequentially, one after another, from start to end.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 42 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
Sequential Control Example
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
int x, y, sum;
printf("enter the two numbers");
scanf("%d%d", &x, &y);
sum = x + y;
printf("the sum of the two numbers is =%d", sum);
getch();
}
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 43 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
Control Structures in C
Control statements manage the execution flow of a program in C.
Various types of control statements:
1 Sequential Control:
Program statements execute sequentially, one after another, from start to end.
2 Conditional Control (Selection or Decision Control):
Execution depends on a condition-test.
If condition is true, one set of statements is executed; otherwise, another set is followed.
Known as Decision Control as it determines which set of statements to execute.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 44 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
Control Structures in C
Control statements manage the execution flow of a program in C.
Various types of control statements:
1 Sequential Control:
Program statements execute sequentially, one after another, from start to end.
2 Conditional Control (Selection or Decision Control):
Execution depends on a condition-test.
If condition is true, one set of statements is executed; otherwise, another set is followed.
Known as Decision Control as it determines which set of statements to execute.
3 Iteration Control (Loops):
Used for executing statements repeatedly based on a test condition.
Statements within the loop execute repetitively until the test condition becomes false.
Types of loops:
While loop
Do-while loop
For loop
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 45 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
Decision Control Statements in C
Decision control instructions manage program flow based on conditions.
These instructions evaluate a condition and execute a set of statements depending
on its truth value.
Using decision control statements, the program can execute specific statements
based on a condition’s outcome (true or false).
In decision control statements, a group of statements executes when the condition is
true. Otherwise, the else part statements are executed.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 46 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
Types of Decision Control Statements in C
Three major types of decision control statements in C:
1 If Statements:
2 If-Else Statements:
3 Nested-If Statements:
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 47 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
If Control Statements in C
1 If Statements:
Used to execute a block of code if a certain condition is true.
Highly utilized in C programming for decision-making based on user input or other
factors.
Example: If Statement Program
#include<stdio.h>
#include<conio.h>
int main()
{
int age;
printf(”enter age\n”);
scanf(”%d”,&age);
if(age>=55)
{
printf(”person is retired\n”);
}
getch();
}
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 48 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
If-Else Statements in C
The ”if-else statement” in C is a decision control statement.
It executes a block of code if a condition is true and another block if the condition is
false.
This statement consists of two blocks: one for the true condition (if block) and one
for the false condition (else block).
Syntax:
i f ( condition x ) {
/ / Statements inside the ’ i f ’ block
} else {
/ / Statements inside the ’ e l s e ’ block
}
It’s essential to note that ’else’ cannot exist without an ’if’ statement.
The if-else statement allows different actions based on whether the condition is true
or false.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 49 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
Nested-If Statements in C
In C programming, ”nested if statements” are used for checking multiple conditions
in a specific order. It involves placing an if statement inside another if statement.
Syntax:
if (condition x)
{
// The statements present inside the body of if
}
else {
// The statements present inside the body of else
}
When the condition in the outer ’if’ block is true, it checks the inner ’if’ condition.
Execution occurs based on the true/false evaluation of these conditions, providing
various cases and a default statement.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 50 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
Switch-Case Statement in C
The switch-case statement in C is a multi-way decision statement that evaluates a
single variable.
Syntax:
switch(expression) { case expression1: // Statements break; case expression2: //
Statements break; // More cases if needed default: // Default statements
}
It simplifies handling multiple cases based on the value of a single variable.
The switch statement jumps to the corresponding case and executes the statements
until it encounters a break or reaches the end.
The default case is executed when none of the cases matches the value of the
expression.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 51 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
While Loop in C
The while loop in C executes as long as the test condition remains true.
It doesn’t rely on a predefined number of iterations, unlike the for loop.
Syntax:
initialization expression;
while(test expression) {
// Body of the while loop
update expression;
}
The loop continues to execute as long as the test condition evaluates to true.
When the test condition becomes false, the loop terminates, and execution moves to
the next statement after the loop.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 52 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
Do-While Loop in C
The do-while loop in C is similar to a while loop but with a crucial difference: the
test condition is checked at the end of the loop body.
In a do-while loop, the loop body executes at least once, regardless of the test
condition.
Syntax:
initialization expression; do {
// Body of do-while loop
update expression;
} while(test expression);
The loop body executes first, then the test condition is checked at the end of the loop.
If the test condition is true, the loop continues; otherwise, it terminates.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 53 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
For Loop in C
The for loop in C is a control structure that enables a specific number of iterations.
It condenses several steps into a single line, allowing precise control over loop
variables.
Syntax:
for(initialize expression; test expression; update expression)
{ // Body of for loop
}
The for loop comprises:
Initialization Expression: Assigns a starting value to the loop variable.
Test Expression: Checks the condition for loop continuation. If true, executes the loop
body; otherwise, exits.
Update Expression: Modifies the loop variable after each iteration.
It repeatedly executes the loop body until the test expression becomes false.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 54 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
Break Statement in C
The break statement in C is used for immediate termination of a loop or a switch
case.
Uses of the break statement:
Termination of a loop: When encountered inside a loop, it immediately exits the loop, and
program control resumes after the loop.
Termination of a switch case: It can be used to terminate a case in a switch statement.
Handling nested loops: In nested loops, it terminates the innermost loop and continues
execution after the loop block.
Syntax:
break;
The break statement provides a means to exit from a loop or switch case
prematurely.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 55 / 56
Introduction to C
Control Structures:
Data Types in C
Unconditional control statements
Control Structure in C
Continue Statement in C
The continue statement in C programming is similar to the break statement but
instead of terminating the loop, it skips the current iteration and proceeds to the
next iteration of the loop.
Behavior:
In a for loop, it proceeds to execute the conditional test and the increment/decrement
portion.
For do-while and while loops, it moves the program control to the conditional test.
Syntax:
continue;
The continue statement allows skipping certain iterations of a loop based on a
condition.
Dr. Vishwa Pratap Singh Unit 2: C BASICS, OPERATORS, LOOPS 56 / 56