Tuesday, October 22, 2019
Essay about C Session2
Essay about C Session2 Essay about C Session2 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) } 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 ï ±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;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.