+ All Categories

Bsit1

Date post: 16-Apr-2017
Category:
Upload: jigeno
View: 542 times
Download: 0 times
Share this document with a friend
33
Transcript
Page 1: Bsit1
Page 2: Bsit1

Program 4-1.1

• #include<stdio.h>• #include<conio.h>• #include<iostream>• int main(){• printf(“hello world!”);• getch();• return 0;• }

Page 3: Bsit1

Reserved wordsauto double int structbreak else long switchcase enum register typedefchar extern return unionconst float short unsignedcontinue for signed voiddefault goto sizeof volatiledo if static while

Page 4: Bsit1

Data Type Specifiers for printf()SPECIFIER DATA

TYPESSAMPLE OUTPUT

c char “%c”, ‘A’ A

d int “%d”, 123 123

i int “%i”, 345 345

f float or double

“%f”, 123.45

123.450000

g float or double

“%g”, 123.98

123.45

Page 5: Bsit1

Size ModifiersStatement 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

Page 6: Bsit1

scanf

• scanf(“control_string”, [ampersand][variable]);or

• scanf(“%f”, &x);

Page 7: Bsit1

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 %li\n\n”, i,l);• • printf(“Now, enter values for float and a double: ”);• scanf(“%f %f”, &f, &d);• printf(“Your float is %f and double is %f\n\n”, f,d);• }

Page 8: Bsit1

Output:

Enter value for an int and a long: 524 79735Your int is 524 and long is 79735

Page 9: Bsit1

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);• }

Page 10: Bsit1

Relational OperatorsOperator 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

Page 11: Bsit1

Ex. x = 45; -> the value of variable x is equal to 45y = 40; -> the value of variable y is equal to 40y==x -> is y equal to x?x<=y -> is x less than or equal to y?x!=y -> is x not equal to y?

Page 12: Bsit1

Logical OperatorsOperator Symbol

Operator function/Meaning

|| OR&& AND! Logical Not

Page 13: Bsit1

RULES:OR Operator AND OperatorTrue || True = TrueTrue || False = TrueFalse || True = TrueFalse || False = False

True && True = TrueTrue && False = FalseFalse && True = FalseFalse && False = False

Page 14: Bsit1

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.

Page 15: Bsit1

Sequence

Example: #include<stdio.h>main(){ int number; printf(“Enter a number: ”); scanf(“%i”, &number); printf(“You entered %i”, number);}

Page 16: Bsit1

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; }

Page 17: Bsit1

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(); }

Page 18: Bsit1

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; }

Page 19: Bsit1

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(); }

Page 20: Bsit1

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

Page 21: Bsit1

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(); }

Page 22: Bsit1

Switch case statementSyntax: switch(basis){ case 1: statement; break; case 2: statement; break; case n: statement: break; default: statement; break; }

Page 23: Bsit1

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(); }

Page 24: Bsit1

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.

Page 25: Bsit1

Syntax: while(condition){ statement; }

Page 26: Bsit1

Program 4-1.5

#include<stdio.h>#include<conio.h>main(){ int x=1; while(x<=5){ printf(“%i ”,x); x=x+1; } getch(); }

Page 27: Bsit1

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.

Page 28: Bsit1

Syntax:do{ statement; } while(condition);

Page 29: Bsit1

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(); }

Page 30: Bsit1

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.

Page 31: Bsit1

Syntax: for(initialization;condition;counter){ statement; }

Page 32: Bsit1

Program 4-1.7Example:

#include<stdio.h>#include<conio.h>main(){ int x; for(x=1;x<=5;x++) { printf(“%i ”,x); } getch(); }

Page 33: Bsit1

Activity Challenge

/* Displaying Fibonacci sequence up to nth term where n is entered by user. */