0% found this document useful (0 votes)
5 views

Fundamentals of Programming - Lecture5-1

The document outlines a lecture on the fundamentals of programming in C, focusing on expressions, statements, control flow structures, and branching. It explains various conditional statements such as if, else, and nested if statements, providing examples and their usage in programming. Additionally, it includes lab exercises for practical application of the concepts discussed.

Uploaded by

Kasun Sameera
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)
5 views

Fundamentals of Programming - Lecture5-1

The document outlines a lecture on the fundamentals of programming in C, focusing on expressions, statements, control flow structures, and branching. It explains various conditional statements such as if, else, and nested if statements, providing examples and their usage in programming. Additionally, it includes lab exercises for practical application of the concepts discussed.

Uploaded by

Kasun Sameera
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/ 19

Fundamentals of

Programming
Lecture 5
C h a m i l a K a r u n a t i l a ke
Department of ICT
Fa c u l t y o f Te c h n o l o g y
U n i v e rs i t y o f S r i J ay e w a r d e n e p u ra
[email protected]
Today’s Lecture Outline
 C Expressions
 C Statements and Blocks
 Introduction to C control flow structures
 C Branching
 If condition statements (if, else, else if)
 Nested if statements

2
C Expressions
 In C Language, an expression is any legal combination of one or more explicit
values, constants, variables, operators, and functions that represents a value.
 The representing value (resulting value) is usually one of various primitive types
or derived data types.
 Every expression consists of at least one operand and can have one or
more operators.
Example:
x+5 is an expression. Two operands x and 5 are combined together by operator +.

3
Statements and Blocks
 An expression such as x = 0 or i++ or printf (“Hello World!\n”) becomes a statement
when it is followed by a semicolon.
x = 0;
i++;
printf (“Hello World!\n”);

 A pair of braces { }, are used to group statements together into a compound


statement, or block.
{
int x=19;
int y = 10;
printf (“%d %d\n”, x++,--y);
}

4
C Control Structures
 C programs which were discussed up to now have a regular flow, from first line to last
line of the program.
 C language(and many other languages) provide mechanisms to handle alternative,
non-leaner flows of the program.
 This flow controlling mechanisms are called flow control structures and they specify
the order in which computations are performed.
 C provides two styles of flow control:
 Branching
 Looping
 Branching specifies what actions to be taken and looping states how many times a
certain action to be repeated.

5
Branching Control Structures
 Using branching statements, the flow of the program is divided into
two or more branches and depending on a condition specified at the
branching, only one of those paths are followed.
 There are three main categories of branching data structures
◦ If condition statements(including if, else, else if)
◦ switch statements
◦ Conditional operator(?:)

6
if Condition Statements
 Basic if statement has if keyword followed by a Boolean condition statement which is placed
between a pair of opening and closing parentheses.
if(condition)
 That condition statement is usually a Boolean expression, that means the result value is true or false.
if( x>10 ) if(x==0) if(x<=y) if( x != 0)
 However, any expression could be used since there is a Boolean value representation for every
expression. If the value is something like 0, 0.0 or null, the Boolean value is false and for any other
non zero value the Boolean value is true.
if(0) if(1) if(‘C’) if(‘\0’)
 if the expression is true then the statement or block of statements gets executed otherwise these
statements are skipped.

7
if Condition Statements
 If statement is followed by a single line statement or a block of statements. The block is
indicated by a pair of braces.
if(x<0)
if(x<0) {
x++; printf(“%d”,x);
x++;
}

 Only If the condition is true, the following statement or the block of statements are executed.
 In a case of the condition is false, the following statement or the block of statements are
skipped and jumped into the subsequent statements.
if(x<0)
if(x<0) {
x++; printf(“%d”,x);
x++;
}

8
if Condition Statements : Example

9
if else Condition Statements
 In basic if statement, when condition is not satisfied (false), the execution is jumped
back into the regular flow skipping the if statement or block.
 There are situations that two branches are necessary. Execution follows one branch or
the other, depending on the condition is true or false.
 The only difference from regular “if statement” is that immediately after if block’s
closing brace( or statement’s semicolon in a case of a single statement), “else” key word
and a block of statements would be placed.
if(x<0)
if(condition) True {
{ printf(“%d”,x); False
…….. x++;
} }
else else
{ {
………. x--;
} }
10
if else Condition Statements

11
If, else if, else Condition Statements
 In case of many parallel branches are needed in program execution, C provides else if
statements.
 Following an if block, instead of else block, one or more else if blocks could be used to specify
several branches of execution flows.
 Optional else block would follow else if blocks and program only executes the else if block that
satisfies the condition.
 If no else if block is satisfied the condition, it will check for an else block at the end of the else if
blocks.
 If else is found, else block is executed. If there is no else block, then execution moved back to
the normal flow and the rest of the program is executed.

12
If, else if, else Condition Statements
if(condition 1) False if(x==1) In a case of x is 3
{ {
…….. printf(“Option 1\n”);
} }
else if (condition 2) False else if (x==2)
{ {
………. printf(“Option 2\n”);
} }
else if (condition 3) True else if (x==3)
{ {
………. printf(“Option 3\n”);
} }
else else
{ Skip the rest of the {
………. conditional statements printf(“Invalid..\n”);
} }

13
If, else if, else Condition Statements :
Example

14
Nested if statements
 Inside if, else or else if block, another if, if-else or if-elseif-else blocks can be included.
 These types of conditional statements are called nested if statements. The internal if
statements are independent from conditions of the outer if statements.
if(a==100)
if(condition 1) {
{ if(b==100)
if(condition 2) {
{ printf(“Both are 100!” );
………. }
} }
} else
else {
{ printf(“A is not 100!” );
………. }
}

15
Nested if statements : Example

16
Questions?

17
Lab Exercise 3
1. Write a C program to get temperature(in Celsius) as user input. Check whether the
temperature is greater than or equals to 26.
i) If it’s greater or equals, then print “AC ON..!”
ii) Otherwise, print “AC Off….!”

18
Lab Exercise 3
2. Write a C program to get marks as user input(With a message, “Enter your marks : ”).
Use following condition to generate grade.
i) 100 >= marks >= 85  A
ii) 85 > marks >= 65  B
iii) 65 > marks >= 50  C
iv) 50 > marks >= 35  D
v) 35 > marks  F
vi) Anything else  I
Finally you should print a message like follows:
“Your grade is A !”

19

You might also like