Paper: Introduction Programming Language using C
Paper ID: 20105
Paper Code: BCA 105
DR. VARUN TIWARI
(ASSOCIATE PROFESSOR)
(DEPARTMENT OF COMPUTER SCIENCE)
BOSCO TECHNICAL TRAINING SOCIETY,
DON BOSCO TECHNICAL SCHOOL, OKHLA ROAD , NEW DELHI
C Construct
Objectives
In this unit you will learn:
1. To understand about conditional statement.
2. To learn about if statement , if else , nested if else , if elseif else etc.
3. To learn about break and continue statement.
4. To use of switch statement in C.
5. To learn about Loop in C.
6. To learn about for loop in C.
7. To learn about while loop in C.
8. To learn about Do While in C (Entry and Exit control loop) in C.
Conditional Statements: In C language to making a decision and control according to
given condition are used to Conditional Statements. It execute sequentially when there is
no condition around the statements. If you If you placed one or more condition for a block
of statements, the execution flow may change based on the result evaluated by the
condition. It’s process called decision making in C language.
1. If Statement
2. If else Statement
3. Multiple if else Statement
4. Nested if else
5. If else if else Statement
6. switch Statement
7. break and continue Statement
8. return Statement
9. Go to Statement
Looping Statements: Looping statements are used to repeat a section of code a number of
times or until some condition occurs. For example, loops are used to count the number of
words in a document or print the value 1 to 10 until the condition is true. There are three
types of looping statement is used in C Language.
1. For Loop
2. While Loop
3. Do-While Loop
If Statement: The if statement allows us to put some decision-making into our programs. The
general form of the if statement is:
Syn : if (condition/expression)
statement;
If the condition is true (nonzero), the statement will be executed. If the condition is false (0),
the statement will not be executed. For example, suppose we are writing a voting
program. At the end, if the person age is greater than eaual to 18 then the condition are
true otherwise the condition is false. If the condition is true then, we want to print a
message “you are eligible for voting”. In C language, this program is written:
Example: if (age>=18)
printf("You are eligible for votingn”);
Flow Chart of If Statement:
If else Statement: The if else statement allows us to put some decision-making into our programs.
If the condition is true then first statement execute otherwise second statement execute which is
under the else condition. The general form of the if statement is:
Syn : if (condition/expression)
statement 1;
else
statement 2;
If the condition is true (non zero), the statement 1 will be executed. If the condition is false (0),
the statement 2 will be executed. For example, suppose we are writing a voting program. At the
end, if the person age is greater than eaual to 18 then the condition are true otherwise the
condition is false. If the condition is true then, first statement execute “you are eligible for voting”
else if the condition false then the second statement will be execute with message “you are not
eligible for voting”. In C language, this program is written:
Example: if (age>=18)
printf("You are eligible for votingn”);
Else
printf(“you are not eligible for votingn”);
Flow Chart of If-else Statement:
Multiple if else with in else Statement: Multiple statements are run when the if condition is true.if multiple
statements are to be executed , then they must be place within a pair of braces.
Syn : if (condition/expression)
{ statement;
statement; }
else
{ statement;
statement; }
Final Statement;
If the condition is true (nonzero), the multiple statement block of if will be executed. If the condition is false
(0), else statement will be executed.
Example: if (a <= 10)
{ c=a+b;
d = a-b; }
else
{ c=a/b;
d=a*b; }
printf(“The output is -: %d%d “, c,d);
Nested if else: In the case of nested if else if the condition in the first statement is true then first if will be
executed, if it is false , then the condition in the second if is checked. If it is false as well , then the second
else is executed.
Syn : if (condition/expression)
statement;
else
{
if (condition/expression)
Statement ;
Else
Statement;
}
Example: if (a < 10)
printf(“Output is -: %d”, a);
else
{
If (a==b)
printf(“Output is -: %d”, a);
else
printf(“output is --: %d”,b);
If else if condition :- The if elseif statement is useful when you need to check multiple conditions within the
program, nesting of if-else blocks can be avoided using elseIf statement.
Syn :
if (condition1)
Statement;
else if(condition2)
Statement;
else if (condition3)
Statement;
else
Statement;
Example: if (per > =75)
printf(“Grade A”);
elseif (per >=60 && per<75)
printf(“Grade B”);
else
printf(“fail”);
}
Switch Statement :- The control statement that allows us to make a decision from the number of choices is called a switch or more
correctly a switch – case – default , since these three keywords go together to make up the control statement.
Syn :
Switch (expression)
{
Case constant1:
Statement;
Case constant2;
Statement;
Case constant3;
Statement;
Default:
Statement;
}
Example: i=3;
switch(i) {
case 1: printf(“One”);
case 2: printf(“Two”);
case 3: printf(“Three”);
default: printf(“wrong value”);
}
Break and Continue Statement :-
Break Statement: Break statement provides an early exit from for, while, and do, just as from switch. A break causes the innermost
enclosing loop or switch to be exited immediately. The only way to exit this loop is through a break statement.
Syntax of break-:
(using while with break)
while (cond/expr){
statement
if(con/expr) {
break;
}}
(using for with break)
for(initialization ;con/exp; incr/decr)
{
statement;
if (cond/exp) {
break; }
statement;
}
(using do while with break)
do
{ statement;
if(con/exp) {
break;
} statement;
} while(con/exp);
Continue Statement: The continue statement passes control to the next iteration of the nearest enclosing
do, for, or while statement in which it appears, bypassing any remaining statements in the do, for, or while
statement body.
Syntax of continue-:
(using while with continue)
while (cond/expr){
statement
if(con/expr) {
continue;
}}
(using do while with continue)
do
{ statement;
if(con/exp) {
continue;
} statement;
} while(con/exp);
(using for with continue)
for(initialization ;con/exp;incr/decr)
{
statement;
if (cond/exp) {
continue; }
statement;
}
Goto Statement:- Goto Statement is a jump statement , it is used to transfer program control from one part of function / condition
/loop etc to another. This statement provide a facility to unconditional jump. I have used the word unconditionally jump because
there is no restriction on control transfer. You can transfer program control from one position to any position within a function. Many
programmers uses goto to gain full control on their program.
Syntax:
Goto label name;
Statement;
Label name: statement;
Example:
int i ;
If (i<10)
goto one;
else
printf(“you are wrong”);
one:
printf(“yes I is less than 10”);
For Loop:- Loop is used to repeat a block of code until the condition is true. It is a repetition control structure that allows you to
execute your expression a specific number of times.The for loop is an important iteration technique in any programming language.
Much different in syntax from its cousins, the while and do while loops, it is much more common for building loops when the
number of iterations is already known. The next program block demonstrates a simple for loop. The for loop statement is busier
than the other loops I’ve shown you. A single for loop statement contains three separate expressions, as described in the following
bulleted list.
• Variable initialization
• Conditional expression
• Increment/decrement
Syntax:
for (expr1; expr2; expr3)
{
statement
}
Example:
int i ;
for (i=1;i<=10;i++)
{
printf(“%d”,i);
}
Nested For Loop:- Multiple for loop involve to repeat a block of code until the condition is true.
Syntax:
for (expr1; expr2; expr3)
{
Statement;
For (expr1;expr2;expr3)
{
statement
}
}
Example:
int i ,j;
for (i=1;i<=10;i++)
{
for(j=1;j<=10;j++)
{
printf(“%d%d”,I,j);
}
}
While Loop:- This loop is also known as pre-tested loop. It allows us to executed your code many times depend on the situation /
condition.
Syntax:
expr1;
while (expr2) {
statement
expr3;
}
Variable intilization;
While (cond/expr)
{
Statement;
Incr/decr;
}
Example:
int i ;
i=1;
while(i<10)
{
printf(“%d”,i);
i++;
}
Do while:- Do while loop is similar to while loop. It is called Entry and Exit Control Loop. There is one difference between the working
of while and dowhile loops. While tests the condition before executing any of the statements within the while loops but do while
tests the condition after having executed the statement with in the loop.
Syntax:
do
{
Statement;
Incr/decr;
}while (cond/expr);
Example:
int i ;
do
{
printf(“%d”,i);
i++;
}while(i<=10);
THANK YOU

C Constructs (C Statements & Loop)

  • 1.
    Paper: Introduction ProgrammingLanguage using C Paper ID: 20105 Paper Code: BCA 105 DR. VARUN TIWARI (ASSOCIATE PROFESSOR) (DEPARTMENT OF COMPUTER SCIENCE) BOSCO TECHNICAL TRAINING SOCIETY, DON BOSCO TECHNICAL SCHOOL, OKHLA ROAD , NEW DELHI
  • 2.
  • 3.
    Objectives In this unityou will learn: 1. To understand about conditional statement. 2. To learn about if statement , if else , nested if else , if elseif else etc. 3. To learn about break and continue statement. 4. To use of switch statement in C. 5. To learn about Loop in C. 6. To learn about for loop in C. 7. To learn about while loop in C. 8. To learn about Do While in C (Entry and Exit control loop) in C.
  • 4.
    Conditional Statements: InC language to making a decision and control according to given condition are used to Conditional Statements. It execute sequentially when there is no condition around the statements. If you If you placed one or more condition for a block of statements, the execution flow may change based on the result evaluated by the condition. It’s process called decision making in C language. 1. If Statement 2. If else Statement 3. Multiple if else Statement 4. Nested if else 5. If else if else Statement 6. switch Statement 7. break and continue Statement 8. return Statement 9. Go to Statement
  • 5.
    Looping Statements: Loopingstatements are used to repeat a section of code a number of times or until some condition occurs. For example, loops are used to count the number of words in a document or print the value 1 to 10 until the condition is true. There are three types of looping statement is used in C Language. 1. For Loop 2. While Loop 3. Do-While Loop
  • 6.
    If Statement: Theif statement allows us to put some decision-making into our programs. The general form of the if statement is: Syn : if (condition/expression) statement; If the condition is true (nonzero), the statement will be executed. If the condition is false (0), the statement will not be executed. For example, suppose we are writing a voting program. At the end, if the person age is greater than eaual to 18 then the condition are true otherwise the condition is false. If the condition is true then, we want to print a message “you are eligible for voting”. In C language, this program is written: Example: if (age>=18) printf("You are eligible for votingn”);
  • 7.
    Flow Chart ofIf Statement:
  • 10.
    If else Statement:The if else statement allows us to put some decision-making into our programs. If the condition is true then first statement execute otherwise second statement execute which is under the else condition. The general form of the if statement is: Syn : if (condition/expression) statement 1; else statement 2; If the condition is true (non zero), the statement 1 will be executed. If the condition is false (0), the statement 2 will be executed. For example, suppose we are writing a voting program. At the end, if the person age is greater than eaual to 18 then the condition are true otherwise the condition is false. If the condition is true then, first statement execute “you are eligible for voting” else if the condition false then the second statement will be execute with message “you are not eligible for voting”. In C language, this program is written: Example: if (age>=18) printf("You are eligible for votingn”); Else printf(“you are not eligible for votingn”);
  • 11.
    Flow Chart ofIf-else Statement:
  • 14.
    Multiple if elsewith in else Statement: Multiple statements are run when the if condition is true.if multiple statements are to be executed , then they must be place within a pair of braces. Syn : if (condition/expression) { statement; statement; } else { statement; statement; } Final Statement; If the condition is true (nonzero), the multiple statement block of if will be executed. If the condition is false (0), else statement will be executed. Example: if (a <= 10) { c=a+b; d = a-b; } else { c=a/b; d=a*b; } printf(“The output is -: %d%d “, c,d);
  • 18.
    Nested if else:In the case of nested if else if the condition in the first statement is true then first if will be executed, if it is false , then the condition in the second if is checked. If it is false as well , then the second else is executed. Syn : if (condition/expression) statement; else { if (condition/expression) Statement ; Else Statement; } Example: if (a < 10) printf(“Output is -: %d”, a); else { If (a==b) printf(“Output is -: %d”, a); else printf(“output is --: %d”,b);
  • 22.
    If else ifcondition :- The if elseif statement is useful when you need to check multiple conditions within the program, nesting of if-else blocks can be avoided using elseIf statement. Syn : if (condition1) Statement; else if(condition2) Statement; else if (condition3) Statement; else Statement; Example: if (per > =75) printf(“Grade A”); elseif (per >=60 && per<75) printf(“Grade B”); else printf(“fail”); }
  • 26.
    Switch Statement :-The control statement that allows us to make a decision from the number of choices is called a switch or more correctly a switch – case – default , since these three keywords go together to make up the control statement. Syn : Switch (expression) { Case constant1: Statement; Case constant2; Statement; Case constant3; Statement; Default: Statement; } Example: i=3; switch(i) { case 1: printf(“One”); case 2: printf(“Two”); case 3: printf(“Three”); default: printf(“wrong value”); }
  • 32.
    Break and ContinueStatement :- Break Statement: Break statement provides an early exit from for, while, and do, just as from switch. A break causes the innermost enclosing loop or switch to be exited immediately. The only way to exit this loop is through a break statement. Syntax of break-: (using while with break) while (cond/expr){ statement if(con/expr) { break; }} (using for with break) for(initialization ;con/exp; incr/decr) { statement; if (cond/exp) { break; } statement; }
  • 33.
    (using do whilewith break) do { statement; if(con/exp) { break; } statement; } while(con/exp); Continue Statement: The continue statement passes control to the next iteration of the nearest enclosing do, for, or while statement in which it appears, bypassing any remaining statements in the do, for, or while statement body. Syntax of continue-: (using while with continue) while (cond/expr){ statement if(con/expr) { continue; }}
  • 34.
    (using do whilewith continue) do { statement; if(con/exp) { continue; } statement; } while(con/exp); (using for with continue) for(initialization ;con/exp;incr/decr) { statement; if (cond/exp) { continue; } statement; }
  • 39.
    Goto Statement:- GotoStatement is a jump statement , it is used to transfer program control from one part of function / condition /loop etc to another. This statement provide a facility to unconditional jump. I have used the word unconditionally jump because there is no restriction on control transfer. You can transfer program control from one position to any position within a function. Many programmers uses goto to gain full control on their program. Syntax: Goto label name; Statement; Label name: statement; Example: int i ; If (i<10) goto one; else printf(“you are wrong”); one: printf(“yes I is less than 10”);
  • 42.
    For Loop:- Loopis used to repeat a block of code until the condition is true. It is a repetition control structure that allows you to execute your expression a specific number of times.The for loop is an important iteration technique in any programming language. Much different in syntax from its cousins, the while and do while loops, it is much more common for building loops when the number of iterations is already known. The next program block demonstrates a simple for loop. The for loop statement is busier than the other loops I’ve shown you. A single for loop statement contains three separate expressions, as described in the following bulleted list. • Variable initialization • Conditional expression • Increment/decrement Syntax: for (expr1; expr2; expr3) { statement } Example: int i ; for (i=1;i<=10;i++) { printf(“%d”,i); }
  • 44.
    Nested For Loop:-Multiple for loop involve to repeat a block of code until the condition is true. Syntax: for (expr1; expr2; expr3) { Statement; For (expr1;expr2;expr3) { statement } } Example: int i ,j; for (i=1;i<=10;i++) { for(j=1;j<=10;j++) { printf(“%d%d”,I,j); } }
  • 46.
    While Loop:- Thisloop is also known as pre-tested loop. It allows us to executed your code many times depend on the situation / condition. Syntax: expr1; while (expr2) { statement expr3; } Variable intilization; While (cond/expr) { Statement; Incr/decr; } Example: int i ; i=1; while(i<10) { printf(“%d”,i); i++; }
  • 48.
    Do while:- Dowhile loop is similar to while loop. It is called Entry and Exit Control Loop. There is one difference between the working of while and dowhile loops. While tests the condition before executing any of the statements within the while loops but do while tests the condition after having executed the statement with in the loop. Syntax: do { Statement; Incr/decr; }while (cond/expr); Example: int i ; do { printf(“%d”,i); i++; }while(i<=10);
  • 50.