Friday, November 8, 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;

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.