Professional Documents
Culture Documents
PiC_3_mod2_2
PiC_3_mod2_2
Viji R
Assistant Prof in ECE
Feb 2024
Contents
Decision making structures in C: if-else, switch statements
Control flow statements-for, while, do-while
Simple programs for familiarisation of if-else, switch and
control flow statements
Decision making using ‘if’
If the boolean expression
evaluates to true, then the
block of code inside the if
statement will be executed.
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
if…else
If the boolean expression
evaluates to true, then the
if block of code will be
executed, otherwise else
block of code will be
executed.
Example 2
#include <stdio.h>
int main ()
{
int a = 100;
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
else
{
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
if...else if...else Statement
Example 3
int main ()
{ else if( a == 30 )
{
printf("Value of a is 30\n" );
int a = 100;
}
if( a == 10 )
{ else
{
printf("Value of a is 10\n" );
printf("No value matches\n" );
} }
else if( a == 20 )
printf("value of a is: %d\n", a );
{ return 0;
printf("Value of a is 20\n" ); }
}
1.Find largest among two number
#include<stdio.h>
int main()
{
int num1,num2;
printf(“Enter the first number : \n”);
scanf(“%d”,&num1);
printf(“Enter the first number : \n”);
scanf(“%d”,&num2);
if (num1 ==num2)
printf(“The numbers are equal”);
else
printf(“The number largest number is : %d” , num2);
}
2.Check whether the character is upper case or lower case
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if(ch >= 'A' && ch <= 'Z’)
{
printf("'%c' is uppercase alphabet.", ch);
}
else if(ch >= 'a' && ch <= 'z’)
{
printf("'%c' is lowercase alphabet.", ch);
}
else
{ printf("'%c' is not an alphabet.", ch);
}
return 0;
}
2. Check whether the character is upper case or lower case
Another way
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if(ch >= 65 && ch <= 90)
{
printf("'%c' is uppercase alphabet.", ch);
}
else if(ch >=97 && ch <=122)
{
printf("'%c' is lowercase alphabet.", ch);
}
else
{ printf("'%c' is not an alphabet.", ch);
}
return 0; ASCII values of A-Z are 65-90 and
} a-z are 97-122
3. Read a character from user and convert its case (Upper case to lower
case/ lower case to upper case number)
#include <stdio.h>
int main()
{
char in,out;
int x;
printf("Enter an uppercase Character: ");
scanf("%c", &in);
/* x=in;
printf("\nAscii value = %d", x);
*/
out = in+32;
printf("\nIts Lowercase = %c", out);
return 0; ASCII values of A-Z are 65-90 and
} a-z are 97-122
SWITCH STATEMENT
Switch statement is one of decision-making control-flow statements. Just
like else if ladder, it is also used to make a decision among multiple choices.
switch statement has the following form:
switch(expression)
{
case exp-val-1:
statements block-1
break;
case exp-val-2:
statements block-2
break;
case exp-val-3:
statements block-3
break;
:
case exp-val-N:
statements block-N
break;
default:
default statements block
}
SWITCH STATEMENT
The keyword case is followed by an integer or a character constant. Each
constant in each case must be different from all the others.
If there is no default case, then the program simply falls through the
entire switch and continues with the next instruction (if any,) that
follows the closing brace of switch.
Switch can check any expression. Thus the following switch statements
are legal.
switch ( i + j * k )
switch ( 23 + 45 % 4 * k )
switch ( a < 4 && b > 7 )
#include <stdio.h>
int main()
{
char ch;
printf("Enter the character");
scanf("%c", &ch);
switch(ch)
{
case ‘A’:
case ‘a’:
case ‘E’:
case ‘e’:
case ‘I’:
case ‘i’:
case ‘O’:
case ‘o’:
case ‘U’:
case ‘u’:
printf(“The given Character is a vowel");
default:
printf(“The given character is not vowel");
}
return 0;
}
// 5. Program to create a simple calculator
switch(operator)
{
case '+’:
#include <stdio.h> printf("%d+ %d= %d",n1, n2, n1+n2);
int main() break;
{ case '-’:
char operator; printf("%d - %d= %d",n1, n2, n1-n2);
int n1, n2; break;
printf("Enter an operator (+, -, *, /): "); case '*’:
scanf("%c", &operator); printf("%d* %d= %d",n1, n2, n1*n2);
printf("Enter two operands: "); break;
scanf("%d %d",&n1, &n2); case '/’:
printf("%d/ %d = %d",n1, n2, n1/n2);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}
? : Operator
Conditional operator ? : can be used to replace if...else statements.
It is a ternary operator (it takes three operands)
It has the following general form:
Exp1 ? Exp2 : Exp3;
int a=5,b;
b=(a==5)?5:4;
printf("b=%d",b);
Control Flow Statements:
For Loop
While Loop, Do While Loop
Break and Continue statements.
(Simple programs covering control flow)
LOOPS
Exit-controlled loops
In this type loops body of loop executed first and then condition is checked.
Body of loop will execute at least once.
For example, do….while loop.
While LOOP
While statement is used to carry out looping operations, in which a group of
statements is executed repeatedly, until some condition has been satisfied.
Basic Syntax
while(condition)
{
statement(s)
}
void main( ) void main( )
{ {
int i = 1 ; int i = 1 ;
while ( i <= 10 ) do
{ {
printf ( "%d\n", i ) printf ( "%d\n", i );
i++ ; i++ ;
} } while (i <= 10 ) ;
} }
The do-while loop is pretty similar to while loop with an additional feature. It is
an exit-controlled loop that gives the user the provision to execute the loop at
least one.
In do…While First, the body of the loop is executed, then the condition is
checked which is in contrast to the while loop.
do
{
statement(s)
_________
_________
} while(condition);
main( )
main( ) {
{ do
while ( 4 < 1 ) {
printf ( "Hello there \n") ; printf ( "Hello there \n") ;
} } while ( 4 < 1 ) ;
}
for LOOP
The for allows us to specify three things about a loop in a single line:
Setting a loop counter to an initial value.
Testing the loop counter to determine whether its value has reached the
number of repetitions desired.
Increasing the value of loop counter each time the program segment within
the loop has been executed.
Syntax:
for( initialization ; condition ; increment / decrement )
{
statement(s)
}
Syntax:
for ( i = 1; i <=10 ;)
printf ( "%d",i++ );
for LOOP
Print numbers from 1 to 10 in different ways
main( )
{
main( ) int i ;
{ for ( i = 1 ; i <= 10 ;)
int i ; {
for ( i = 1 ; i <= 10 ; i = i + 1 ) printf ( "%d\n", i ) ;
printf ( "%d\n", i ) ; i = i + 1;
} }
}
main( )
main( ) {
{ int i = 1;
int i = 1 ; for (; i <= 10 ;)
for (; i <= 10 ; i = i + 1 ) {
printf ( "%d\n", i ) ; printf ( "%d\n", i ) ;
} i = i + 1;
}
}
for LOOP
Multiple statements can also be used in the incrementation expression of for loop;
i.e., for loop support increment (or decrement) two or more variables at the same
time.
Only one expression is allowed in the test expression. This expression may contain
several conditions linked together using logical operators.
BREAK Statement
Sometimes it becomes necessary to come out of the loop even before loop
condition becomes false then break statement is used.
Break statement is used inside loop and switch statements.
It cause immediate exit from that loop in which it appears and it is generally
written with condition.
It is written with the keyword as break. When break statement is encountered loop
is terminated and control is transferred to the statement, immediately after loop
without waiting to get back to conditional state.
BREAK Statement
void main( )
{
int i;
for ( i = 0; i < 10; i++)
{
if ( i == 3)
break;
printf ( “The number is%d\n", i);
}
printf ( “Out of loop”);
}
OUTPUT :
The number is 0
The number is 1
The number is 2
Out of loop
CONTINUE Statement
Continue statement is used for continuing next iteration of loop after
skipping some statement of loop.
The difference between break and continue is, when the break encountered
loop is terminated and it transfer to the next statement and when continue is
encounter control come back to the beginning position.
In while and do while loop after continue statement control transfer to the test
condition and then loop continue
In for loop after continue control transferred to the updating expression and
condition is tested.
CONTINUE Statement
void main( )
{
OUTPUT :
int i;
The number is 0
for ( i = 0; i < 10; i++) The number is 1
{ The number is 2
if ( i == 3) The number is 4
continue; The number is 5
printf ( “The numer is %d\n", i); The number is 6
} The number is 7
printf ( “Out of loop”); The number is 8
} The number is 9
Out of loop
Fibonacci Series: It is a series of numbers in which each number ( Fibonacci
number ) is the sum of the two preceding numbers.
#include<stdio.h>
int main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (i=1; i<=rows; ++i)
{
for (j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0
/* 9. Modify the above Program to print number triangle */
1
1 2
1 2 3
1 2 3 4
………
……….
Sum of digits algorithm
An Armstrong number is a number such that the sum of the nth power of its digits is equal
to the number itself, where n is the number of digits in the number (taken here to mean
positive integer)
#include <stdio.h>
#include <math.h>
Y No
i<N Stop
14. Program for Fibonacci Series: 0,1, 1, 2, 3, 5, 8, etc.
#include <stdio.h> 1. N=?
int main()
{ 2. a=0,b=1
int i, N, a = 0, b = 1, F; 3. Print a ,b
printf("Enter the number of terms: ");
scanf("%d", &N); 4. i=2
printf("Fibonacci Series: ");
5. F=a+b
for (i = 1; i <= N; ++i)
{ 6. Print F
printf("%d\t", a);
F = a+b; 7. a=b,b=F
a=b; 8. i=i+1
b=F;
} 9. If i<N go to step 5
return 0;
} 10. Stop
N! = 1 * 2 * 3 * …… N
Algorithm
1. Start
2. Read N
3. Initialize i = 1,F = 1
4. F=F*i
5. i=i+1
6. Repeat steps 4 and 5 until i > N
7. Print F
8. Stop
15: Program for factorial print