Bsit1
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Bsit1

on

  • 563 views

 

Statistics

Views

Total Views
563
Views on SlideShare
430
Embed Views
133

Actions

Likes
0
Downloads
1
Comments
0

1 Embed 133

http://jigeno.wordpress.com 133

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Bsit1 Presentation Transcript

  • 1. Program 4-1.1 • #include<stdio.h> • #include<conio.h> • #include<iostream> • int main(){ • printf(“hello world!”); • getch(); • return 0; • }
  • 2. Reserved words auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
  • 3. Data Type Specifiers for printf() SPECIFIER DATA TYPES SAMPLE OUTPUT c char “%c”, ‘A’ A d int “%d”, 123 123 i int “%i”, 345 345 f float or double “%f”, 123.45 123.45000 0 g float or double “%g”, 123.98 123.45
  • 4. Size Modifiers Statement Output “%4i”, 123 *123 “%04i”, 123 0123 “%8.2f”, 12.345 ***12.34 “%.2f”, 1234.567 1234.56 “%1.2f”, 1234.23 1234.23 “%5.1”, 12.3678 12.4
  • 5. scanf • scanf(“control_string”, *ampersand+*variable+); or • scanf(“%f”, &x);
  • 6. Program 4-1.2: • #include<stdio.h> • void main(){ • int i; • long l; • float f; • double d; • printf(“Enter a values for an int and a long: ”); • scanf(“%i %li”, &I, &l); • printf(“Your int is %i and long is %linn”, i,l); • • printf(“Now, enter values for float and a double: ”); • scanf(“%f %f”, &f, &d); • printf(“Your float is %f and double is %fnn”, f,d); • }
  • 7. Output: Enter value for an int and a long: 524 79735 Your int is 524 and long is 79735
  • 8. Program 4-1.3 • #include<stdio.h> • void main(){ • int x, y, sum; • float quotient; • printf(“Enter a number: “); • scanf(“%i”, &x); • printf(“Enter another number: ”); • scanf(“%i”, &y); • sum = x + y; • printf(“Sum: %”, sum); • }
  • 9. Relational Operators Operator symbol Operator function/Meaning < Is less than to > Is greater than to <= Is less than or equal to >= Is greater than or equal to == Is equal to != Not equal to
  • 10. Ex. x = 45; -> the value of variable x is equal to 45 y = 40; -> the value of variable y is equal to 40 y==x -> is y equal to x? x<=y -> is x less than or equal to y? x!=y -> is x not equal to y?
  • 11. Logical Operators Operator Symbol Operator function/Meani ng || OR && AND ! Logical Not
  • 12. RULES: OR Operator AND Operator True || True = True True || False = True False || True = True False || False = False True && True = True True && False = False False && True = False False && False = False
  • 13. The Three Control Structures or program constructs: • Sequence – The instructions are executed in a serial manner, one after another. • Selection – It provides a decision point that enables the program to choose one between two or more pathways. • Iteration – Repeats a set of instructions a number of times based on the condition stated.
  • 14. Sequence Example: #include<stdio.h> main(){ int number; printf(“Enter a number: ”); scanf(“%i”, &number); printf(“You entered %i”, number); }
  • 15. Selection If & else statement Syntax: Single if – the ody of if statement will be executed only if the condition’s. Syntax: if(condition){ statement/body; }
  • 16. Example: #include<stdio.h> #include<conio.h> main(){ int x; printf(“Enter number: ”); scanf(“%i”, &x); if(x>=5){ printf(“You entered a value greater than four”); } getch(); }
  • 17. If & else – the body of “if” will be executed if the condition’s outcome is true, if false, the body of “else” statement will be executed. Syntax: if(condition){ body/statement; } Else{ Body/statement; }
  • 18. Example: #include<stdio.h> #include<conio.h> main(){ int grade; printf(“Enter number: ”); scanf(“%i”, &grade); if(grade>=5){ printf(“You Passed.”); } else{ printf(“You failed.”); } getch(); }
  • 19. If, else if & else – if the condition’s outcome of “if” statement is true, its body will be executed but if not it will test the condition of “else if” statement, if it is true it will be executed and if not it will execute the body of “else” statement
  • 20. Example #include<stdio.h> #include<conio.h> main(){ int x; printf(“Enter number from 1 - 3: ”); scanf(“%i”, &x); if(x==1){ printf(“You entered one”); } else if(number==2){ printf(“You entered two”); } else if(number==3){ printf(“You entered three”); } else{ printf(“Invalid Input!”); } getch(); }
  • 21. Switch case statement Syntax: switch(basis){ case 1: statement; break; case 2: statement; break; case n: statement: break; default: statement; break; }
  • 22. Example #include<stdio.h> #include<conio.h> main(){ int x; printf(“Enter a number from 1 - 3: ”); scanf(“%i”, &x); switch(x){ case 1: printf(“you entered one”); break; case 2: printf(“you entered two”); break; case 3: printf(“you entered three”); break; default: printf(“invalid input”); break; } getch(); }
  • 23. Iteration Pre-test Loop - while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.The while construct consists of a block of code and a condition. The condition is evaluated, and if the condition is true, the code within the block is executed. This repeats until the condition becomes false. Because while loop checks the condition before the block is executed.
  • 24. Syntax: while(condition){ statement; }
  • 25. Program 4-1.5 #include<stdio.h> #include<conio.h> main(){ int x=1; while(x<=5){ printf(“%i ”,x); x=x+1; } getch(); }
  • 26. Post-test Loop - is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The do while construct consists of a block of code and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false. Because do while loops check the condition after the block is executed.
  • 27. Syntax: do{ statement; } while(condition);
  • 28. Program 4-1.6: Example: #include<stdio.h> #include<conio.h> main(){ int x=1; do { printf(“%i ”,x); x=x+1; } while(x<=5); getch(); }
  • 29. Counter-controlled Loop syntax – is a programming statement which allows code to be repeatedly executed. Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For loops are also typically used when the number of iterations is known before entering the loop. For loops are shorthand way to make loops when the number of iterations is known, as a for loop can be written as a while loop.
  • 30. Syntax: for(initialization;condition;counter){ statement; }
  • 31. Program 4-1.7 Example: #include<stdio.h> #include<conio.h> main(){ int x; for(x=1;x<=5;x++) { printf(“%i ”,x); } getch(); }
  • 32. Activity Challenge /* Displaying Fibonacci sequence up to nth term where n is entered by user. */