C Theory UNIT3
C Theory UNIT3
1. if in C
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
}
#include <stdio.h>
int main()
int i = 10;
if (i > 15) {
}
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
}
// C program to illustrate If statement
#include <stdio.h>
int main()
{
int i = 20;
if (i < 15) {
int main()
{
int i = 10;
if (i == 10) {
// First if statement
if (i < 15)
printf("i is smaller than 15\n");
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
else {
if (i == 20) {
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 22)
printf("i is smaller than 22 too\n");
else
printf("i is greater than 25");
}
}
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.
int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
Output
i is 20
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;
}
Note: The switch expression should evaluate to either integer or character. It
cannot evaluate any other data type.
int main()
{
// variable to be used in switch statement
int var = 2;
return 0;
}
Output
Case 2 is executed
6.For Loop
When you know exactly how many times you want to loop through a block of
code, use the for loop instead of a while loop:
Syntax
for (expression 1; expression 2; expression 3) {
// code block to be executed
}
Expression 1 is executed (one time) before the execution of the code block.
Expression 3 is executed (every time) after the code block has been executed.
Example
int i;
7.While Loop
The while loop loops through a block of code as long as a specified condition
is true:
Syntax
while (condition) {
// code block to be executed
}
In the example below, the code in the loop will run, over and over again, as long
as a variable (i) is less than 5:
Example
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
Syntax
do {
// code block to be executed
}
while (condition);
The example below uses a do/while loop. The loop will always be executed at
least once, even if the condition is false, because the code block is executed
before the condition is tested:
Example
int i = 0;
do {
printf("%d\n", i);
i++;
}
while (i < 5);
1. Nested for Loop
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":
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)
}
}
++ pehle hi for loop
Here’s an example of a nested while loop in C. This program prints a multiplication table
from 1 to 5:
#include <stdio.h>
int main() {
int outer = 1;
return 0;
}
In this example:
For each value of outer, the inner while loop runs from
inner = 1 to inner = 10, printing the multiplication table
for that specific number.
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
do {
// Outer loop body
do {
// Inner loop body
} while (inner_condition);
// Updation condition/ Code after the inner loop
} while (outer_condition);
Example:
#include <stdio.h>
int main(){
int i,j;
i=1;
printf("Here is your pattern:\n");
do{ j=1;
do{
printf("* ");
j++;
}while(j<=5);
printf("\n");
i++;
}while(i<=5);
return 0;
}
Break
Break was used to "jump out" of a switch statement.
Example
int i;
https://www.w3schools.com/c/c_break_continue.php
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;
Recursion Example
Adding two numbers together is easy to do, but adding a range of numbers is
more complicated. In the following example, recursion is used to add a range of
numbers together by breaking it down into the simple task of adding two
numbers:
Example
int sum(int k);
int main() {
int result = sum(10);
printf("%d", result);
return 0;
}
int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}
Example Explained
When the sum() function is called, it adds parameter k to the sum of all numbers
smaller than k and returns the result. When k becomes 0, the function just
returns 0. When running, the program follows these steps:
10 + sum(9)
10 10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 (8) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
Since the function does not call itself when k is 0, the program stops there and
returns the result.
In C, a function specifies the modes of parameter passing to it. There are two ways to specify
function calls: call by value and call by reference in C. In call by value, the function parameters
gets the copy of actual parameters which means changes made in function parameters did not
reflect in actual parameters. In call by reference, the function parameter gets reference of actual
parameter which means they point to similar storage space and changes made in function
parameters will reflect in actual parameters.
Introduction
Suppose you have a file and someone wants the information present in the file. So to protect
from alteration in the original file, you give a copy of your file to them and if you want the
changes done by someone else in your file then you have to give them your original file. In C
also, if we want the changes done by function to reflect in the original parameters also, then we
pass the parameter by reference, and if we don't want the changes in the original parameter, then
we pass the parameters by value. We get to know about both call by value and call by reference
in c and their differences in upcoming sections.
Call by Value in C
Calling a function by value will cause the program to copy the contents of an object passed into a
function. To implement this in C, a function declaration has the following form: [return type]
functionName([type][parameter name],...).
int main(){
int x = 10;
int y = 11;
printf("Values before swap: x = %d, y = %d\n", x,y);
swap(x,y);
printf("Values after swap: x = %d, y = %d", x,y);
}
Output:
Call by Reference in C
Calling a function by reference will give function parameter the address of original parameter
due to which they will point to same memory location and any changes made in the function
parameter will also reflect in original parameters. To implement this in C, a function declaration
has the following form: [return type] functionName([type]* [parameter name],...).
int main(){
int x = 10;
int y = 11;
printf("Values before swap: x = %d, y = %d\n", x,y);
swap(&x,&y);
printf("Values after swap: x = %d, y = %d", x,y);
}
Output:
We can observe in function parameters instead of using int x,int y we used int *x,int *y and in
function call instead of giving x,y, we give &x,&y this methodology is call by reference as we
used pointers as function parameter which will get original parameters' address instead of their
value. & operator is used to give address of the variables and * is used to access the memory
location that pointer is pointing. As the function variable is pointing to the same memory
location as the original parameters, the changes made in swap() reflect in main() which we can
see in the above output.
https://www.scaler.com/topics/c/call-by-value-and-call-by-reference-in-c/