|
Previous Next Page
1.11 The Conditional Operator
expression1 ?
expression2 : expression3
The expression1 is evaluated first. If condition becomes
true then expression2 is the result else expression3 is the
result.
max = (num1>num2) ? num1 : num2;
1.12 The Sizeof Operator
It returns the length, in bytes, of the
variable.
#include<stdio.h>
main()
{
float f;
clrscr();
printf("The Size of the f is %d\n", sizeof(f));
printf("Size of double is %d\n", sizeof (double));
}
1.13 The Comma Operator
The comma operator is used to string
together several expressions.
a = (b = 5, b+2) \;
1.14 C Shorthand
a = a+5;
can be written as a+=5;
a = a * b;
can be written as a *= b;
1.15 Compiling and Running C Program.
Source Code -----After compile----->
Object Code ------ Run----> Executable Code
(
.c)
(.obj) (.exe)
For compile Alt + F9
Run Ctrl + F9 (Compile & Run )
Output Alt + F5
2.1
If Statement
If the If expression becomes true then
statement following the if statement is executed otherwise
following the else statement is executed.
#include<stdio.h>
main()
{
int x, y;
scanf("%d, %d", &x , &y);
if(x>y)
printf("The number %d is greater than %d\n", x,y);
else
printf("The number %d is greater than %d\n",y,x);
}
2.2
Switch Statement
The switch statement is multiway decision
maker that tests the value of an expression against a list
of integer or character constants. When a match found, the
statement associated with that constant are executed. When
match found then the statement executed and the program
controls exit from the switch by break() statement.
#include<stdio.h>
main()
{
char ch;
clrscr();
printf("\n Enter an alphabet in lowercase (a-z):");
scanf("%c", &ch);
if(ch<'a' || ch> 'z')
printf("\n Character is not an alphabet in lowercase") ;
else
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("\nCharacter is a vowel"):
break;
case 'z':
printf("\nLast Alphabet (z) was entered");
default:
printf("\ncharacter is a consonant");
break;
}
}
2.3
The For Loop
The for loop continues to executes as long
as the conditional test evaluates to true. When the
condition becomes false, control is transferred to the
statements after the for loop.
#include<stdio.h>
main()
{
int a;
printf("The even numbers between 1 and 25 are :");
for(a=2; a<=25; a+=2)
printf("%d\n",a);
}
2.4
The While Loop
While loop is like for loop difference is that while
loop check the condition at the beginning of the loop, which
means that the loop code is not executed, if the condition
is false at the start.
#include<stdio.h>
main()
{
int a;
printf("The even numbers between 1 and 25 are :");
while(a<25)
{
printf("%d\n",a);
a+=2;
}
}
2.5 The Do While Loop
Do While loop is similar to While loop
difference only is that it check the condition after
statement execution. so here statement executed at least
single time. (if the condition become false or true in both
cases)
#include<stdio.h>
main()
{
int a;
printf("The even numbers between 1 and 25 are :");
do
{
printf("%d\n",a);
a+=2;
}while(a<25);
}
Previous Next Page
|