A Brief Note On Instructions

Submitted By Ramesh-Thota
Words: 1090
Pages: 5

1

“C” PROGRAMMING
TUTORIALS

Session – 2

2

AGENDA
RECAP OF SESSION-1
ALL *IF*S & *BUT*S
NEVER ENDING *LOOPS*
LET’S TALK *FUN*CTIONS
Session – 2

RECAP
OF
SESSION-1

From coding to execution
 Tools

Program segments
 stack, heap, data, code
 Data Types
 unsigned/signed – long, short, char
 float, double – someone to teach us
 typecast
 Storage Classes
 auto, register, static, extern
 Keywords
 const, volatile
 Operator precedence
 NO SHORTCUTS, someone to demo their hard work

Session – 2

3

RECAP OF SESSION-1
Example#1

unsigned long a =10;
Unsigned long b = 0; b = a++ + ++a; printf("%d,%d,%d,%d",b,a++,a,++a); Example#2 unsigned long a = 20; const unsigned long b = 10; b = ++a – a--; printf("%d,%d,%d,%d",b--,a+1,--a,a++); Session – 2

4

ALL*IF*S & *BUT*S
 Simple example of IF-ELSE
If (B is TRUE)
{ A = x; } else { A = y; }

 It can get messy and nested quickly based on the number of conditions
 Yes, we are talking about *nested* IF-ELSE
If (B is 1)
{ A = x; } else if (B is 2)
{ A = y; } else if (B is 3)
{ A = z; }

goes on …
 Beware, watch out carefully the condition statement for
 ==
Vs
=
 &&
Vs
&
 ||
Vs
|

Session – 2

5

ALL*IF*S & *BUT*S
Example# 1 unsigned long count = 10;

Example# 2 long a = -12

if (count = 1) { printf (“[%d]”, ++count); } else if (count = 10) { printf (“[%d]”, --count); } else { printf(“[%d]”, count); }

if (a) { printf(“TRUE”); } else { printf(“FALSE”); }

Example# 3 a = 5; b = 0; c = 0; if (a || (b=1) & (c == 0)) { printf(“[%d] [%d] [%d]”, ++a, ++b, ++c); } else { printf(“[%d] [%d] [%d]”, a++, b++, c++); }

Session – 2

6

ALL*IF*S & *BUT*S

7

 A friendly version of *nested* IF-ELSE
 Not necessary that all nested IF-ELSE can be converted to SWITCH-CASE
 Each case block shall have a BREK unless if desired to do so
 In case of missing BREAK, execution simply continues with next case until it finds a BREAK or SWITCH block ends
 Beware and watch out for missing breaks for CASE blocks
 DEFAULT, if written, will be the case if none of the listed cases match
 Simple example:
Switch(B)
{ case 0: { A = y } break; case 1: { A = x } break; default: { A = 0 } break; }

Session – 2

ALL*IF*S & *BUT*S
Example# 1 unsigned long a = 9; unsigned long b = 11;

Example# 2 unsigned long a = 11; unsigned long b = 9;

switch(a)
{
case 9: { a++; --b;
}
case 11: { a = a+b; a--;
}break;
case 19: { a = b = 0;
}
default: { a = 9; b = 11;
}
} printf(“[%d] [%d]”, a, b);

Session – 2

8

ALL*IF*S & *BUT*S

9

 Which is efficient - nested IF-ELSE or SWITCH-CASE?
 Answer is not either way, it depends on compiler and also the CASE values grouping and range
 Read for yourself at leisure
http://www.eventhelix.com/realtimemantra/Basics/CToAssemblyTransla
tion3.htm
http://books.google.co.in/books?
id=vdk4ZGRqMskC&pg=PA197&lpg=PA197&dq=ARM+assembly+for+sw itch+case&source=bl&ots=UJFgqJjZ8H&sig=T9VGU9ak6WnlqVoyOSv73d2_JQ&hl=en&ei=FleSSonIO8WIkQWJ6eC7Cg&sa=X&oi=book_result& ct=result&resnum=6#v=onepage&q=&f=false
 Another way to represent a simple if (cond) { … } else { … }
 if (B is TRUE) { A = x; } else { A = y; }
 A = (B)? x:y;
 Typically used in simple assignment statements with a decision and/or return a value based on simple decision

Session – 2

NEVER
ENDING
*LOOPS*


10

while loop
 syntax: while (condition) { … }
Execution enters the loop if condition is TRUE else loop terminates

 do-while loop
 syntax: do { … } while(condition);
 Execution always enters the loop and terminates loop at the end of loop block if condition is FALSE else loop continues
 Difference between *while* loop and *do-while* loop
 while
 entry control loop
 do-while  exit control loop
 Example for do-while
 do{
Read a line of file;
} while (content of read has some special data, continue);
 In the above example, if you don’t use do-while you may have to perform a extra read outside while and then kickoff the loop

Session – 2

NEVER ENDING *LOOPS*
 What is the output

unsigned long i = 1;