Date post: | 16-Apr-2017 |
Category: |
Technology |
Upload: | jigeno |
View: | 542 times |
Download: | 0 times |
Program 4-1.1
• #include<stdio.h>• #include<conio.h>• #include<iostream>• int main(){• printf(“hello world!”);• getch();• return 0;• }
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
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
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
scanf
• scanf(“control_string”, [ampersand][variable]);or
• scanf(“%f”, &x);
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);• }
Output:
Enter value for an int and a long: 524 79735Your int is 524 and long is 79735
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);• }
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
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?
Logical OperatorsOperator Symbol
Operator function/Meaning
|| OR&& AND! Logical Not
RULES:OR Operator AND OperatorTrue || True = TrueTrue || False = TrueFalse || True = TrueFalse || False = False
True && True = TrueTrue && False = FalseFalse && True = FalseFalse && False = False
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.
Sequence
Example: #include<stdio.h>main(){ int number; printf(“Enter a number: ”); scanf(“%i”, &number); printf(“You entered %i”, number);}
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; }
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(); }
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; }
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(); }
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
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(); }
Switch case statementSyntax: switch(basis){ case 1: statement; break; case 2: statement; break; case n: statement: break; default: statement; break; }
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(); }
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.
Syntax: while(condition){ statement; }
Program 4-1.5
#include<stdio.h>#include<conio.h>main(){ int x=1; while(x<=5){ printf(“%i ”,x); x=x+1; } getch(); }
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.
Syntax:do{ statement; } while(condition);
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(); }
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.
Syntax: for(initialization;condition;counter){ statement; }
Program 4-1.7Example:
#include<stdio.h>#include<conio.h>main(){ int x; for(x=1;x<=5;x++) { printf(“%i ”,x); } getch(); }
Activity Challenge
/* Displaying Fibonacci sequence up to nth term where n is entered by user. */