Input and output operators in C (5th Day)
Scanf()
We can read any value using this function.
Syntax:
scanf(“%d”,&a);
Meaning:
Here the compiler accept a value (integer type) from the keyboard and it is saved in the address of ‘a’. Here ‘%d’ indicates that the value is integer data type. To read other data types refer the following:
%d reads Integer data type
%f reads Floating point data type
%c reads Single character
%s reads String value
%o reads Octal integer
%x reads Hexadecimal integer
printf()
Used to display an output data in standard output device.
Syntax:
printf(“i-St@r Electronics”);
Above statement displays i-St@r Electronics on the monitor
Another syntax:
printf(“%d”,a); It displays the value of variable ‘a’ in the monitor.
Decision making and branching in C program: if, else, goto, switch statements
We can arrange the execution sequence of a program in different ways using decision making and branching statements. These instructions are also called as control statements.Simple if-else statements
‘if-else’ statements are very powerful and effective tool for decision making in C. In this situation, program codes are divided in to two sections and we are selecting one part using ‘if’ statement. ‘else’ part is optional so we can neglect it if not needed.Syntax:
Here if the expression is true, the statement1 will execute and if it is false, statement2 will execute.
else-if ladder statements
Simple ‘if-else’ statements are used to select a part of codes from two sections. But ‘else if’ ladder is used to make decisions when there are many conditions.Example:
Here the compiler makes decision from 4 conditions.
goto statement
It is used in unconditional branching. The syntax of goto statement is given below:Here the Statement 1 to n will not be executed, before the execution, program sequence branched to Label.
switch statement
This statement is used for multiple branching. Here the branching takes place according to a valid expression.Syntax:
Ads
0 comments:
Post a Comment