SRM INSTITUTE OF SCIENCE & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Mr. P.Gowsikraja M.E., (Ph.D.,)
Assistant Professor (Sr. G)
Department of Computer Science and Engineering,
SRM IST, Chennai.
18CSS101J – PROGRAMMING FOR PROBLEM SOLVING
UNIT II OPERATORS, CONTROL AND
LOOPING STATEMENTSUNIT II
2.1 Relational and logical Operators, Condition Operators, Operator Precedence
2.2 Expressions with pre / post increment Operator
2.3 Expression with conditional and assignment operators
2.4 If statement in expression - L value and R value in expression
2.5Control Statements – 2.5.1 Simple IF, if and else - else if
– 2.5.2 nested if, switch case - Iterations,
2.6 Conditional and Unconditional Branching – For loop - While loop - do while, goto, break, continue
2.7 Array - Initialization and Declaration- - Initialization: one Dimensional Array-Accessing,
Indexing one Dimensional Array Operations
- Array Programs – 1D
2.1 OPERATOR PRECEDENCE
Operator Precedence
 Operator Precedence is used to determine the order of
operators evaluated in an expression
 Every operator has precedence (Priority)
 Operator with higher precedence is evaluated first and the operator with least
precedence is evaluated last
 Associativityis used when two operators of same precedence appear in an
expression
 Determines the order of evaluation of those operators
 Associativity can be either Left to Right or Right to Left
2.1 OPERATOR PRECEDENCE
 Operators are listed in descending order of precedence
 An Expression can contain several operators with equal precedence
 Evaluation proceeds according to the associativity of the operator i.e.,
 From Right to Left (or)
 From Left to Right
 Note:Order of operations is not defined by the language
2.1 OPERATOR PRECEDENCE
2.2 Expressions with pre / post increment Operator
Expressions using Pre/Post Increment Operator
 Increment operators increase the value of the variable by one
 Decrement operators decrease the value of the variable by one
 Syntax
Increment operator: ++var_name; (or) var_name++; Decrement operator:
– -var_name; (or) var_name – -;
 Example
Increment operator : Decrement operator :
++ i ; – – i ;
i ++ ; i – – ;
2.2 Expressions with pre / post increment Operator
/* Expresssions using Pre-Increment Operator*/
#include<stdio.h>
int main( )
{
int x,i;
i=10;
x=++i;
printf("x: %d",x);
printf("i: %d",i);
return 0;
}
Output
x: 11
i: 11
2.2 Expressions with pre / post increment Operator
/* Expresssions using Post-Decrement Operator*/
#include<stdio.h> int main( )
{
int x,i;
i=10;
x=i--;
printf("x: %d",x);
printf("i: %d",i); return 0;
}
Output
x: 10
i: 9
2.3 Expression with conditional and assignment operators
Expression with conditional:-
 Any operator is used on three operands or variable is known
as Ternary Operator
 It can be represented with ? : . It is also called as conditional
operator
2.3 Expression with conditional and assignment operators
#include<stdio.h>
int main( )
{
int num;
printf("Enter the Number : ");
scanf("%d",&num);
(num%2==0)?printf("Evenn"):printf("Odd");
}
OUTPUT:
Enter the Number :
10 Even
2.4 If statement in expression - L value and R
value in expression
 Assignment Operator is used to assign value to an variable
 Assignment Operator is denoted by equal to sign
 Assignment Operator is binary operator which operates on two operands
 Assignment Operator have Two Values – L-Value and R-Value
Operator = copies R-Value into L-Value
 Assignment Operator have lower precedence than all available operators but has
higher precedence than comma Operator
2.4 If statement in expression - L value and R
value in expression
2.4 If statement in expression - L value in expression
L-Value and R-Value of Expression
a) L-Value stands for left value
 L-Value of Expressions refer to a memory locations
 In any assignment statement L-Value of Expression must be a container(i.e.
must have ability to hold the data)
 Variable is the only container in C programming thus L Value must be any
Variable.
 L Value cannot be a Constant, Function or any of the available data type in C
2.4 If statement in expression - L value in expression
#include<stdio.h>
int main( )
{
int num;
num=5;
return(0);
}
ExampleofL-
ValueExpression
#include<stdio.h>
int main( )
{
int num;
5 =num;//Error
return(0);
}
#include<stdio.h>
int main( )
{
const num;
num=20;//Error
return(0);
}
L-valuecannotbe aConstant L-valuecannotbe
aConstant
Variable
2.4 If statement in expression - L value in expression
#include<stdio.h>
#define MAX 20
int main( )
{
MAX=20;//Error
return(0);
}
#include<stdio.h>
enum {JAN,FEB,MARCH};
int main( )
{
JAN=20;//Error
return(0);
}
L-valuecannotbe aMACRO L-valuecannotbe aEnumConstant
2.4 If statement in expression R value in expression
R Value stands for Right value of the expression
• In any Assignment statement R-Value of Expression must be anything which is
capable of returning Constant Expression or Constant Value
Examples of R-Value of Expression
Variable Constant
Function Macro
Enum Constant Any other data type
 R value may be a Constant or Constant
Expression
 R value may be a MACRO
 R Value may be a variable
2.4 If statement in expression R value in expression
2.5 Control Statements
Control Statements
 Also called as Conditional Statement
 Decides order of execution based on conditions
 Helps repeat a group of statements
 Modifies control flow of program
 Decision Making
 Branching
2.5 Control Statements
 TypesofBranchingStatements
a) if statement
i. Simple if
ii. if…else statement
iii. nested if…else statement
iv. else…if statement
b) switch statement
c) goto statement
2.5 Control Statements
i. Simple if statement
 Basic if statement
 What is a condition?
 Executes statement block only if condition is true
 Syntax
if (condition)
{
Statements;
}
2.5 Control Statements
2.5 Control Statements
/* Simple if – Program to check whether a number is Odd*/ #include<stdio.h>
int main( )
{
int number;
printf(“Enter the Number:”); scanf(“%d,&number);
if(number%2==0)
{
printf(“TheNumber isEven”);
}
return 0;
}
Output
Enter avalue: 10342
Thenumber isEven
2.5 Control Statements
ii. If else statement
 Extension of basic if statement
 Takes care of True and False condition
 Number of Statement Blocks - 2
 Block 1 – True Condition
 Block 2 – False Condition
2.5 Control Statements
/* if else –Tocheck whether a number is Odd or Even*/
#include<stdio.h>
int main( )
{
int number;
printf(“Enter the Number: ”); scanf(“%d, &number);
if(number%2==0)
{
printf(“The Number is Even”);
}
else
{
printf(“The Number is Odd”);
}
return 0;
}
Output1
Enter the Number : 10341
The number is Odd
Output2
Enter the Number : 10342
The number is Even
2.5 Control Statements
iii. Nested if else statement
 Used when a series of decisions are involved
 Makes a choice between several alternatives
 New if else statement block is used within existing if else statement
block
2.5 Control Statements
#include <stdio.h>
void main( )
{char username;
int password;
printf("Username:");
scanf("%c",&username);
printf("Password:");
scanf("%d",&password );
if(username=='a')
{if(password==12345)
{printf("Login successful");}
else{printf("Password is incorrect, Try
again.");
}
}else
{printf("Username is incorrect, Try again.");
}
return 0;
}
Output1 Username: a Password:
12345 Login Successful
Output2 Username: a Password:
54321
Password is incorrect, Try again.
2.5 Control Statements
b) Switch statement
 Allows to make decisions from a number of choices
 Also called as Switch-Case-Default Statement
 Faster than nested if else statement, Easier to understand
 Rules for writing switch ( ) statement
 Expression in switch must be an integer value or a character constant
 No real numbers used in Expression
 Each case block and default block must end with break statements
 Default is optional, Case keyword must end with colon ( : )
 Default may be placed anywhere in the switch
 No two case constants are identical
2.5 Control Statements
switch(variable or expression)
{
case constant 1:
statements;
break;
….
case constant N;
statements;
break; default:
statements;
}
2.5 Control Statements
#include<stdio.h> int main( )
{
int a, b, choice;
printf(“nEnter Two Numbers: ”);
scanf(“%d%d”, &a,&b);
printf(“n Enter 1 for Addition”);
printf(“n Enter 2 for Subtraction”);
printf(“n Enter 3 for Multiplication”);
printf(“n Enter 4 for Division”);
printf(“ Enter your Choice”);
scanf(“%d”,&choice);
2.5 Control Statements
switch (choice)
{
case 1:
printf(“Sum is : %d”, a+b);
break;
case 2:
printf(“Difference is : %d”, a-b);
break;
case 3:
printf(“Multiplication is : %d”, a*b);
break;
case 4:
printf(“Difference is : %d”, a/b);
break;
default:
printf(“Invalid Choice:”);
}getch( );}
2.5 Control Statements
Enter two numbers
20
10
Enter 1 for Addition Enter 2 for Subtraction Enter 3 for
Multiplication
Enter 4 for Division Enter your Choice: 3
Product is : 200
2.6 Conditional and Unconditional Branching
For loop
 While loop
do while
 goto
Break
continue
2.6 Conditional and Unconditional Branching
The for loop
 Most commonly and popularly used loop structure
 Structure of the for loop
Initialize loop counter variable
Check for condition
Increment / Decrement the loop counter variable
 Syntax
for(initialization; condition; increment / decrement)
{
-----
}
2.6 Conditional and Unconditional Branching
i. for(i = 0; i < n; i++)
{
Statements;
}
ii. for(count = 0; count > n; count--)
{
Statements;
}
2.6 Conditional and Unconditional Branching
#include<stdio.h>
int main( )
{
int i, n, sum=0;
printf(“n Enter the value for n: ”);
scanf(“%d”, &n);
for (i =1; i<=n; i++)
{
sum = sum + i;
}
printf(“The sum of n Numbers is: %d”, sum);
return 0;
}
Output
Enter the value for n: 5
The sum of n Numbers
is: 15
2.6 Conditional and Unconditional Branching
2.6 Conditional and Unconditional Branching
Try it Out Yourself ! Write a C program to:
1) To print all even numbers from 1 to 100
2) To print all even numbers from 1 to n
3) To print table for any number
4) To calculate the sum of its digits
5) To check whether the entered number is Prime or not
6) To get a number as input and print it in reverse.
7) To check whether the number is Armstrong number
2.6 Conditional and Unconditional Branching
WHILE LOOP:-
The while loop evaluates the test expression inside the parenthesis ().
If the test expression is true, statements inside the body of while loop are executed. Then,
the test expression is evaluated again.
The process goes on until the test expression is evaluated to false.
If the test expression is false, the loop terminates (ends).
SYNTAX:
while( test expression)
{
// block of codes
}
2.6 Conditional and Unconditional Branching
WHILE LOOP:-
// Print numbers from 1 to 5
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%dn", i);
++i;
}
return 0;
}
Output: /tmp/nd80WOR9Gs.o 1 2 3 4 5
2.6 Conditional and Unconditional Branching
DO… WHILE LOOP:-
 The body of the loop is executed at least
once
 Syntax
do
{
statements;
}
while (condition);
2.6 Conditional and Unconditional Branching
DO… WHILE LOOP:-
// Program to add numbers until the user enters zero
#include <stdio.h>
int main()
{
int j=0;
//printf("n Enter the J value");
//scanf("%d",&j);
do
{
printf("Value of variable j is: %dn", j);
j++;
}
while (j<=5);
return 0;
}
Value of variable j is: 0
Value of variable j is: 1
Value of variable j is: 2
Value of variable j is: 3
Value of variable j is: 4
2.6 Conditional and Unconditional BranchingWhile Do Loop Do While Loop
Entry Controlled Loop Exit Controlled Loop
Test condition is checked before body of the
loop is executed
Test condition is checked after the body of the loop
is executed
Loop will not be executed if condition is
false
Loop will be executed at least once even if
condition is false
Top tested loop Bottom tested loop
2.6 Conditional and Unconditional Branching
The goto statement:
 It is used to transfer control to a specified label.
Here label is an identifier that specifies the place where the branch is to be made. Label can be any valid
variable name that is followed by a colon (:).
Note that label can be placed anywhere in the program either before or after the goto statement. Whenever the
goto statement is encountered the control is immediately transferred to the statements following the label.
Goto statement breaks the normal sequential execution of the program.
If the label is placed after the goto statement then it is called a forward jump and in case it is located before the
goto statement, it is said to be a backward jump.
Example: int num, sum=0;
read: // label for go to statement
printf("n Enter the number. Enter 999 to end : ");
scanf("%d", &num);
if (num != 999){
if(num < 0) goto read; // jump to label- read
sum += num;
goto read; // jump to label- read }
printf("n Sum of the numbers entered by the user is = %d", sum);
2.6 Conditional and Unconditional Branching
•The break statement is used to terminate the execution of the nearest enclosing loop in
which it appears.
• When compiler encounters a break statement, the control passes to the statement that
follows the loop in which the break statement appears.
•Syntax: break;
•In switch statement if the break statement is missing then every case from the matched
case label to the end of the switch, including the default, is executed.
BREAK STATEMENT
2.6 Conditional and Unconditional Branching
• It can only appear in the body of a loop.
•When the compiler encounters a continue statement then the rest of the statements in the loop are skipped and
the control is unconditionally transferred to the loop-continuation portion of the nearest enclosing loop
•Syntax: continue;
•Example:-
int i;
for(i=0; i<= 10; i++)
{ if (i==5)
continue;
printf(“t %d”, i);
}
CONTINUE STATEMENT
2.7 Array - Initialization and Declaration
An array is a collection of similar data elements.
These data elements have the same data type.
The elements of the array are stored in consecutive memory locations and are referenced by an index (also
known as the subscript).
Declaring an array means specifying three things:
 The data type- what kind of values it can store ex, int, char, float
 Name- to identify the array
 The size- the maximum number of values that the array can hold
Syntax:- dataype name[size];
Example: int register_no[12];
ARRAY DECLARATION
2.7 Array - Initialization and Declaration
ACCESSING ELEMENTS
• To access all the elements of the array, you must use a loop. That is, we can access all the elements of the array by
varying the value of the subscript into the array.
• But note that the subscript must be an integral value or an expression that evaluates to an integral value.
Example:
Given the base address of an array B[1300…..1900] as 1020 and size of each element is 2 bytes in the memory.
Find the address of B[1700].
Solution:
The given values are: B = 1020, LB = 1300, W = 2, I = 1700
Address of A [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820 [Ans]
CALCULATING THE ADDRESS OF ARRAY ELEMENTS
2.7 Array - Initialization and Declaration
Array of an element of an array say “A[ I ]” is calculated using the following formula:
Address of A [ I ] = B + W * ( I – LB )
Where, B = Base address
W = Storage Size of one element stored in the array (in byte)
I = Subscript of element whose address is to be found
LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)
Insert the Sub Title of Your Presentation

18CSS101J PROGRAMMING FOR PROBLEM SOLVING

  • 1.
    SRM INSTITUTE OFSCIENCE & TECHNOLOGY DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Mr. P.Gowsikraja M.E., (Ph.D.,) Assistant Professor (Sr. G) Department of Computer Science and Engineering, SRM IST, Chennai. 18CSS101J – PROGRAMMING FOR PROBLEM SOLVING
  • 2.
    UNIT II OPERATORS,CONTROL AND LOOPING STATEMENTSUNIT II 2.1 Relational and logical Operators, Condition Operators, Operator Precedence 2.2 Expressions with pre / post increment Operator 2.3 Expression with conditional and assignment operators 2.4 If statement in expression - L value and R value in expression 2.5Control Statements – 2.5.1 Simple IF, if and else - else if – 2.5.2 nested if, switch case - Iterations, 2.6 Conditional and Unconditional Branching – For loop - While loop - do while, goto, break, continue 2.7 Array - Initialization and Declaration- - Initialization: one Dimensional Array-Accessing, Indexing one Dimensional Array Operations - Array Programs – 1D
  • 3.
    2.1 OPERATOR PRECEDENCE OperatorPrecedence  Operator Precedence is used to determine the order of operators evaluated in an expression  Every operator has precedence (Priority)  Operator with higher precedence is evaluated first and the operator with least precedence is evaluated last  Associativityis used when two operators of same precedence appear in an expression  Determines the order of evaluation of those operators  Associativity can be either Left to Right or Right to Left
  • 4.
    2.1 OPERATOR PRECEDENCE Operators are listed in descending order of precedence  An Expression can contain several operators with equal precedence  Evaluation proceeds according to the associativity of the operator i.e.,  From Right to Left (or)  From Left to Right  Note:Order of operations is not defined by the language
  • 5.
  • 6.
    2.2 Expressions withpre / post increment Operator Expressions using Pre/Post Increment Operator  Increment operators increase the value of the variable by one  Decrement operators decrease the value of the variable by one  Syntax Increment operator: ++var_name; (or) var_name++; Decrement operator: – -var_name; (or) var_name – -;  Example Increment operator : Decrement operator : ++ i ; – – i ; i ++ ; i – – ;
  • 7.
    2.2 Expressions withpre / post increment Operator /* Expresssions using Pre-Increment Operator*/ #include<stdio.h> int main( ) { int x,i; i=10; x=++i; printf("x: %d",x); printf("i: %d",i); return 0; } Output x: 11 i: 11
  • 8.
    2.2 Expressions withpre / post increment Operator /* Expresssions using Post-Decrement Operator*/ #include<stdio.h> int main( ) { int x,i; i=10; x=i--; printf("x: %d",x); printf("i: %d",i); return 0; } Output x: 10 i: 9
  • 9.
    2.3 Expression withconditional and assignment operators Expression with conditional:-  Any operator is used on three operands or variable is known as Ternary Operator  It can be represented with ? : . It is also called as conditional operator
  • 10.
    2.3 Expression withconditional and assignment operators #include<stdio.h> int main( ) { int num; printf("Enter the Number : "); scanf("%d",&num); (num%2==0)?printf("Evenn"):printf("Odd"); } OUTPUT: Enter the Number : 10 Even
  • 11.
    2.4 If statementin expression - L value and R value in expression  Assignment Operator is used to assign value to an variable  Assignment Operator is denoted by equal to sign  Assignment Operator is binary operator which operates on two operands  Assignment Operator have Two Values – L-Value and R-Value Operator = copies R-Value into L-Value  Assignment Operator have lower precedence than all available operators but has higher precedence than comma Operator
  • 12.
    2.4 If statementin expression - L value and R value in expression
  • 13.
    2.4 If statementin expression - L value in expression L-Value and R-Value of Expression a) L-Value stands for left value  L-Value of Expressions refer to a memory locations  In any assignment statement L-Value of Expression must be a container(i.e. must have ability to hold the data)  Variable is the only container in C programming thus L Value must be any Variable.  L Value cannot be a Constant, Function or any of the available data type in C
  • 14.
    2.4 If statementin expression - L value in expression #include<stdio.h> int main( ) { int num; num=5; return(0); } ExampleofL- ValueExpression #include<stdio.h> int main( ) { int num; 5 =num;//Error return(0); } #include<stdio.h> int main( ) { const num; num=20;//Error return(0); } L-valuecannotbe aConstant L-valuecannotbe aConstant Variable
  • 15.
    2.4 If statementin expression - L value in expression #include<stdio.h> #define MAX 20 int main( ) { MAX=20;//Error return(0); } #include<stdio.h> enum {JAN,FEB,MARCH}; int main( ) { JAN=20;//Error return(0); } L-valuecannotbe aMACRO L-valuecannotbe aEnumConstant
  • 16.
    2.4 If statementin expression R value in expression R Value stands for Right value of the expression • In any Assignment statement R-Value of Expression must be anything which is capable of returning Constant Expression or Constant Value Examples of R-Value of Expression Variable Constant Function Macro Enum Constant Any other data type  R value may be a Constant or Constant Expression  R value may be a MACRO  R Value may be a variable
  • 17.
    2.4 If statementin expression R value in expression
  • 18.
    2.5 Control Statements ControlStatements  Also called as Conditional Statement  Decides order of execution based on conditions  Helps repeat a group of statements  Modifies control flow of program  Decision Making  Branching
  • 19.
    2.5 Control Statements TypesofBranchingStatements a) if statement i. Simple if ii. if…else statement iii. nested if…else statement iv. else…if statement b) switch statement c) goto statement
  • 20.
    2.5 Control Statements i.Simple if statement  Basic if statement  What is a condition?  Executes statement block only if condition is true  Syntax if (condition) { Statements; }
  • 21.
  • 22.
    2.5 Control Statements /*Simple if – Program to check whether a number is Odd*/ #include<stdio.h> int main( ) { int number; printf(“Enter the Number:”); scanf(“%d,&number); if(number%2==0) { printf(“TheNumber isEven”); } return 0; } Output Enter avalue: 10342 Thenumber isEven
  • 23.
    2.5 Control Statements ii.If else statement  Extension of basic if statement  Takes care of True and False condition  Number of Statement Blocks - 2  Block 1 – True Condition  Block 2 – False Condition
  • 24.
    2.5 Control Statements /*if else –Tocheck whether a number is Odd or Even*/ #include<stdio.h> int main( ) { int number; printf(“Enter the Number: ”); scanf(“%d, &number); if(number%2==0) { printf(“The Number is Even”); } else { printf(“The Number is Odd”); } return 0; } Output1 Enter the Number : 10341 The number is Odd Output2 Enter the Number : 10342 The number is Even
  • 25.
    2.5 Control Statements iii.Nested if else statement  Used when a series of decisions are involved  Makes a choice between several alternatives  New if else statement block is used within existing if else statement block
  • 26.
    2.5 Control Statements #include<stdio.h> void main( ) {char username; int password; printf("Username:"); scanf("%c",&username); printf("Password:"); scanf("%d",&password ); if(username=='a') {if(password==12345) {printf("Login successful");} else{printf("Password is incorrect, Try again."); } }else {printf("Username is incorrect, Try again."); } return 0; } Output1 Username: a Password: 12345 Login Successful Output2 Username: a Password: 54321 Password is incorrect, Try again.
  • 27.
    2.5 Control Statements b)Switch statement  Allows to make decisions from a number of choices  Also called as Switch-Case-Default Statement  Faster than nested if else statement, Easier to understand  Rules for writing switch ( ) statement  Expression in switch must be an integer value or a character constant  No real numbers used in Expression  Each case block and default block must end with break statements  Default is optional, Case keyword must end with colon ( : )  Default may be placed anywhere in the switch  No two case constants are identical
  • 28.
    2.5 Control Statements switch(variableor expression) { case constant 1: statements; break; …. case constant N; statements; break; default: statements; }
  • 29.
    2.5 Control Statements #include<stdio.h>int main( ) { int a, b, choice; printf(“nEnter Two Numbers: ”); scanf(“%d%d”, &a,&b); printf(“n Enter 1 for Addition”); printf(“n Enter 2 for Subtraction”); printf(“n Enter 3 for Multiplication”); printf(“n Enter 4 for Division”); printf(“ Enter your Choice”); scanf(“%d”,&choice);
  • 30.
    2.5 Control Statements switch(choice) { case 1: printf(“Sum is : %d”, a+b); break; case 2: printf(“Difference is : %d”, a-b); break; case 3: printf(“Multiplication is : %d”, a*b); break; case 4: printf(“Difference is : %d”, a/b); break; default: printf(“Invalid Choice:”); }getch( );}
  • 31.
    2.5 Control Statements Entertwo numbers 20 10 Enter 1 for Addition Enter 2 for Subtraction Enter 3 for Multiplication Enter 4 for Division Enter your Choice: 3 Product is : 200
  • 32.
    2.6 Conditional andUnconditional Branching For loop  While loop do while  goto Break continue
  • 33.
    2.6 Conditional andUnconditional Branching The for loop  Most commonly and popularly used loop structure  Structure of the for loop Initialize loop counter variable Check for condition Increment / Decrement the loop counter variable  Syntax for(initialization; condition; increment / decrement) { ----- }
  • 34.
    2.6 Conditional andUnconditional Branching i. for(i = 0; i < n; i++) { Statements; } ii. for(count = 0; count > n; count--) { Statements; }
  • 35.
    2.6 Conditional andUnconditional Branching #include<stdio.h> int main( ) { int i, n, sum=0; printf(“n Enter the value for n: ”); scanf(“%d”, &n); for (i =1; i<=n; i++) { sum = sum + i; } printf(“The sum of n Numbers is: %d”, sum); return 0; } Output Enter the value for n: 5 The sum of n Numbers is: 15
  • 36.
    2.6 Conditional andUnconditional Branching
  • 37.
    2.6 Conditional andUnconditional Branching Try it Out Yourself ! Write a C program to: 1) To print all even numbers from 1 to 100 2) To print all even numbers from 1 to n 3) To print table for any number 4) To calculate the sum of its digits 5) To check whether the entered number is Prime or not 6) To get a number as input and print it in reverse. 7) To check whether the number is Armstrong number
  • 38.
    2.6 Conditional andUnconditional Branching WHILE LOOP:- The while loop evaluates the test expression inside the parenthesis (). If the test expression is true, statements inside the body of while loop are executed. Then, the test expression is evaluated again. The process goes on until the test expression is evaluated to false. If the test expression is false, the loop terminates (ends). SYNTAX: while( test expression) { // block of codes }
  • 39.
    2.6 Conditional andUnconditional Branching WHILE LOOP:- // Print numbers from 1 to 5 #include <stdio.h> int main() { int i = 1; while (i <= 5) { printf("%dn", i); ++i; } return 0; } Output: /tmp/nd80WOR9Gs.o 1 2 3 4 5
  • 40.
    2.6 Conditional andUnconditional Branching DO… WHILE LOOP:-  The body of the loop is executed at least once  Syntax do { statements; } while (condition);
  • 41.
    2.6 Conditional andUnconditional Branching DO… WHILE LOOP:- // Program to add numbers until the user enters zero #include <stdio.h> int main() { int j=0; //printf("n Enter the J value"); //scanf("%d",&j); do { printf("Value of variable j is: %dn", j); j++; } while (j<=5); return 0; } Value of variable j is: 0 Value of variable j is: 1 Value of variable j is: 2 Value of variable j is: 3 Value of variable j is: 4
  • 42.
    2.6 Conditional andUnconditional BranchingWhile Do Loop Do While Loop Entry Controlled Loop Exit Controlled Loop Test condition is checked before body of the loop is executed Test condition is checked after the body of the loop is executed Loop will not be executed if condition is false Loop will be executed at least once even if condition is false Top tested loop Bottom tested loop
  • 43.
    2.6 Conditional andUnconditional Branching The goto statement:  It is used to transfer control to a specified label. Here label is an identifier that specifies the place where the branch is to be made. Label can be any valid variable name that is followed by a colon (:). Note that label can be placed anywhere in the program either before or after the goto statement. Whenever the goto statement is encountered the control is immediately transferred to the statements following the label. Goto statement breaks the normal sequential execution of the program. If the label is placed after the goto statement then it is called a forward jump and in case it is located before the goto statement, it is said to be a backward jump. Example: int num, sum=0; read: // label for go to statement printf("n Enter the number. Enter 999 to end : "); scanf("%d", &num); if (num != 999){ if(num < 0) goto read; // jump to label- read sum += num; goto read; // jump to label- read } printf("n Sum of the numbers entered by the user is = %d", sum);
  • 44.
    2.6 Conditional andUnconditional Branching •The break statement is used to terminate the execution of the nearest enclosing loop in which it appears. • When compiler encounters a break statement, the control passes to the statement that follows the loop in which the break statement appears. •Syntax: break; •In switch statement if the break statement is missing then every case from the matched case label to the end of the switch, including the default, is executed. BREAK STATEMENT
  • 45.
    2.6 Conditional andUnconditional Branching • It can only appear in the body of a loop. •When the compiler encounters a continue statement then the rest of the statements in the loop are skipped and the control is unconditionally transferred to the loop-continuation portion of the nearest enclosing loop •Syntax: continue; •Example:- int i; for(i=0; i<= 10; i++) { if (i==5) continue; printf(“t %d”, i); } CONTINUE STATEMENT
  • 46.
    2.7 Array -Initialization and Declaration An array is a collection of similar data elements. These data elements have the same data type. The elements of the array are stored in consecutive memory locations and are referenced by an index (also known as the subscript). Declaring an array means specifying three things:  The data type- what kind of values it can store ex, int, char, float  Name- to identify the array  The size- the maximum number of values that the array can hold Syntax:- dataype name[size]; Example: int register_no[12]; ARRAY DECLARATION
  • 47.
    2.7 Array -Initialization and Declaration ACCESSING ELEMENTS • To access all the elements of the array, you must use a loop. That is, we can access all the elements of the array by varying the value of the subscript into the array. • But note that the subscript must be an integral value or an expression that evaluates to an integral value. Example: Given the base address of an array B[1300…..1900] as 1020 and size of each element is 2 bytes in the memory. Find the address of B[1700]. Solution: The given values are: B = 1020, LB = 1300, W = 2, I = 1700 Address of A [ I ] = B + W * ( I – LB ) = 1020 + 2 * (1700 – 1300) = 1020 + 2 * 400 = 1020 + 800 = 1820 [Ans] CALCULATING THE ADDRESS OF ARRAY ELEMENTS
  • 48.
    2.7 Array -Initialization and Declaration Array of an element of an array say “A[ I ]” is calculated using the following formula: Address of A [ I ] = B + W * ( I – LB ) Where, B = Base address W = Storage Size of one element stored in the array (in byte) I = Subscript of element whose address is to be found LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)
  • 49.
    Insert the SubTitle of Your Presentation