0% found this document useful (0 votes)
27 views20 pages

C - Variables, Loops & Conditional Statements

This document discusses key concepts in C programming including variables, loops, and conditional statements. It defines different variable types in C and how to declare and name variables. It describes the three types of loops - while, do-while, and for - and how they are used to repeatedly execute code. It also explains common loop control statements and the basic logic operators used to construct conditional statements like if/else.

Uploaded by

owengitau02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views20 pages

C - Variables, Loops & Conditional Statements

This document discusses key concepts in C programming including variables, loops, and conditional statements. It defines different variable types in C and how to declare and name variables. It describes the three types of loops - while, do-while, and for - and how they are used to repeatedly execute code. It also explains common loop control statements and the basic logic operators used to construct conditional statements like if/else.

Uploaded by

owengitau02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

C:

Variables
Loops
Conditional-statements
Agenda
1. Variables in C

2. Conditional statements

3. Loops
Variables
Variables are containers
● Variables are containers for storing data values, like numbers
and characters.
● In C, there are different types of variables (defined with
different keywords), for example:
int - stores integers (whole numbers), without decimals, such as 123 or -123

float - stores floating point numbers, with decimals, such as 19.99 or


-19.99

char - stores single characters, such as 'a' or 'B' . Char values are
surrounded by single quotes
Declaring (Creating) Variables
● To create a variable, specify the type and assign it a value:
type variableName = value; ● type is one of C types (such as int)
● variableName is the name of the variable
int cohort_Num = 14; (such as x or myName).
● The equal sign is used to assign a value to
the variable.
cont
● You can also declare a variable without assigning the value,
and assign the value later:
// Declare a variable
int myNum;

// Assign a value to the variable


myNum = 15;
Naming variables
➢ Names can contain letters, digits and underscores
➢ Names must begin with a letter or an underscore (_)
➢ Names are case sensitive (myVar and myvar are different
variables)
➢ Names cannot contain whitespaces or special characters like
!, #, %, etc.
➢ Reserved words (such as int) cannot be used as names
Loops
What are loops
● Loop is used to execute the block of code several times
according to the condition given in the loop.
● It executes the same code multiple times so it saves code and
also helps to traverse the elements of an array.
● There are 3 types of loop –
a. while loop
b. do – while loop
c. for loop
1. while loop
● While loop execute the code until condition is false.
● While loop is executed only when the condition is true.
2. do-while loop
● It also executes the code (at least once) until condition is
false.
● Code is executed whether condition is true or false.
cont...
3. for loop
● Executes the code until condition is false.
● It is used when number of iterations are known where while
loop is used when number of iterations are not known.
● In this three parameters are given that is
○ Initialization
○ Condition
○ Increment/Decrement
Loop control statements
● These statements change the execution of the loop from its
normal execution.
○ break statement – It is used to end the loop or switch
statement and transfers execution to the statement
immediately following the loop or switch.
○ continue statement – It skip some statements according
to the given condition.
○ goto statement – It transfer control to the labeled
statement.
Conditions
&
if statements
intro
● We already know that C supports the usual logical conditions
from mathematics:
○ Less than: a < b
○ Less than or equal to: a <= b
○ Greater than: a > b
○ Greater than or equal to: a >= b
○ Equal to a == b
○ Not Equal to: a != b
1. if
● Used to specify a block of code to be executed if a condition is
true.
2. else
● Used to specify a block of code to be executed if the
condition is false.
3. else if
● Used to specify a new condition if the first condition is false.
Resources
1. All About variables
2. Loops
3. Condition statements

You might also like