PPS Unit 3-Notes
PPS Unit 3-Notes
Decision Making in C
(if , if..else, Nested if, if-else-if )
• The conditional statements (also known as decision control structures) such
as if, if else, switch, etc. are used for decision-making purposes in C
programs.
• They are also known as Decision-Making Statements and are used to evaluate
one or more conditions and make the decision whether to execute a set of
statements or not.
• These decision-making statements in programming languages decide the
direction of the flow of program execution.
4. if-else-if Ladder
5. switch Statement
6. Conditional Operator
7. Jump Statements:
• break
• continue
• goto
• return
So start discussing each of them one by one.
1. if in C
The if-else statement is used to carry out a logical test and then take one of twopossible
actions depending on the outcome of the test (ie, whether the outcome is trueor false).
The if statement is the most simple decision-making statement. It is used to decide whether
a certain statement or block of statements will be executed or not i.e if a certain condition is
true then a block of statements is executed otherwise not.
Syntax of if Statement
if(condition)
{
// Statements to execute if
// condition is true
}
• Here, the condition after evaluation will be either true or false.
• C if statement accepts boolean values – if the value is true then it will execute
the block of statements below it otherwise not.
• If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by
default if statement will consider the first immediately below statement to be
inside its block.
Flowchart of if Statement
Example of if in C
// C program to illustrate If statement
#include <stdio.h>
int main()
int i = 10;
if (i > 15)
Output
I am Not in if
As the condition present in the if statement is false. So, the block below the if
statement is not executed.
2. if-else in C
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t.
But what if we want to do something else when the condition is false? Here comes
the C else statement.
We can use the else statement with the if statement to execute a block of code when
the condition is false.
The if-else statement consists of two blocks, one for false expression and one for
true expression.
Syntax of if else in C
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
4
Example of if-else
// C program to illustrate If statement
#include <stdio.h>
int main()
{
int i = 20;
if (i < 15) {
printf("i is smaller than 15");
}
else {
printf("i is greater than 15");
}
return 0;
Output
5
i is greater than 15
The block of code following the else statement is executed as the condition present
in the if statement is false.
Another Example:
int time = 20;
if (time < 18)
{
printf("Good day.");
} else
{
printf("Good evening.");
}
// Outputs "Good evening."
3. Nested if-else in C
A nested if in C is an if statement that is the target of another if statement. Nested if
statements mean an if statement inside another if statement. Yes, C allow us to
nested if statements within if statements, i.e, we can place an if statement inside
another if statement.
}
}
return 0;
}
Output
i is smaller than 15
i is smaller than 12 too
4. if-else-if Ladder in C
The if else if statements are used when the user has to decide among multiple
options. The C if statements are executed from the top down. As soon as one of the
conditions controlling the if is true, the statement associated with that if is executed,
and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then
the final else statement will be executed. if-else-if ladder is similar to the switch
statement.
Syntax of if-else-if Ladder
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Output
i is 20
Another Example
int time = 22;
if (time < 10)
{
printf("Good morning.");
} else if (time < 18)
{
printf("Good day.");
} else if (time < 21)
{
printf("Good evening.");
} else
{
printf("Good Neight.");
}
Example explained
In the example above, time (22) is greater than 10, so the first condition is false. The next
condition, in the else if statement, is also false, so we move on to the else condition
since condition1 and condition2 is both false - and print to the screen "Good evening".
8
However, if the time was 14, our program would print "Good day."
5. switch Statement in C
The switch case statement is an alternative to the if else if ladder that can be used to
execute the conditional code based on the value of the variable specified in the
switch statement.
The switch block consists of cases to be executed based on the value of the switch
variable.
Syntax of switch
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
Flowchart of switch in C
9
The example below uses the weekday number to calculate the weekday name:
Example
#include<stdio.h>
Int main()
{
int day = 4;
switch (day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
}
Return (0);
}
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more
testing.
10
A break can save a lot of execution time because it "ignores" the execution of all the rest of the
code in the switch block.
Example
int day = 4;
switch (day) {
case 6:
printf("Today is Saturday");
break;
case 7:
printf("Today is Sunday");
break;
default:
printf("Looking forward to the Weekend");
}
Output
Case 2 is executed
6. Conditional Operator in C
The conditional operator is used to add conditional code in our program. It is similar
to the if-else statement. It is also known as the ternary operator as it works on three
operands. It is often used to replace simple if else statements:
11
Example
int time = 20;
(time < 18) ? printf("Good day.") : printf("Good evening.");
Output
Value of var when flag is 0: 25
Value of var when flag is NOT 0: -25
if (myNum % 2 == 0)
{
printf("%d is even.\n", myNum);
}
else
{
printf("%d is odd.\n", myNum);
}
7. Jump Statements in C
These statements are used in C for the unconditional flow of control throughout the
functions in a program. They support four types of jump statements:
A) break
This loop control statement is used to terminate the loop. As soon as
the break statement is encountered from within a loop, the loop iterations stop there,
and control returns from the loop immediately to the first statement after the loop.
Syntax of break
break;
Basically, break statements are used in situations when we are not sure about the
actual number of iterations for the loop or we want to terminate the loop based on
some condition.
13
Example of break
// C program to illustrate
// statement
#include <stdio.h>
void findElement(int arr[], int size, int key)
{
// loop to traverse array and search for key
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
printf("Element found at position: %d",
(i + 1));
break;
}
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
// no of elements
14
int n = 6;
// key to be searched
int key = 3;
// Calling function to find the key
findElement(arr, n, key);
return 0;
}
Output
Element found at position: 3
B) continue
• This loop control statement is just like the break statement.
The continue statement is opposite to that of the break statement, instead of
terminating the loop, it forces to execute the next iteration of the loop.
• As the name suggests the continue statement forces the loop to continue or
execute the next iteration. When the continue statement is executed in the
loop, the code inside the loop following the continue statement will be
skipped and the next iteration of the loop will begin.
Syntax of continue
continue;
Example of continue
// C program to explain the use of continue statement
#include <stdio.h>
int main()
{
// loop from 1 to 10
for (int i = 1; i <= 10; i++)
{
// If i is equals to 6, continue to next iteration without printing
if (i == 6)
continue;
else
// otherwise print the value of i
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5 7 8 9 10
If you create a variable in if-else in C, it will be local to that if/else block only. You
can use global variables inside the if/else block. If the name of the variable you
15
created in if/else is as same as any global variable then priority will be given to the
`local variable`.
#include <stdio.h>
int main()
if (1) {
return 0;
Output
Before if-else block 0
if block 100
After if block 0
C) goto
The goto statement in C also referred to as the unconditional jump statement can be
used to jump from one point to another within a function.
Syntax of goto
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;
In the above syntax, the first line tells the compiler to go to or jump to the statement
marked as a label. Here, a label is a user-defined identifier that indicates the target
statement. The statement immediately followed after ‘label:’ is the destination
statement. The ‘label:’ can also appear before the ‘goto label;’ statement in the
above syntax.
16
Examples of goto
// C program to print numbers from 1 to 10 using goto statement
#include <stdio.h>
// function to print numbers from 1 to 10
int main()
{
int n = 1;
label:
printf("%d ", n);
n++;
if (n <= 10)
goto label;
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
D) return
• The return in C returns the flow of the execution to the function from where it
is called.
• This statement does not mandatorily need any conditional statements.
• As soon as the statement is executed, the flow of the program stops
immediately and returns the control from where it was called.
• The return statement may or may not return anything for a void function, but
for a non-void function, a return value must be returned.
Flowchart of return
Syntax of return
return [expression];
17
Example of return
// C code to illustrate return statement
#include <stdio.h>
// non-void return type function to calculate sum
int SUM(int a, int b)
{
int s1 = a + b;
return s1;
}
// returns void function to print
void Print(int s2)
{
printf("The sum is %d", s2);
return;
}
int main()
{
int num1 = 10;
int num2 = 10;
int sum_of = SUM(num1, num2);
Print(sum_of);
return 0;
}
Output
The sum is 20
18
C – Loops
Loops in programming are used to repeat a block of code until the specified
condition is met.
A loop statement allows programmers to execute a statement or group of
statements multiple times without repetition of code.
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
while first Initializes, then condition checks, and then executes the
loop body, and updating can be inside the body.
do-while do-while first executes the body and then the condition check is
loop done.
for Loop
for loop in C programming is a repetition control structure that allows
programmers to write a loop that will be executed a specific number of times.
for loop enables programmers to perform n number of steps together in a single
line.
Syntax:
for (initialize expression; test expression; update expression) //for(int i=1; i<=10; i++) 11
{ {
// printf(“Hello…”);
// body of for loop }
//
}
20
Example:
int n=15;
for(int i = 0; i < n; ++i)
{
printf("Body of for loop which will execute till n");
}
Example
Hello World
Hello World
Another example :
Write a C program to create table for first 10 natural numbers;
While Loop
While loop does not depend upon the number of iterations.
In for loop the number of iterations was previously known to
us but in the While loop, the execution is terminated on the
basis of the test condition.
If the test condition will become false then it will break from
the while loop else body will be executed.
Syntax:
initialization_expression; int i=1;
while (test_expression) while(i<5)
{ {
// body of the while loop printf(“hello”);
i++; //i=i+1
}
update_expression;
}
23
// update expression
i++;
}
return 0;
}
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
24
do-while Loop
The do-while loop is similar to a while loop but the only difference
lies in the do-while loop test condition which is tested at the end of
the body.
In the do-while loop, the loop body will execute at least
once irrespective of the test condition.
Syntax:
initialization_expression;
do
{
// body of do-while loop
//update_expression;
} while (test_expression);
25
// Update expression
i++;
return 0;
}
Output
Hello World
Nested Loops
It is also possible to place a loop inside another loop. This is called a nested loop.
The "inner loop" will be executed one time for each iteration of the "outer loop":
26
Example
int i, j;
// Outer loop
for (i = 1; i <= 2; ++i)
{
printf("Outer: %d\n", i); // Executes 2 times
// Inner loop
for (j = 1; j <= 3; ++j)
{
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
}
Example
int number = 2;
int i;
return 0;
Name Description
Name Description
Example
int i;
Continue
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and
continues with the next iteration in the loop.
Example
int i;
{
if (i == 4)
continue;
printf("%d\n", i);
}
Break Example
int i = 0;
Continue Example
int i = 0;
Infinite Loop
An infinite loop is executed when the test expression never
becomes false and the body of the loop is executed
repeatedly. A program is stuck in an Infinite loop when the
condition is always true. Mostly this is an error that can be
resolved by using Loop Control statements.
Using for loop:
29
return 0;
}
Output
This loop will run forever.
This loop will run forever.
This loop will run forever.
...
int main()
{
while (1)
printf("This loop will run forever.\n");
return 0;
}
Output
This loop will run forever.
This loop will run forever.
This loop will run forever.
...
30
return 0;
}
Output
This loop will run forever.
This loop will run forever.
This loop will run forever.
...