UNIT-II STARTING PART
UNIT-II STARTING PART
if(expression)
Example:
#include <stdio.h>
int main()
{
int i = 10;
if (i > 15)
{
printf("10 is greater than 15");
}
printf("I am Not in if");
}
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.
If else statement:
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. The if-else
statement consists of two blocks, one for false expression and one for true
expression.
Syntax:
The general form of a simple if...else statement is,
if(expression)
{
statement block1;
}
else
{
statement block2;
}
If the expression is true, the statement-block1 is executed, else statement-
block1 is skipped and statement-block2 is executed.
Flowchart of if-else Statement:-
Example:
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 18;
if (x > y )
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
}
Output:
Y is greater than x.
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,
both C and C++ allow us to nested if statements within if statements, i.e, we
can place an if statement inside another if statement.
Syntax:
The general form of a nested if...else statement is,
if( expression )
{
if( expression1 )
{
statement block1;
}
else
{
statement block2;
}
}
else
{
statement block3;
}
if expression is false then statement-block3 will be executed, otherwise the
execution continues and enters inside the first if to perform the check for the
next if block, where if expression 1 is true the statement-block1 is executed
otherwise statement-block2 is executed.
Flowchart for Nested if else statement:
Example:
#include <stdio.h>
void main( )
{
int a, b, c;
printf("Enter 3 numbers...");
scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}
}
}
else if ladder:-
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:
The general form of else-if ladder is,
if(expression1)
{
statement block1;
}
else if(expression2)
{
statement block2;
}
else if(expression3 )
{
statement block3;
}
else
default statement;
The expression is tested from the top(of the ladder) downwards. As soon as
a true condition is found, the statement associated with it is executed.
Flowchart of if-else-if Ladder:
Example:
#include <stdio.h>
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
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. If there is no match,
then default block is executed(if present).
Syntax:
The general form of switch statement is,
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
1. The expression (after switch keyword) must yield an integer value i.e the
expression should be an integer or a variable or an expression that
evaluates to an integer.
2. The case label values must be unique.
3. The case label must end with a colon(:)
4. The next line, after the case statement, can be any valid C statement.
Flowchart of switch:-
Example:
#include<stdio.h>
int main()
{
int day;
printf(“Enter a day:”);
scanf(“%d”,&day);
switch(day)
{
Case1:printf(“Sunday”);
Case2:printf(“Monday\n”);
Case3:printf(“Tuesday\n”);
Case4:printf(“Wednesday\n”);
Case5:printf(“Thrusday\n”);
Case6:printf(“Friday\n”);
Case7:printf(“Saturday\n”);
break;
default:printf(“Invalid choice\n”);
}
return 0;
}
Output:
Enter a day: 4
Wednesday
Thursday
Friday
Saturday
Conditional Operator(?:):
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.
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
printf(“enter the values:”);
scanf(“%d%d”,&a,&b);
(a>b)?printf(“a is greater than b”):printf(“a is not greater than b”);
return 0;
}
Output:
Enter the values: 20 10
a is greater than b
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:
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.
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;
Flowchart of Continue:
Example:-
#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 6 7 8 9 10
goto :
The goto statement in C/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.
Example:
#include <stdio.h>
int main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table;
}
Output:
Enter the number whose table you want to print?10
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100
Loops in C
During programming, sometimes we might need to execute a certain code
statement again and again. We can write the code statement as many times as
we need it to execute but that would be very inefficient, because what if you
want a code statement to execute a 100 times? This is why we use loops.
In this kind of loop, the condition is checked before executing the loop's body.
So, if the condition is never true, it won't execute even once. For
example, for and while loop.
In this kind of loop, the condition is checked after the loop's body is executed,
i.e., in the end. Hence, even if the condition is not fulfilled, this loop will
execute one time. The do-while loop is an example of exit controlled loop.
Types of Loop in C
1. while loop
2. for loop
3. do while loop
while loop in C:
variable initialization;
while(condition)
{
statements;
variable increment or decrement;
}
The following flowchart shows the flow of execution when we use a while loop.
Here, we can see that firstly, we initialize our iterator. Then we check the
condition of while loop. If it is false, we exit the loop and if it is true, we enter
the loop. After entering the loop, we execute the statements inside
the while loop, update the iterator and then again check the condition. We do
the same thing unless the condition is false.
Program to print first 10 natural numbers using while loop
#include<stdio.h>
void main( )
{
int x;
x = 1;
while(x <= 10)
{
printf("%d\t", x);
/* below statement means, do x = x+1, increment x by 1 */
x++;
}
}
Output:
1 2 3 4 5 6 7 8 9 10
for loop in C
We first initialize our iterator. Then we check the condition of the loop. If it
is false, we exit the loop and if it is true, we enter the loop. After entering the
loop, we execute the statements inside the for loop, update the iterator and then
again check the condition. We do the same thing unless the test condition
returns false.
Program to print first 10 natural numbers using for loop
#include<stdio.h>
void main( )
{
int x;
for(x = 1; x <= 10; x++)
{
printf("%d\t", x);
}
}
Output:
1 2 3 4 5 6 7 8 9 10
We can also have nested for loops, i.e one for loop inside another for loop in C
language. This type of loop is generally used while working with multi-
dimensional arrays.
Syntax: Nested for loops
for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
{
statement ;
}
}
do while loop in C
In some situations it is necessary to execute body of the loop once before testing
the condition. Such situations can be handled with the help of do-while loop.
The do statement evaluates the body of the loop first and at the end, the
condition is checked using while statement. It means that the body of the loop
will be executed at least once, even though the starting condition inside while is
initialized to be false. General syntax is,
do
{
…………..
…………..
}
While(condition);
Following is the flowchart for do-while loop:
We initialize our iterator. Then we enter body of the do-while loop. We execute
the statement and then reach the end. At the end, we check the condition of the
loop. If it is false, we exit the loop and if it is true, we enter the loop. We keep
repeating the same thing unless the condition turns false.
int main()
{
int a, i;
a = 5;
i = 1;
do
{
printf("%d\t", a*i);
i++;
}
while(i <= 10);
}
.....
.....
}